在模式匹配时打印连续的3行

时间:2016-08-08 08:08:25

标签: python awk sed

我有一个巨大的文件,我需要排序和合并3行,它们具有模式匹配“vm”,需要它们进入单行,如下

kfg-ap4是服务器名称,我们可以拥有它只有一次在排序时会很好.. 我用getline尝试了awk但不知何故我不适合它..

awk'/ vm / {printf $ 0“”; getline; print $ 0}'mem_overc

**[kfg-ap4] out: vm.overcommit_memory = 0 [kfg-ap4] out: vm.overcommit_ratio = 50 [kfg-ap4] out: vm.nr_overcommit_hugepages = 0**


[kfg-ap4] Executing task 'moc'
[kfg-ap4] sudo: /sbin/sysctl -A | grep overcommit
[kfg-ap4] out:
[kfg-ap4] out: We trust you have received the usual lecture from the local System
[kfg-ap4] out: Administrator. It usually boils down to these three things:
[kfg-ap4] out:
[kfg-ap4] out:     #1) Respect the privacy of others.
[kfg-ap4] out:     #2) Think before you type.
[kfg-ap4] out:     #3) With great power comes great responsibility.
[kfg-ap4] out:
[kfg-ap4] out: sudo password:
[kfg-ap4] out: vm.overcommit_memory = 0
[kfg-ap4] out: vm.overcommit_ratio = 50
[kfg-ap4] out: vm.nr_overcommit_hugepages = 0
[kfg-ap4] out:

=============================================== =======================

实际数据如下,除服务器名称

外,其余数据相同
[kfg-ap3] Executing task 'moc'
[kfg-ap3] sudo: /sbin/sysctl -A | grep overcommit
[kfg-ap3] out:
[kfg-ap3] out: We trust you have received the usual lecture from the local System
[kfg-ap3] out: Administrator. It usually boils down to these three things:
[kfg-ap3] out:
[kfg-ap3] out:     #1) Respect the privacy of others.
[kfg-ap3] out:     #2) Think before you type.
[kfg-ap3] out:     #3) With great power comes great responsibility.
[kfg-ap3] out:
[kfg-ap3] out: sudo password:
[kfg-ap3] out: vm.overcommit_memory = 0
[kfg-ap3] out: vm.overcommit_ratio = 50
[kfg-ap3] out: vm.nr_overcommit_hugepages = 0
[kfg-ap3] out:

[kfg-ap4] Executing task 'moc'
[kfg-ap4] sudo: /sbin/sysctl -A | grep overcommit
[kfg-ap4] out:
[kfg-ap4] out: We trust you have received the usual lecture from the local System
[kfg-ap4] out: Administrator. It usually boils down to these three things:
[kfg-ap4] out:
[kfg-ap4] out:     #1) Respect the privacy of others.
[kfg-ap4] out:     #2) Think before you type.
[kfg-ap4] out:     #3) With great power comes great responsibility.
[kfg-ap4] out:
[kfg-ap4] out: sudo password:
[kfg-ap4] out: vm.overcommit_memory = 0
[kfg-ap4] out: vm.overcommit_ratio = 50
[kfg-ap4] out: vm.nr_overcommit_hugepages = 0
[kfg-ap4] out:

3 个答案:

答案 0 :(得分:1)

你几乎就在那里:只是积累一个变量并在最后打印:

awk 'BEGIN{s="";} /vm/ {s = s $0 " "} END {print s}' log.txt

您还可以使用确切的构造并转换换行符:

awk '/vm/ {printf $0 " ";getline; print $0}' log.txt | tr "\n" " "

答案 1 :(得分:0)

$ awk '/vm/ {printf "%s%s", $0, (++i%3?OFS:ORS)}' log.txt
[kfg-ap4] out: vm.overcommit_memory = 0 [kfg-ap4] out: vm.overcommit_ratio = 50 [kfg-ap4] out: vm.nr_overcommit_hugepages = 0
[kfg-ap4] out: vm.overcommit_memory = 0 [kfg-ap4] out: vm.overcommit_ratio = 50 [kfg-ap4] out: vm.nr_overcommit_hugepages = 0

步行得来速:

/vm/ {                                 # if vm on the record
    printf "%s%s", $0, (++i%3?OFS:ORS) # print record and OFS
}                                      # every 3rd time print ORS

因为实际上组中有3行,所以请使用:

$ awk '/vm/ {buf=buf OFS $0; next} buf!="" {print buf; buf=""}'

它会在遇到不匹配/vm/的记录后缓冲记录并将其打印出来。如果您的文件中有连续的组,则可能会出现问题。最终可能会出现比预期更长的行。

$ cat > process.awk
/vm/ {              # if vm string in the record
    buf=buf OFS $0  # append the record $0 to the buffer variable buf
    next            # skip the rest of script for this record, ie.
}                   # only records without vm match get past this point
buf!="" {           # if the buffer is not empty
    print buf       # print it out
    buf=""          # empty the buffer
}
$ awk -f process.awk log.txt

答案 2 :(得分:0)

仅使用sed

的另一个答案
/vm/{
H
x
s/\n/ /g
s/ \[kfg-ap.\] out: / /
x
blab
}
x
p
:lab

像这样调用(将上面的文件保存为filter.sed):

sed -n -f filter.sed text.txt 

如果vm匹配,则存储在保持缓冲区中。然后交换整个缓冲区以删除行中的换行符和无用的kfg前缀,再次交换。

如果不匹配,请再次交换缓冲区并打印。

相当简单&工作得很好(如果日志以vm行结束,则可以省略它们)