awk 打印到输出文件的顶部

awk print to the top of the output file

我有一个输入文本文件,里面有段落,由 3 个空行分隔。示例:

P1
P1
empty line here
empty line here
empty line here
P2
P2
empty line here
empty line here
empty line here
P3
P3
empty line here
empty line here
empty line here

目前我正在使用写入 *.awk 文件的这段代码来获取段落:

BEGIN{ORS=RS="\n\n\n"}
/some text pattern comes here because I dont want to print every paragraph just some of them but in reversed order/

所以我希望输出文件看起来像这样:

P3
P3
empty line here
empty line here
empty line here
P2
P2
empty line here
empty line here
empty line here
P1
P1
empty line here
empty line here
empty line here

所以我想知道是否可以将每个段落打印到输出文件的顶部以获得相反的顺序。可以吗?

tac inputfile | tail -n +4 | awk '{print};END{printf("\n\n\n")}'

这 (tac) 将反转输入文件的顺序,删除顶部(尾部)的空白,然后打印所有内容,但末尾有 3 个尾随换行符(因为 tac 消失了那些)。

如果您设置 RS="" 那么 awk 将用空行分隔 multi-line records

鉴于:

$ cat /tmp/so.txt
P1
P1



P2
P2



P3
P3

然后您可以获取每条记录的 [=16=] 然后反转该记录:

$ awk 'BEGIN{RS=""} {a[i++]=[=11=]} END {while(i--){ print a[i]; print "\n\n\n"}}' /tmp/so.txt
P3
P3




P2
P2




P1
P1

如果你有一个固定的三个空白行分隔符(并且你有gawk),你也可以这样做:

$ awk 'BEGIN{RS="\n\n\n"} {a[i++]=[=12=]} END {while(i--) print a[i]}' /tmp/so.txt

根据评论编辑

鉴于:

P1 a
P1 b

P2 a filter this block
P2 b

P3 a
P3 b

您可以添加一个模式来过滤不需要的块:

$ awk 'BEGIN{RS=""} /filter/ {next} {a[i++]=[=14=]} END {while(i--){ print a[i]; print "\n"}}' /tmp/so.txt
P3 a
P3 b


P1 a
P1 b

这对你有用吗?

cat -n inputfile | sort -r | grep -i 'pattern' | awk -F'\t' 'ORS="\n\n\n" {print }'

说明

cat -n inputfile           # number each line in the file
sort -r                    # sort in reverse order
grep -i 'pattern'          # grep out paragraphs with your text pattern
awk -F'\t' 'ORS="\n\n\n" {print }'
                           # awk out the numbers and print the second column

例如,如果您的输入文件是

Pz - The quick brown fox jumped over the lazy dog
Pz - The quick blue fox jumped over the lazy dog



Pa - The quick brown fox jumped over the lazy dog
Pa - The quick blue fox jumped over the lazy deer



Px - The quick brown fox jumped over the lazy cat
Px - The quick bronw fox jumped over the lazy dog

运行 下面用文本模式grep出段落 "blue"

cat -n inputfile | sort -r | grep -i 'blue' | awk -F'\t' 'ORS="\n\n\n" {print }'

会给你

Pa - The quick blue fox jumped over the lazy deer


Pz - The quick blue fox jumped over the lazy dog