Sed Editor single check

I have a sed command which will append a string on the end of a line. When I re-run the same command again the same content is getting append at the end of the line again and again.

I am looking for a command which will check if the content is already there or not then proceed.

Here is my sed command:

shell: sed -i '/only_from/s/$/ xx.xx.xx.xx\/24/' file.txt

Answers


this line works for your needs:

sed -i '/only_from/{/ xx\.xx\.xx\.xx\/24$/!s#$# xx.xx.xx.xx/24#}' file

E.g:

kent$  cat f
only_from foo bar

kent$  sed -i '/only_from/{/xx\.xx\.xx\.xx\/24$/!s#$# xx.xx.xx.xx/24#}' f

kent$  cat f
only_from foo bar  xx.xx.xx.xx/24

kent$  sed -i '/only_from/{/xx\.xx\.xx\.xx\/24$/!s#$# xx.xx.xx.xx/24#}' f

kent$  cat f
only_from foo bar  xx.xx.xx.xx/24

You can try this sed:

sed '/only_from/{ / xx\.xx\.xx\.xx\/24/ !s/$/ xx\.xx\.xx\.xx\/24/}' file

This might be a bit naive, but why don't you write something as simple as

sed -i '/only_from$/s/$/ xx.xx.xx.xx\/24/' file.txt

Need Your Help

What is wrong with multiple classes to elements for media query selectors? Or what is going on here?

html css

I've been trying for horus to figure out a problem with a webpage I made and I've narrowed down it down to being something to do with multiple classes and I've made a fiddle for proof.

android.os.TransactionTooLargeException thrown randomly

android parcelable

I am seeing quite a few error reports from one of my live apps, the caused is this exception: