Awk search across multiple file fields with multiple lines

I have a file with entries that have the form:

SMS-MT-FSM-DEL-REP
country: IN
1280363645.979354_PFS_1_1887728354

SMS-MT-FSM-DEL-REP
country: IN
1280363645.729309_PFS_1_1084296392

SMS-MO-FSM
country: IR
1280105721.484103_PFM_1_1187616097

SMS-MO-FSM
country: MO
1280105721.461090_PFM_1_882824215

This can be parsed through awk using something like: awk 'BEGIN {FS = "\ n"; RS = ""} / country:. * MO / {print $ 0} '

My question is, how can I use awk to search for entries in 2 separate fields? For example, I just want to print the records in which the country of MO is, and the first record of the record is SMS-MO-FSM?

+5
source share
2 answers

if you set FS = "\ n" and RS = "", then the first field of $ 1 will be SMS-MO-FSM. Therefore your awk code

awk 'BEGIN{FS="\n"; RS=""} $2~/country.*MO/ && $1~/SMS-MO-FSM/ ' file
+4
source

( )

: OFS ORS, . $0 awk NF $0 $1 OFS $2 OFS ... $NF ORS. :

BEGIN {
    FS  = "\n"
    RS  = ""
    OFS = ";"     # Or another delimiter that does not appear in your data
    ORS = "\n"
}
$2 ~ /^[ \t]*country:[ \t]*MO[ \t]*$/ && $1 ~ /^[ \t]*SMS-MO-FSM[ \t]*$ {
    $1 = $1 ""    # This forces the reconstruction
    print
}
+3

All Articles