Joel Dare

Using Awk and Sed on CSV or Text Files

Using Awk to Pull Matching Lines

You can use AWK to pull a specific set of lines from any text, CSV, html, or XML file. Use awk as a substitute for cat when you need to pull text from one specific line to another specific line.

Here’s the simple AWK command that you can use.

awk '/My Start String/,/My End String/' file.txt

That simple command will output the data in file.txt starting with the text “My Start String” and ending with the text “My End String”.

Using SED to Pull Specific Line Numbers

You can use SED to pull a specific range of lines, by line number, from any text file. Here’s the simple command.

sed -n <start>,<end>p <file>

And here is an example.

sed -n 1131,1152p sample.txt

That command pulls lines from 1131 to line 1152 from the sample.txt file.

See Also

Using AWK on CSV Files