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
grep '^Proto\|:7777 ' file.txt
? – heemayl Jun 18 '15 at 13:44