Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何使用sed仅替换第二个匹配行_Linux_Unix_Sed - Fatal编程技术网

Linux 如何使用sed仅替换第二个匹配行

Linux 如何使用sed仅替换第二个匹配行,linux,unix,sed,Linux,Unix,Sed,使用sed: $ cat file cat cat dog cat dog puppy dog cat 我的目标是用大狗只替换第二只狗,但这不会发生在: $ sed 's/dog/big_dog/' my_file > new_file cat new_file cat cat big_dog cat big_dog puppy big_dog cat 如何仅替换第二个引用,即: $ sed 's/dog/big_dog/2' my_file > new_file cat do

使用sed:

$ cat file
cat cat
dog cat
dog puppy
dog cat
我的目标是用
大狗
只替换第二只
,但这不会发生在:

$ sed 's/dog/big_dog/' my_file > new_file
cat new_file
cat cat 
big_dog cat
big_dog puppy
big_dog cat
如何仅替换第二个引用,即:

$ sed 's/dog/big_dog/2' my_file > new_file
cat
dog cat
dog puppy
dog cat

如注释中所述,此选项将替换第二行上的匹配项:

cat
dog cat
big_dog puppy
dog cat
要使用sed替换第二个匹配项,请使用:

$ sed '2s/dog/big_dog/' your_file
dog cat
big_dog puppy
dog cat

如注释中所述,此选项将替换第二行上的匹配项:

cat
dog cat
big_dog puppy
dog cat
要使用sed替换第二个匹配项,请使用:

$ sed '2s/dog/big_dog/' your_file
dog cat
big_dog puppy
dog cat

使用下面的命令将单词dog替换为第2行中的big_dog

sed ':a;N;$!ba;s/dog/big_dog/2'   your_file_with_foo_on_first_row_to_demonstrate
foo
dog cat
big_dog puppy
dog cat
如果要从第二个替换为第三个,则可以使用以下命令:-

# sed -i '2 s/dog/big_dog/g' my_file
# cat my_file
Output:-
dog cat
big_dog puppy
dog cat

使用下面的命令将单词dog替换为第2行中的big_dog

sed ':a;N;$!ba;s/dog/big_dog/2'   your_file_with_foo_on_first_row_to_demonstrate
foo
dog cat
big_dog puppy
dog cat
如果要从第二个替换为第三个,则可以使用以下命令:-

# sed -i '2 s/dog/big_dog/g' my_file
# cat my_file
Output:-
dog cat
big_dog puppy
dog cat

以下
awk
也可能对您有所帮助

# sed -i '2,3 s/dog/big_dog/g' my_file
如果将输出保存到输入文件本身,则在上述代码中附加
>临时文件和&mv临时文件输入文件

说明:


以下
awk
也可能对您有所帮助

# sed -i '2,3 s/dog/big_dog/g' my_file
如果将输出保存到输入文件本身,则在上述代码中附加
>临时文件和&mv临时文件输入文件

说明:


sed
第二次出现的替换为:

awk -v line=2 '             ##Creating an awk variable line whose value is 2 here.
$1=="dog" && ++count==line{ ##Checking condition if $1 is dog and increasing variable count value is equal to value of line(then do following)
  $1="big_dog"}             ##Assigning $1 to big_dog here.
1                           ##awk works on method of condition then action so by mentioning 1 I am making condition TRUE here so by default action print of line will happen.
'  Input_file               ##Mentioning Input_file here.

说明:

sed "/dog/ {n; :a; /dog/! {N; ba;}; s/dog/big_dog/; :b; n; $! bb}" your_file

请注意,在找到第二次出现后,需要最后3个命令来打印文件的其余部分,否则可能会对搜索模式的每一次偶数出现重复替换。

sed
对于替换,第二次出现是:

awk -v line=2 '             ##Creating an awk variable line whose value is 2 here.
$1=="dog" && ++count==line{ ##Checking condition if $1 is dog and increasing variable count value is equal to value of line(then do following)
  $1="big_dog"}             ##Assigning $1 to big_dog here.
1                           ##awk works on method of condition then action so by mentioning 1 I am making condition TRUE here so by default action print of line will happen.
'  Input_file               ##Mentioning Input_file here.

说明:

sed "/dog/ {n; :a; /dog/! {N; ba;}; s/dog/big_dog/; :b; n; $! bb}" your_file

请注意,在找到第二次出现后,需要最后3个命令来打印文件的其余部分,否则,可能会对搜索的图案的每一次偶数出现重复替换。

效果很好。您能给我指一下相关文档吗?,搜索第4节地址:选择行。哦,不。有没有办法使其不特定于任何行?@BishwasMishra,请尝试我的
awk
代码,我在其中放置了一个变量,该变量可能有发生次数,然后它将根据发生次数更改为该行,请告诉我这是否对您有帮助?另外,请总是在你的帖子中一次性提到你的所有要求。@RavinderSingh13也许我应该说声对不起。。。。我发表了关于这个答案的评论(只做了第二行)。然而,你认为我对你的评论发表了评论。。。在我读了你的评论后,我认为这是詹姆斯的回答。。。。所以你在这个答案下又解释了你的awk问题^_*工作得很好。你能给我指一下这方面的文档吗?搜索第4节地址:选择行。哦,不。有没有办法使这不特定于任何行?@BishwasMishra,试试我的
awk
代码,我在其中放了一个变量,该变量可能有事件编号,然后它将根据事件更改为该行,让我知道这对你有帮助吗?另外,请总是在你的帖子中一次性提到你的所有要求。@RavinderSingh13也许我应该说声对不起。。。。我发表了关于这个答案的评论(只做了第二行)。然而,你认为我对你的评论发表了评论。。。在我读了你的评论后,我认为这是詹姆斯的回答。。。。所以你在这个答案下又解释了你的awk问题^_*总是试着把你所有的要求放在一篇文章里。请始终尝试选择提供的任何答案作为正确答案,以关闭该线程,干杯。更新问题。第一件事您没有在代码标记中涵盖您的示例(请始终这样做)。第二,总是试着通过在各自的评论中告诉回答过的人他们的代码是否有效来对待他们,所以这只是为了指导而不是免费服务。希望这能有所帮助。“在代码标签中包含您的示例”是什么意思?、等。始终尝试将您的所有需求放在一篇文章中。请始终尝试选择提供的任何答案作为正确答案,以关闭该线程,干杯。更新问题。第一件事您没有在代码标记中涵盖您的示例(请始终这样做)。第二,总是试着通过在各自的评论中告诉回答过的人他们的代码是否有效来对待他们,所以这只是为了指导而不是免费服务。希望这有帮助。“在代码标记中覆盖您的示例”是什么意思?、等。希望它不是特定于行的。希望它不是特定于行的。