I know there are quite a few questions asked on this topic. But I need help in a case basis. This is my scenario. I am having a file with the following string
TRACE(HELLO_WORLD, GEN, INFO, "Hello world")
TRACE(
HELLO_WORLD, GEN, INFO, "Hello World")
I want to match all the lines containing HELLO_WORLD
and in those lines I want to change TRACE
to TRACE_CLEANUP
in sed.
My sed command sed -i '/HELLO_WORLD/s/TRACE/TRACE_CLEANUP/g' abc.txt
matches the first case but not second.
Can you please help get the proper command.
Thanks
Chid
$ sed 'H;1h;$!d;x; s/TRACE\(([[:space:]]*HELLO_WORLD\)/TRACE_CLEANUP\1/g' file
TRACE_CLEANUP(HELLO_WORLD, GEN, INFO, "Hello world")
TRACE_CLEANUP(
HELLO_WORLD, GEN, INFO, "Hello World")
H;1h;$!d;x;
This reads the whole file in at once.
If your file were huge (too big for memory), we would want a different approach. If it is not huge, this approach is quite simple.
s/TRACE\\(([[:space:]]*HELLO_WORLD\\)/TRACE_CLEANUP\\1/g
This looks for TRACE(
followed by any whitespace followed by HELLO_WORLD
and substitutes in TRACE_CLEANUP
. The final g
means that this substitution is done "globally," meaning as many times as needed.
Perl solution:
perl -e 'undef $/;
$s = <>;
$s =~ s/TRACE(\(\s*HELLO_WORLD)/TRACE_CLEANUP$1/g;
print $s;
' input > output
undef $/
makes Perl read the whole input at once. s///
works similarly as in sed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.