0

I have not found (nor been able to guess) a sed command that would allow me :

  • To keep : specific lines according to their index (the header for instance)
  • To keep : lines matching a pattern (like grep)
  • To delete : the other lines

What is the best way of doing that ?

Edit (example) :

INPUT :

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 188.165.65.214:53       0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.2:53            0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:53            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:953           0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:7777            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:587           0.0.0.0:*               LISTEN
tcp6       0      0 :::53                   :::*                    LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN
tcp6       0      0 ::1:953                 :::*                    LISTEN
tcp6       0      0 :::443                  :::*                    LISTEN

OUTPUT : After asking to keep line 2 and pattern 7777 :

Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:7777            0.0.0.0:*               LISTEN
  • Is the header the first line? – cuonglm Jun 18 '15 at 13:13
  • unfortunately not. Here it is the second line. I could take the two first lines if needed. – Maxx Jun 18 '15 at 13:18
  • It would help if you updated your question with a sample of your input file and desired output. – X Tian Jun 18 '15 at 13:28
  • Sure ! I just did – Maxx Jun 18 '15 at 13:37
  • grep '^Proto\|:7777 ' file.txt ? – heemayl Jun 18 '15 at 13:44
0
christian@fujiu1404:~/tmp$ cat f1
line 1 l
aaa bbb ccc ddd
111 111 111 111 abc
222 222 222 222
333 333 333 333 abc

christian@fujiu1404:~/tmp$ sed -n -e '2p' -e '/abc/p' f1
aaa bbb ccc ddd
111 111 111 111 abc
333 333 333 333 abc

Update

For your input/output

sed -n -e '2p' -e '/:7777/p'
  • Perfect ! Thank you very much. I'll keep that "-n" and "p" trick in mind ! – Maxx Jun 18 '15 at 13:54
2

You can do it easily with sed:

$ sed -e '2{
  p
  d
}
/7777/!d' file

or awk:

awk 'FNR==2 || /pattern/' file
  • 2 is the line number of header, you can change it to whatever line number you want.
  • pattern is the pattern you want to match.
  • I tested, and actually here it's a AND. (my 2nd line does not contain the pattern). I'd like a OR : I want to delete everything tha's not the second line or contains the pattern. In other words I'd like to keep the seconde line and every line matching the pattern. – Maxx Jun 18 '15 at 13:31
  • Sorry for the mis-reading, try the updated. – cuonglm Jun 18 '15 at 14:07

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.