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 linesWhat is the best way of doing that ?Edit (example) :INPUT :Active Internet connections (only servers)Proto Recv-Q Send-Q Local Address           Foreign Address         Statetcp        0      0 188.165.65.214:53       0.0.0.0:*               LISTENtcp        0      0 127.0.0.2:53            0.0.0.0:*               LISTENtcp        0      0 127.0.0.1:53            0.0.0.0:*               LISTENtcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTENtcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTENtcp        0      0 127.0.0.1:953           0.0.0.0:*               LISTENtcp        0      0 0.0.0.0:7777            0.0.0.0:*               LISTENtcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTENtcp        0      0 127.0.0.1:587           0.0.0.0:*               LISTENtcp6       0      0 :::53                   :::*                    LISTENtcp6       0      0 :::22                   :::*                    LISTENtcp6       0      0 ::1:953                 :::*                    LISTENtcp6       0      0 :::443                  :::*                    LISTENOUTPUT :After asking to keep line 2 and pattern 7777 :Proto Recv-Q Send-Q Local Address           Foreign Address         Statetcp        0      0 0.0.0.0:7777            0.0.0.0:*               LISTEN
Sed to keep header(s) line(s) + matching lines
sed
christian@fujiu1404:~/tmp$ cat f1line 1 laaa bbb ccc ddd111 111 111 111 abc222 222 222 222333 333 333 333 abcchristian@fujiu1404:~/tmp$ sed -n -e '2p' -e '/abc/p' f1aaa bbb ccc ddd111 111 111 111 abc333 333 333 333 abcUpdateFor your input/outputsed -n -e '2p' -e '/:7777/p'