SED extract line from file

Problem

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



Solution

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

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


sed -n 456,466p filename



Example


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 Coding School[/tags]



Previous Post

Leave a Reply

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