extract specific line number from file using sed

Problem

You want to extract a specific line number from a file.

Or a range of numbers from a file.



Solution

Once you have your line number – see grep, we can extract lines around the pattern.

To do this sed (stream editor) can be used to print just desired lines – see the example which says don’t print all lines (-n); start at line 456 and finish at line 466 – print:



Example


This says don't print all lines (-n); start at line 456 and finish at line 466 – print:


sed -n 456,466p filename

Also with sed, we can say delete specific lines – in this case remove lines 5 to 10:


sed 5,10d filename

That's not all sed can accept patterns, as start/end identifiers:


sed /start_pattern/,/end_pattern/d filename



Reference

[tags]UNIX sed, UNIX, sed, UNIX Coding School[/tags]



Leave a Reply

Your email address will not be published. Required fields are marked *