Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select multiple lines using the linux command sed

Tags:

linux

shell

sed

awk

I have an example [file] that I want to Grab lines 3-6 and lines 11 - 13 then sort with a one line command and save it as 3_6-11_13. These are the commands I have used thus far but I haven't gotten the desired output:

sed -n '/3/,/6/p'/11/,/13/p file_1 > file_2 | sort -k 2 > file_2  & sed -n 3,6,11,13p file_1 > file_2 | sort -k 2 file_2.

Is there a better way to shorten this. I have thought about using awk but have I stayed with sed so far.

like image 277
demet8 Avatar asked Oct 02 '12 00:10

demet8


1 Answers

With sed you're allowed to specify addresses by number like so:

sed -n '3,6p'

The -n is to keep sed from automatically printing output.

Then you can run multiple commands if you're using gsed by separating those commands with semicolons:

sed -n '3,6p; 11,13p' | sort -k2 > 3_6-11_13
like image 147
Tim Pote Avatar answered Sep 18 '22 16:09

Tim Pote