Robert Jhay Lumayag Robert Jhay Lumayag - 4 months ago 19
Bash Question

Unix Shell - Parsing file value into a variable

How can I assign a string to a variable like below:

Input content:

+++ USCDB 2016-07-29 20:19:53 PGW #036326 %%/*HWHandle=12547*/LST EPS:IMSI="515020211157018";%% RETCODE = 0 SUCCESS0001:Operation is successful

IMSI = 515020211157018
ISDN = 632000000016
EPS = NOTPROV
ANCHOR = FALSE
ICSIND = FALSE

Total count = 5

There is together 1 report

--- END
+++ USCDB 2016-07-29 20:19:53 PGW #036331 %%/*HWHandle=12547*/LST EPS:IMSI="515020211157020";%% RETCODE = 0
SUCCESS0001:Operation is successful

IMSI = 515020211157020
ISDN = 632000000018
EPS = PROV
AMBRMAXUL = 7104000
AMBRMAXDL = 15200000
RATFREQSELPRIID = 256
ANCHOR = FALSE
ICSIND = FALSE
MPS = FALSE
TAUTIMER = 0
MDT = NOTGIVEN
LTEAUTOPROV = FALSE
RELAY_NODE = FALSE
EPSODBPOS = NOBPOS
LTE_M2M_FLAG = FALSE

Total count = 15

There is together 1 report


While I'm currently using grep and awk to get the value above.

IMSI=$(echo $LINE | grep 'IMSI' | awk -F'=' '{print $2}')
ISDN=$(echo $LINE | grep 'ISDN' | awk -F'=' '{print $2}')
EPS=$(echo $LINE | grep 'EPS =' | awk -F'=' '{print $2}')

AMBRMAXUL=$(echo $LINE | grep 'AMBRMAXUL' | awk -F'=' '{print $2}')

AMBRMAXDL=$(echo $LINE | grep 'AMBRMAXDL' | awk -F'=' '{print $2}')


But when printing all IMSI value will be stored in one variable

Expected output:

515020211157018 632000000016 NOTPROV
515020211157020 632000000018 PROV 7104000 15200000

Answer

Assuming that the record contains AMBRMAXUL & AMBRMAXDL only when EPS = PROV, below should solve the problem:

awk '/^[[:blank:]]*IMSI[[:blank:]]+|^[[:blank:]]*ISDN[[:blank:]]+/{printf "%s ",$3}
      /^[[:blank:]]*EPS[[:blank:]]+/{if($3 == "PROV"){printf "%s",$3}else{print $3};check=0}
      /^[[:blank:]]*AMBRMAXUL[[:blank:]]+|^[[:blank:]]*AMBRMAXDL[[:blank:]]+/{check++;val=val" "$3}
       check == 2 {print val;val="";check=0}' file

will give you

515020211157018 632000000016 NOTPROV
515020211157020 632000000018 PROV 7104000 15200000

which is what you're looking for.

I have used print which lets me work around hard-coding \n (less portable) to the script.

Comments