简体   繁体   English

UNIX:如何使用sed / awk / grep打印文件中的特定行?

[英]UNIX: How to print out specific lines in a file using sed/awk/grep?

I have a data in Unix which were fetched by a command and prints: 我在Unix中有一个通过命令获取并打印的数据:

line01
line02
line03
line04
line05
line06
line07
line08
line09
line10
line11
line12

and I wanted to sort it out such that the lines 10 to 12 are above lines 1 to 9. like this: 我想对它进行排序,使第10至12行在第1至9行上方,如下所示:

line10
line11
line12
line01
line02
line03
line04
line05
line06
line07
line08
line09

i tried using 我尝试使用

<command that fetches the data> | awk 'NR>=10 || NR<=9'

and

<command that fetches the data> | sed -n -e '4,5p' -e '1,3p'

but it still display in a sorted order. 但它仍按排序顺序显示。 i'm new to unix so i don't know how to properly use awk/sed. 我是unix的新手,所以我不知道如何正确使用awk / sed。

PS. PS。 These data are stored in a variable which will then be processed by another command. 这些数据存储在变量中,然后将由另一个命令处理。 so i needed it to be sorted that way so that line 10-12 will be processed first. 所以我需要将其排序,以便首先处理10-12行。 :) :)

Use head and tail : headtail

$ tail -n 2 file && head -n 3 file
name4
name5
name1
name2
name3

Your awk and sed approach do not work because you are just saying: print lines number X, Y and Z, and they will do so as soon as they find any of them. 您的awksed方法行不通,因为您只是在说:打印编号为X,Y和Z的行,一旦找到它们,它们就会立即执行。 If you wanted to use these tools, you would need to read the file first, storing its content, and then print it. 如果要使用这些工具,则需要先读取文件,存储其内容,然后再打印。

$ awk -v OFS="\n"  '{a[NR]=$0} END {print a[4], a[5], a[1], a[2], a[3]}' file
name4
name5
name1
name2
name3

Or even give the order as a variable: 甚至将订单作为变量:

awk -v order="4 5 1 2 3" 
      'BEGIN {split(order,lines)}
       {a[NR]=$0} 
       END {for (i=1;i<=length(lines);i++) print a[lines[i]]}' file

If you want to give the order of the lines as an argument, you can use process substitution saying awk '...' <(command) file and working with FNR/NR to distinguish between the input and the file 如果要将行的顺序作为参数给出,则可以使用流程替换awk '...' <(command) file然后使用FNR / NR来区分输入和文件

Or you can use - to read from stdin as first file: 或者,您可以使用-作为第一个文件从stdin读取:

echo "4 5 1 2 3" | awk 'FNR==NR {n=split($0,lines); next}
    {a[FNR]=$0}
    END {for (i=1;i<=n;i++) print a[lines[i]]}' - file

As one-liner: 作为单线:

$ echo "4 5 1 2 3" | awk 'FNR==NR {n=split($0,lines); next} {a[FNR]=$0} END {for (i=1;i<=n;i++) print a[lines[i]]}' - a

This might work for you (GNU sed): 这可能对您有用(GNU sed):

sed '1h;2,9H;1,9d;12G' file

Replace the hold space with line 1, then append lines 2 to 9 to the hold space and delete lines 1 thru 9. Print all other lines normally but on line 12 append the lines stored in the hold space to the pattern space. 将保留空间替换为第1行,然后将第2到9行添加到保留空间,并删除第1到9行。正常打印所有其他行,但在第12行上将存储在保留空间中的行追加到模式空间。

Using sort actually: 实际使用sort

$ sort -r -s -k 1.5,1.5 /tmp/lines 
line10
line11
line12
line01
line02
line03
line04
line05
line06
line07
line08
line09

The -k 1.5,1.5 means I'm using only 5th character of first word for sorting. -k 1.5,1.5表示我仅使用第一个单词的第5个字符进行排序。 -r means reverse order and -s means stable - leaving lines that have same 5th character in the same order. -r表示相反的顺序, -s表示稳定-使具有相同第5个字符的行以相同顺序排列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM