Select multiple lines using linux sed command

I have an example [file] that I want to capture lines 3-6 and lines 11-13, then sort with a single line and save it as 3_6-11_13. These are the commands that I have used so far, but I did not get the desired result:

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 cut this. I was thinking about using awk but still stayed with sed.

+6
source share
2 answers

With sed you are allowed to specify addresses by number as follows:

 sed -n '3,6p' 

-n should save sed from automatically printing output.

Then you can run several commands if you use gsed , dividing these commands into semicolons:

 sed -n '3,6p; 11,13p' | sort -k2 > 3_6-11_13 
+11
source

sed combines multiple commands using the -e option

 $ sed -e 'comm' -e 'comm' file.txt 

or you can separate the commands with a semicolon

 $ sed 'comm;comm;comm' file.txt 
+1
source

Source: https://habr.com/ru/post/926714/


All Articles