查找和替换一个给定的输入字符串,比如abc,并替换成另一个字符串,比如文件/tmp/file.txt中的XYZ,最简单的方法是什么?

我正在编写一个应用程序,并使用IronPython通过SSH执行命令-但我不太了解Unix,也不知道该寻找什么。

我听说Bash除了是一种命令行界面外,还可以是一种非常强大的脚本语言。所以,如果这是真的,我假设你可以执行这样的操作。

我可以用bash实现它吗?要实现我的目标,最简单的(一行)脚本是什么?


当前回答

我在其他帖子中发现了这个帖子,我认为它包含了最完整的答案,所以我也添加了我的答案:

sed and ed are so useful...by hand. Look at this code from @Johnny: sed -i -e 's/abc/XYZ/g' /tmp/file.txt When my restriction is to use it in a shell script, no variable can be used inside in place of "abc" or "XYZ". The BashFAQ seems to agree with what I understand at least. So, I can't use: x='abc' y='XYZ' sed -i -e 's/$x/$y/g' /tmp/file.txt #or, sed -i -e "s/$x/$y/g" /tmp/file.txt but, what can we do? As, @Johnny said use a while read... but, unfortunately that's not the end of the story. The following worked well with me: #edit user's virtual domain result= #if nullglob is set then, unset it temporarily is_nullglob=$( shopt -s | egrep -i '*nullglob' ) if [[ is_nullglob ]]; then shopt -u nullglob fi while IFS= read -r line; do line="${line//'<servername>'/$server}" line="${line//'<serveralias>'/$alias}" line="${line//'<user>'/$user}" line="${line//'<group>'/$group}" result="$result""$line"'\n' done < $tmp echo -e $result > $tmp #if nullglob was set then, re-enable it if [[ is_nullglob ]]; then shopt -s nullglob fi #move user's virtual domain to Apache 2 domain directory ...... As one can see if nullglob is set then, it behaves strangely when there is a string containing a * as in: <VirtualHost *:80> ServerName www.example.com which becomes <VirtualHost ServerName www.example.com there is no ending angle bracket and Apache2 can't even load. This kind of parsing should be slower than one-hit search and replace but, as you already saw, there are four variables for four different search patterns working out of one parse cycle.

在给定的假设下,我能想到的最合适的解决方案。

其他回答

要以非交互式方式编辑文件中的文本,需要就地文本编辑器,例如vim。

下面是一个简单的例子,如何从命令行使用它:

vim -esnc '%s/foo/bar/g|:wq' file.txt

这相当于ex editor的@slim answer,基本上是一样的。

这里有一些实际的例子。

在文件中用bar替换文本foo:

ex -s +%s/foo/bar/ge -cwq file.txt

删除多个文件的尾随空格:

ex +'bufdo!%s/\s\+$//e' -cxa *.txt

故障处理(终端卡死):

添加-V1参数以显示详细消息。 强制退出:-cwq!

参见:

如何非交互地编辑文件(例如在管道中)?在Vi SE

你也可以使用ed命令进行文件内搜索和替换:

# delete all lines matching foobar 
ed -s test.txt <<< $'g/foobar/d\nw' 

参见“使用ed通过脚本编辑文件”。

这是一篇旧文章,但对于任何想要使用变量的人来说,@centurian说单引号意味着不会展开任何内容。

获取变量的一个简单方法是做字符串拼接,因为这是通过在bash中并置完成的,以下应该工作:

sed -i -e "s/$var1/$var2/g" /tmp/file.txt

如果在多个文件中一起做更改,我们可以在一行中做:-

user_name='whoami'
for file in file1.txt file2.txt file3.txt; do sed -i -e 's/default_user/${user_name}/g' $file; done

补充如果万一可能有用。

Bash与其他shell一样,只是一个协调其他命令的工具。通常您会尝试使用标准的UNIX命令,但当然您可以使用Bash调用任何东西,包括您自己编译的程序、其他shell脚本、Python和Perl脚本等。

在这种情况下,有几种方法可以做到这一点。

如果你想读取一个文件,并将其写入另一个文件,执行搜索/替换,使用sed:

    sed 's/abc/XYZ/g' <infile >outfile

如果你想就地编辑文件(就像在编辑器中打开文件,编辑它,然后保存它),向行编辑器'ex'提供指令

    echo "%s/abc/XYZ/g
    w
    q
    " | ex file

示例就像没有全屏模式的vi。您可以向它输入与vi的:prompt相同的命令。