python - Print the consecutive 3 line at the pattern match -


i have huge file need sort , merge 3 line having pattern match "vm" , need them 1 single line, below

kfg-ap4 server name , can have once nice while sorting .. tried awk getline somehow missing fit ..

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: trust have received usual lecture local system [kfg-ap4] out: administrator. boils down these 3 things: [kfg-ap4] out: [kfg-ap4] out:     #1) respect privacy of others. [kfg-ap4] out:     #2) think before type. [kfg-ap4] out:     #3) 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: 

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

actual data below , rest of data same except server names

[kfg-ap3] executing task 'moc' [kfg-ap3] sudo: /sbin/sysctl -a | grep overcommit [kfg-ap3] out: [kfg-ap3] out: trust have received usual lecture local system [kfg-ap3] out: administrator. boils down these 3 things: [kfg-ap3] out: [kfg-ap3] out:     #1) respect privacy of others. [kfg-ap3] out:     #2) think before type. [kfg-ap3] out:     #3) 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: trust have received usual lecture local system [kfg-ap4] out: administrator. boils down these 3 things: [kfg-ap4] out: [kfg-ap4] out:     #1) respect privacy of others. [kfg-ap4] out:     #2) think before type. [kfg-ap4] out:     #3) 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: 

$ 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 

walk-thru:

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

since there in fact more 3 rows in group, use this:

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

it buffers records , prints them out after encounters record doesn't match /vm/. may case problem if have consecutive groups in file. may end-up longer-than expected lines.

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

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -