I wish to append " dddd" to the next line whenever I encounter "=" in a textfile.
This command
sed -i '/=/s|$| dddd|' *.krn
is close to what I am looking for as it appends to the current line where "=" is. How can I append to the next line instead?
Use append, see here:
E.g.:
$ echo $'abc\ndef\ne=f\nqqq'
abc
def
e=f
qqq
$ echo $'abc\ndef\ne=f\nqqq'|sed '/=/adddd'
abc
def
e=f
dddd
qqq
Edited to clarify as per comment from @je4d- if you want to append to what is present in the next line, you can use this:
$ echo $'abc\ndef\ne=f\nqqq\nyyy'
abc
def
e=f
qqq
yyy
$ echo $'abc\ndef\ne=f\nqqq\nyyy'|sed '/=/{n;s/$/ dddd/}'
abc
def
e=f
qqq dddd
yyy
See here for a great sed cheatsheet for more info if you want:
So to reiterate the question, when you match on one line, you want to append a string to the next line---a line that already exists, rather than adding a new line after it with the new data.
I think this will work for you:
sed '/=/ { N; s/$/ ddd/ }'
Say you have a file like:
=
hello
world
=
foo
bar
=
Then processing this command on it will yield:
=
hello ddd
world
=
foo ddd
bar
=
The trick here is using the N command first. This reads in the "next" line of input. Commands following it will be applied to the next line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With