0

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.

3
Contributors
3
Replies
15
Views
2 Years
Discussion Span
Last Post by changeworld4u
Featured Replies
  • 1

    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. Read More

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

1

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.

1

hi,

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.