How to display 5 lines immediately before a line in a config file using tail and head

Issue

So I want to display 5 lines immediately before a line that says # Step #6: Configure output plugins in the snort.conf file.

so I use the following to get the line number:

nl /etc/snort/snort.conf | grep output

and the line number is 445

the book then advises to use the following:

tail -n+445 /etc/snort/snort.conf | head -n 6

but this doesn’t work and does not display the 5 line before line 445

Solution

tail -n +445 outputs lines starting from 445. head -n 6 shows the first 6 lines of output.

Together, they show lines 445-451.

If you instead want to show 439-445, you have to adjust accordingly:

tail -n +439 /etc/snort/snort.conf  | head -n 6

Answered By – that other guy

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published