1

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
6
  • Does the variable contain spaces for example is AMB RMAXUL allowed? Commented Aug 2, 2016 at 3:06
  • @shellter : You might have broken the format for expected output Commented Aug 2, 2016 at 3:33
  • hm... all I did was use the {} tool. If you think it is wrong, feel free to amend. Good luck to all. Commented Aug 2, 2016 at 3:41
  • 3
    Don't pipe grep to awk. echo $LINE | grep 'IMSI' | awk -F'=' '{print $2}' is better written echo $LINE | awk -F= '/IMSI/{print $2}' Commented Aug 2, 2016 at 3:53
  • Is the END tag on the second report really missing in your input? Commented Aug 2, 2016 at 3:54

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

So just to be explicit, awk '...' file | while read imsi isdn eps; do ... : things with "$imsi" and "$isdn" and "$eps"; done whill allow you to loop over the values. This should be easy to extend to add more values, and is way more efficient than repeatedly echoing the entire file (which to top it off you were doing without quoting, but that's a separate issue) as well as more maintainable and readable.
Also notice how I converted your variables to lowercase; uppercase is reserved for system variables.
@tripleee : Noted your first point. but I don't have any variables outside the awk-script here.
I meant that for the OP primarily. Sorry for being unclear.
Thanks Sir! this will helps a lot :)
1

Just use the shell:

#!/bin/bash

while read line
do
    case ${line%% *} in
        IMSI) IMSI=${line##* }
              ;;
        ISDN) ISDN=${line##* }
              ;;
        EPS) EPS=${line##* }
             ;;
        AMBRMAXUL) AMBRMAXUL=${line##* }
             ;;
        AMBRMAXDL) AMBRMAXDL=${line##* }
             ;;
        ---) echo $IMSI $ISDN $EPS $AMBRMAXUL $AMBRMAXDL
             unset IMSI ISDN EPS AMBRMAXUL AMBRMAXDL
             ;;
    esac
done < test.in

Let's test:

$ bash test.sh
515020211157018 632000000016 NOTPROV
515020211157020 632000000018 PROV 7104000 15200000

BTW, above expects every record to end with --- END, which seems to be the case with the first record but not the second.

2 Comments

Hi Sir, Thanks for your advise, assuming I will use this code. Can you confirm if the code below is advisable to add condition for value of AMBRMAXUL and AMBRMAXDL. > AMBRMAXUL) AMBRMAXUL=${line##* } > ;; > AMBRMAXDL) AMBRMAXDL=${line##* } > ;; > ---) if [ $EPS = "PROV" ]; then > echo $IMSI $ISDN $EPS $AMBRMAXUL $AMBRMAXDL > else > echo $IMSI $ISDN $EPS > fi > ;; Output: >515020211172557 632000005500 PROV 7104000 15200000 >515020211172560 632000005503 NOTPROV
That's how I would've done it, had I thought that far.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.