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
6
  • Is the header the first line?
    – cuonglm
    Commented Jun 18, 2015 at 13:13
  • unfortunately not. Here it is the second line. I could take the two first lines if needed.
    – Maxx
    Commented Jun 18, 2015 at 13:18
  • It would help if you updated your question with a sample of your input file and desired output.
    – X Tian
    Commented Jun 18, 2015 at 13:28
  • Sure ! I just did
    – Maxx
    Commented Jun 18, 2015 at 13:37
  • grep '^Proto\|:7777 ' file.txt ?
    – heemayl
    Commented Jun 18, 2015 at 13:44

2 Answers 2

3

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.
2
  • 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
    Commented Jun 18, 2015 at 13:31
  • Sorry for the mis-reading, try the updated.
    – cuonglm
    Commented Jun 18, 2015 at 14:07
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'
1
  • Perfect ! Thank you very much. I'll keep that "-n" and "p" trick in mind !
    – Maxx
    Commented Jun 18, 2015 at 13:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .