简体   繁体   English

根据另一行用另一个替换字符串

[英]Replace string with another depending on another line

I need to write a script, that goes through a bunch of text files and replaces a certain string with another.我需要编写一个脚本,它遍历一堆文本文件并用另一个替换某个字符串。 The other one is located two lines below the certain string.另一个位于特定字符串下方的两行。 Like this:像这样:

some text
this is the first line (helloWorld).
this is the second line.
This is the third line (patternxxx).
more text

And I want this:我想要这个:

some text
this is the first line (helloxxxWorld).
this is the second line.
This is the third line (patternxxx).
more text

I'm under Linux.我在Linux下。

awk '
  BEGIN { mr=3 }
  /helloWorld/{ mr=0; gsub("helloWorld","helloxxxWorld");print; };
  mr==2 { gsub("pattern", "patternxxx");}
  mr++
'

test:测试:

$ cat file
some text
this is the first line (helloWorld).
this is the second line.
This is the third line (pattern).
more text
some text
this is the first line (helloWorld).
this is the second line.
this is the second line.
This is the third line (pattern).
more text

$ awk '
  BEGIN { mr=3 }
  /helloWorld/{ mr=0; gsub("helloWorld","helloxxxWorld");print; };
  mr==2 { gsub("pattern", "patternxxx");}
  mr++
' file
some text
this is the first line (helloxxxWorld).
this is the second line.
This is the third line (patternxxx).
more text
some text
this is the first line (helloxxxWorld).
this is the second line.
this is the second line.
This is the third line (pattern).
more text

You can achieve this using sed, see below command.您可以使用 sed 来实现这一点,请参见下面的命令。 this will permanently make changes in the files.这将永久更改文件。

sed -i 's/helloWorld/helloxxxWorld/g' file1 file2 ...filen

You can loop files one by one also.您也可以一个一个地循环文件。

 for file in `ls`;
 do
 sed -i 's/helloWorld/helloxxxWorld/g' $file;
 echo "$file completed";
 done

make sure you have backups for the files.确保您有文件的备份。

This might work for you (GNU sed):这可能对你有用(GNU sed):

sed -ri '1N;N;s/(hello)(World.*\n.*\n.*pattern(xxx))/\1\3\2/;P;D' file1 file2 ...

Open a three line window in the file and pattern match on the first and third lines throughout the file.在文件中打开一个三行窗口,并在整个文件的第一行和第三行进行模式匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果行以另一个字符串开头,则替换文件中的字符串 - Replace string in a file if line starts with another string Linux在一行中搜索一个字符串,但替换该行中的另一个字符串 - Linux search for a string in a line but replace another string in that line 如何在UNIX中用另一个字符串替换文本文件中行尾的字符串? - How to replace a string at the end of a line in a text file with another string in UNIX? 仅当下一行有另一个特定字符串时才替换字符串 - Replace a string only if the next line has another specific string 用另一个列表的下一行替换列表中的字符串 - Replace a string from a list with the next line from another list Linux用另一个字符串替换字符串 - linux replace string with another string Bash 如果存在另一行则替换行 - Bash replace line if another line exist Linux bash:如何根据另一行/不同行上的模式替换一行上的字符串? - Linux bash: How do I replace a string on a line based on a pattern on another/different line? Bash Scripting:如果行以(或匹配)另一个字符串开头,则替换(或删除)文件中的字符串 - Bash Scripting: Replace (or delete) string in a file if line starts with (or matches) another string 基于bash中的反向引用将字符串替换为另一个字符串 - Replace string with another string based on backreference in bash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM