在图案匹配处打印连续的3行

2024-09-29 22:05:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个巨大的文件,我需要排序和合并3行有模式匹配“vm”,并需要他们到一个单一的行,如下所示

kfg-ap4是服务器名,我们只能有一次,这将是很好的排序时。。 我尝试了awk与getline,但不知何故,我错过了适合它。。你知道吗

awk'/vm/{printf$0”“;getline;print$0}'内存溢出

**[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:

Tags: theyousudovmoutnrmemoryratio
3条回答
$ 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

另一个答案只使用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前缀,再次交换。你知道吗

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

相当简单&效果很好(cornercase:如果log以vm行结尾,则可以省略)

你几乎做到了:只需在变量中累积,最后打印:

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

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

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

相关问题 更多 >

    热门问题