Replace string and next one using sed - sed

I know how to replace sting via sed:
sed -i "s|.*#app-${BRANCH}-log.*| Path ${LOG_PATH} #app-${BRANCH}-log|" /etc/td-agent-bit/td-agent-bit.conf
and this is work for file like
[INPUT]
Name tail
Path /var/lib/docker/containers/f774c1a3689dfffb2528833ac2ded629c3b1873fd3af96fe0cf1f041f22f88d8/f774c1a3689dfffb2528833ac2ded629c3b1873fd3af96fe0cf1f041f22f88d8-json.log #app-develop-log
Tag app.develop
Interval_Sec 1
but how to replace next line? for example:
#This line just for triggering sed
And this one must be replaced
Any idea? Sorry for my horrible English.

With GNU sed:
sed '/^#This line just for triggering sed$/{n;s/.*/foo/}' file
Output:
#This line just for triggering sed
foo
See: man sed

Related

Sed: unescaped newline inside substitute pattern

I want to replace '-Djava.library.path=""' with '-Djava.library.path="path"' in file named "file" using sed
sed -i 's/-Djava.library.path=""/-Djava.library.path="path"/g' -f file
returns
sed: 2: file: unescaped newline inside substitute pattern
What's wrong? Can someone help?
Changed to
sed -i 's/Djava.library.path=""/Djava.library.path="path"/' file
Now it works.

Add string to file at certain line number

I want to add a string to file at certain line number in Linux. I searched and found a command like:
sed "5i helloworld" test.txt
to add helloworld at line 5, but I got an error:
sed: command garbled.
I am testing in RedHat here. Is there any other command I can use here? Any other ways?
Older seds are a bit pickier with how you type commands like i, a and c. Try an actual line continuation:
sed '5i\
helloworld' test.txt
The i text syntax is a GNU extension. POSIX sed only know about the i\ version with linebreak.
Also, notice that there is a difference between the sed i command1 (insert text) and the -i option (in-place editing).
1 Or "function".
Here is awk solution:
awk 'NR==5{1;print "Hey there this is new text added on line 5"}1' inputfile

Sed find and replace Cpp file(.C)

I am having issues of editing huge C++ file where I am using sed to convert List(something) to List<something> why I am doing this because our List has been converted to template.
Command I have written in small shell file is like this
sed -i '/List/s/(/</g' $1
sed -i '/List/s/)/>/g' $1
But this command is converting the whole line associated with List to angular braces like,
some_Fun(List(something)) to some_Fun<List<something>>
I don't want sed to change some_Fun<> , sed should keep some_Fun() and change only List() to List<>.
You can use this sed:
sed 's/\(List\)(\([^)]*\))/\1<\2>/g' file
(OR)
sed 's/List(\([^)]*\))/List<\1>/g' file

Sed: can't define the pattern correctly, can you please assist?

I'm trying to add "ARG1$" to the end of this line:
command[check_net_speed]=/usr/lib64/nagios/plugins/check_net_speed.sh $
I've tried:
sed -e 's/^command\[check_net_speed\]$/$ARG1$/g' /etc/nagios/nrpe.cfg
sed -e 's/.*speed.*/$ARG1$/g' /etc/nagios/nrpe.cfg
But none did the trick... what's the right way to catch the pattern of the "check_net_speed" command and add "ARG1$" at the end of the line, so the line will look like this:
command[check_net_speed]=/usr/lib64/nagios/plugins/check_net_speed.sh $ARG1$
Something like
sed -e 's/^command\[check_net_speed\].*/&ARG1$/g' input
command[check_net_speed]=/usr/lib64/nagios/plugins/check_net_speed.sh $ARG1$
Change your sed command like below,
sed -e '/^command\[check_net_speed\]/s~$~ARG1$~' file
/^command\[check_net_speed\]/ matches the lines which starts with command[check_net_speed] and it do the replacement on those matched lines.
$ in the regex part means end of the line. So the above command replaces the end of the line anchor with ARG1$
Example:
$ echo 'command[check_net_speed]=/usr/lib64/nagios/plugins/check_net_speed.sh $' | sed -e '/^command\[check_net_speed\]/s~$~ARG1$~'
command[check_net_speed]=/usr/lib64/nagios/plugins/check_net_speed.sh $ARG1$
$ has a special meaning: end of line. To treat it as a literal, you have to escape it:
sed '/^command\[check_net_speed\].*\$/s/$/ARG1$/' file
This will replace the end of line (indicated by $ alone) with the string ARG1$. So at the end, ARG1$ will be appended to the line.
The /command/ part is used to perform this replacement only in the lines containing the string command.
Test
$ cat a
command[check_net_speed]=/usr/lib64/nagios/plugins/check_net_speed.sh $
ddd
$ sed '/^command\[check_net_speed\].*\$/s/$/ARG1$/' a
command[check_net_speed]=/usr/lib64/nagios/plugins/check_net_speed.sh $ARG1$
ddd
As a supplement to nu11p01n73R's answer. Use
sed -e 's/^command\[check_net_speed\].*\$$/&ARG1$/g;q' /etc/nagios/nrpe.cfg
;q after substitution command means stop processing the rest of this file after first match.

Replace pattern in an existing line using sed

I want to replace the
/fdasatavol/ankit
to
/fdasatavol_sata/ankit
Can anyone help me out in this?
to write to a new file (without modifying file1):
sed 's/fdasatavol/fdasatavol_sata/g' file1 > file2
or to replace in the original file:
sed -i 's/fdasatavol/fdasatavol_sata/g' file1
This will replace each occurrence of fdasatavol with fdasatavol_sata:
sed 's/fdasatavol/&_sata/g'
If your input has occurrence of fdasatavol that are not in /fdasatavol/ankit and you don't want to substitute these then use:
sed 's#/fdasatavol/ankit#/fdastatavol_sata/ankit#g'
Note: you can use any character as sed's delimilter to aviod the confusion with the parrtern contiaing /. sed prints to stdout by default, if you are happy with the changes produced by sed you can use the -i option to store back to the file.
sed -i 's/fdasatavol/&_stat/g' file