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.

[/problem]

[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:

[/solution]

[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

[/example]

[reference]

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

[/reference]

If you have found my website useful, please consider buying me a coffee below 😉

Leave a Reply

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