sed replace return code

Hi

i have multiple sed replacements happening in my script and on those sed replacements, my requirement is to print a message if actually a replacement happened

for eg : if sed 's/abc/xyz/g' if [$replacement_return_code = 0 ]; then print "replacement happended from abc to xyz"

do we have any return codes like the above in sed to know if the actual replacement operation happened or not

please advise

Hi
You can get this in perl:

$ echo "abc efg" | perl -ne 'print s/abc/ABC/; '
1

Substitution in perl returns the number of substitutions made. If none, it will not throw any result.

Guru.

1 Like

thanks for the perl command, but
how to perform the below sed operations in perl and get return code

sed ''$start_replace_num',$s/^#[ ]*//g' // to replace all the "# " starting from nth line

sed 's/[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\} *$/'`date +"%Y-%m-%d"`'/g' // replace the date at the end of the line with the current date

Hi

You can do something like this in your shell:

$ cat a
welcome
to india

$ sed 's/to/TO/' a > b

$ diff a b >/dev/null

$ echo $?
1

I have a file "a". The output of sed command sent to file b. On doing diff between these files, I get return code non-zero if they are different, 0 if they are same. Hope this should do for you.

thanks ...this will work for me