Using Shell variables in awk

Hi All,
I have a file which I am reading and looking for "EXIT" statement. I want to insert ":JCWPROD" after each EXIT statement only if ":JCWPROD" doesn't exist.

Here is the sample file :

EXIT
Testing
EXIT
tesing123
EXIT

Desired file :

EXIT
:JCWPROD
Testing
EXIT
:JCWPROD
tesing123
EXIT
:JCWPROD

Code :

temp1=`awk '$0~"EXIT"{getline; print}' tst1`
awk -v var= "$temp1" '{ if(var != ":JCWPROD") {/EXIT/{print;print ":JCWPROD";next}1}' tst1 > tst2

The code above is not working for some reason.Can someone please help

$ cat infile
EXIT
Testing
EXIT
tesing123
EXIT
awk '/EXIT/{print $0 RS ":JCWPROD" ; next}1' infile

Resulting

EXIT
:JCWPROD
Testing
EXIT
:JCWPROD
tesing123
EXIT
:JCWPROD

Hello nua7,

Could you please try following and let me know if this helps.

 awk '($0 == "EXIT"){A=$0;getline;if($0 == "JCWPROD"){print A ORS $0} else if($0 != "JCWPROD" && $0 != "EXIT") {print A ORS ":JCWPROD" ORS $0} else{print A ORS ":JCWPROD"}}'  Input_file

Output will be as follows.

EXIT
:JCWPROD
Testing
EXIT
:JCWPROD
tesing123
EXIT
:JCWPROD

Also tested with a custom file where I have added an entry for word JCWPROD init and it looks to work for the same too.

cat test46
EXIT
JCWPROD
EXIT
Testing
EXIT
tesing123
EXIT

After running code output will be as follows.

EXIT
JCWPROD
EXIT
:JCWPROD
Testing
EXIT
:JCWPROD
tesing123
EXIT
:JCWPROD

Hope this helps.

Thanks,
R. Singh

1 Like

Hi Ravinder Singh,
It is deleting all the lines found before 1st EXIT statement.

Input file :

test
EXIT
Testing
EXIT
tesing123
EXIT
:JCWPROD

Output file , where first line is deleted

EXIT
:JCWPROD
Testing
EXIT
:JCWPROD
tesing123
EXIT
:JCWPROD

Try

awk -v var="$temp1" \
        'X && ($0!~var) {print var}
                        {X=0}
         /EXIT/         {X=1}
         1
         END            {if (X) print var}
        ' file

Hello nua7,

Sorry for same, I forgot to mention it will only look for string EXIT as per your input shown in POST#1, following may give you expected output, let me know if you have any queries on same.

awk '{if($0 == "EXIT"){A=$0;getline;if($0 == ":JCWPROD"){print A ORS $0} else if($0 != ":JCWPROD" && $0 != "EXIT") {print A ORS ":JCWPROD" ORS $0} else{print A ORS ":JCWPROD"}} else {print $0}}'   Input_file

Output will be as follows.

test
EXIT
:JCWPROD
Testing
EXIT
:JCWPROD
tesing123
EXIT
:JCWPROD

Hope this helps.

EDIT: Adding a non oneliner form of solution on same too.

awk '{
        if($0 == "EXIT"){
                                A=$0;
                                getline;
                                        if($0 == ":JCWPROD")                            {
                                                                                                print A ORS $0
                                                                                        }
                                        else if($0 != ":JCWPROD" && $0 != "EXIT")       {
                                                                                                print A ORS ":JCWPROD" ORS $0
                                                                                        }
                                        else                                            {
                                                                                                print A ORS ":JCWPROD"
                                                                                        }
                                                                                        }
        else            {
                                print $0
                        }
     }
    '  Input_file

Thanks,
R. Singh

1 Like

... or even

awk -v var="$temp1"\
        'X && ($0!~var) {print var}
                        {X=/EXIT/}
         1
         END            {if (X) print var}
        ' file

Seems simpler in sed:

sed '
  /^EXIT$/{
    ${
      s/$/\
:JCWPROD/
      b
     }
    N
    /\n:JCWPROD/b
    s/.*\n/&:JCWPROD\
/
   }
 '