awk打印到输出文件的顶部

时间:2016-01-10 23:26:48

标签: awk printing reverse text-processing gawk

我有一个带有段落的输入文本文件,由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

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

3 个答案:

答案 0 :(得分:2)

如果您设置RS="",则awk会将multi-line records分隔为空行。

假设:

$ cat /tmp/so.txt
P1
P1



P2
P2



P3
P3

然后,您可以抓取每个记录的$0,然后反转该记录:

$ awk 'BEGIN{RS=""} {a[i++]=$0} 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++]=$0} 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++]=$0} END {while(i--){ print a[i]; print "\n"}}' /tmp/so.txt
P3 a
P3 b


P1 a
P1 b

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

这对你有用吗?

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

解释

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 $2}'
                           # 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

运行以下内容以使用文本模式"蓝色"

来显示段落
cat -n inputfile | sort -r | grep -i 'blue' | awk -F'\t' 'ORS="\n\n\n" {print $2}'

会给你

Pa - The quick blue fox jumped over the lazy deer


Pz - The quick blue fox jumped over the lazy dog