So I have a simple use case, I have a file like
line_1
anything
anything
anything
aaa
line_2
line_3
anything
anything
anything
aaa
anything
anything
line_4
aaa
Now I have to replace the first aaa
which comes after line_3
.
Output-
line_1
anything
anything
anything
aaa
line_2
line_3
anything
anything
anything
YES REPLACE THIS
anything
anything
line_4
aaa
I was able to replace anything on the very next line using-
sed -i '/line_3/{n; s/aaa/YES REPLACE THIS/}' file.txt
But aaa
is not of the next line, I do not know after how many lines it occurs, any solution using sed
, awk
or anything bash related will work, any help will be appreciated.
CodePudding user response:
Using awk
awk '/line_3/{f=1} f&&/aaa/{$0="YES REPLACE THIS";f=0} 1' file
CodePudding user response:
Set a flag once it sees line_3
, unset it once it finds and replaces the pattern
perl -wpe'if ($on) { $on = 0 if s/aaa/XXX/ }; $on = 1 if /^line_3$/' file
This only prints the resulting lines to screen. If you want ot change the file ("in-place") add -i
switch (perl -i -wpe'...'
), or better -i.bak
to keep a backup, too.
CodePudding user response:
Using sed
$ sed '/line_3/,/line_4/{0,/^aaa/s/^aaa/YES REPLACE THIS/}' input_file
line_1
anything
anything
anything
aaa
line_2
line_3
anything
anything
anything
YES REPLACE THIS
anything
anything
line_4
aaa
CodePudding user response:
Using just replacement in perl
, let file.txt
content be
line_1
anything
anything
anything
aaa
line_2
line_3
anything
anything
anything
aaa
anything
anything
line_4
aaa
then
perl -p -0777 -e 's/(line_3.*?)aaa\n/$1/s' file.txt
gives output
line_1
anything
anything
anything
aaa
line_2
line_3
anything
anything
anything
anything
anything
line_4
aaa
Explanation: -p
and -e
means work like sed
, -0777
means shove whole file as single line. s/
- substitute, /s
- make .
also match newline character, I match line_3
and non-greedily any characters before aaa\n
and replace all of that with what I matched before aaa\n
denoted by (
and )
.
(tested in perl 5.30.0)
CodePudding user response:
ed
is good for automated editing of files:
ed -s file.txt <<<EOF
/line_3/;/aaa/,.c
YES REPLACE THIS
.
w
EOF
Finds the first line matching line_3
, then changes the first line after that matching aaa
, and finally writes the modified file back out.