1

This sed command delete lines that contain the word 'rocommunity':

sed -i '/rocommunity/d' snmpd.conf

rocommunity hedona 127.0.0.1
rocommunity hedona linux1 
rocommunity hedona linux12
rocommunity hedona linux13
rocommunity hedona linux15
rocommunity hedona linux16
rocommunity hedona linux17
syscontact linux 67
syslocation UNKNOWN
trapcommunity hedona
trapsink  linux76 parshedona

How to update my sed in order to delete lines that contain rocommunity, except the line with 127.0.0.1

expected output:

rocommunity hedona 127.0.0.1
syscontact linux 67
syslocation UNKNOWN
trapcommunity hedona
trapsink  linux76 parshedona
  • 1
    sed -i '/127\.0\.0\.1/b;/rocommunity/d' snmpd.conf – Costas Jan 23 '17 at 17:03
4

Simply combine two searches:

sed  '/rocommunity.*127\.0\.0\.1/p;/rocommunity/d' /path/to/input

/rocommunity.*127\.0\.0\.1/p - On a line with a match for rocommunity followed by a match for 127.0.0.1, print the line

/rocommunity/d - On a line with a match for rocommunity, do not print

Everything else will be printed.

To change file file once you've confirmed what you want is what is being done, you can use sed --in-place to make the changes to the file.

  • You're avoiding the "double negative" in my solution with that sed. Neat. – Kusalananda Jan 23 '17 at 17:18
  • Interestingly, that's what my first draft did, but it was bugging me for being too indirect. – DopeGhoti Jan 23 '17 at 17:22
2
$ sed -n -e '/rocommunity.*127.0.0.1/p' -e '/rocommunity/!p' data.in >data.out

The sed command first of all turns off the default printing of every line (-n) and then uses two editing commands (-e). The first one will print any line that contians the word rocommunity followed by 127.0.0.1 later on the line. The second editing command will print all lines not containing rocommunity.

With the data in the beginning of your question:

$ sed -n -e '/rocommunity.*127.0.0.1/p' -e '/rocommunity/!p' data.in
rocommunity hedona 127.0.0.1
syscontact linux 67
syslocation UNKNOWN
trapcommunity hedona
trapsink  linux76 parshedona
  • Amusingly, I revised my original answer to match yours before yours appeared. Great minds something something. – DopeGhoti Jan 23 '17 at 17:15
  • @DopeGhoti Ah. But there are minor differences still. It's a matter of taste whether one uses ; to separate editing commands (your way), or multiple -e (my way). – Kusalananda Jan 23 '17 at 17:16
  • Indeed. For more complex operations, I will opt to break them out, but for a simple thing like this, I tend to stitch them together. – DopeGhoti Jan 23 '17 at 17:21
1

if matched "rocommunity" and "127.0.0.1" then branch to end. and will print the line.

sed -e '/rocommunity{/127\.0\.0\.1/b' -e 'd;}' file

Which with the GNU implementation of sed, you can shorten to:

sed '/rocommunity/{/127\.0\.0\.1/b;d}' file
  • 1
    Explaining what this command does would significantly improve the quality of the answer. You can edit your post. – Michael Homer Jan 29 '17 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.