使用linux命令sed选择多行

时间:2022-07-20 04:24:41

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:

我有一个示例[文件],我想抓住第3-6行和第11 - 13行,然后用一行命令排序并保存为3_6-11_13。这些是我到目前为止使用的命令,但我没有得到所需的输出:

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.

有没有更好的方法来缩短这一点。我曾经考虑过使用awk,但到目前为止我一直在使用sed。

2 个解决方案

#1


13  

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

使用sed,您可以按数字指定地址,如下所示:

sed -n '3,6p'

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

-n是为了防止sed自动打印输出。

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

然后,如果您通过使用分号分隔这些命令来使用gsed,则可以运行多个命令:

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

#2


1  

sed combine multiple commands using -e option

sed使用-e选项组合多个命令

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

or you can separate commands using the semicolon

或者您可以使用分号分隔命令

$ sed 'comm;comm;comm' file.txt

#1


13  

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

使用sed,您可以按数字指定地址,如下所示:

sed -n '3,6p'

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

-n是为了防止sed自动打印输出。

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

然后,如果您通过使用分号分隔这些命令来使用gsed,则可以运行多个命令:

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

#2


1  

sed combine multiple commands using -e option

sed使用-e选项组合多个命令

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

or you can separate commands using the semicolon

或者您可以使用分号分隔命令

$ sed 'comm;comm;comm' file.txt