Hi Team,

I have a file which contain following data like

BKP0000032183140217000019 053IGZYEVSDOX  .........field 2....... field3.....field4.....                                          
BKP0000032284140217000019CCTP1220  ...............
BKP0000032384140217000019CA    .....................

each line has 5 fields

Now using shell script i am trying to extract field 4 based on condition that line start with BKP and contain code 84 and CA or CC
code

    grep -A12 " RFND "  xfer_BSPDe | grep "^BKP" >file2.txt
    While read line
    do
     awk '{if((substr($1,12,2)==84) && (substr($1,26,2)=='CC' || substr($1,26,2)=='CA') ) print $4;else print "0"}'
     done<file2.txt

it is printing 0 only .
any help is appreciable.

Recommended Answers

All 3 Replies

You are using single quotes in the awk expression. That terminates the expression prematurely. Try using double quotes; something like:

awk '{
   if((substr($1,12,2)==84) && 
      (substr($1,26,2)=="CC" || substr($1,26,2)=="CA")) 
      print $4;
   else 
      print "0"
}'

Should work out for you.

hi,

And, I guess the while loop is useless, because awk reads files.
Here, it's the while loop that reads the file :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.