For loop, awk command issue

 
limit.csv data
--------------
5600050 38Nhava
400077 27Bomay
 
rate.txt data
-------------
38NhaVA
27BomaY
27Bomay

below is my script:

 
for i in `cat limit.csv`
do
        b=`awk '{print $1}' $i`
        a=`awk '{print $2}' $i`
        grep -x "$a" rate.txt > /dev/null 2>&1
        if [ $? -eq 0 ]
        then
                rate=`grep -x "$a" rate.txt`
        else
                grep -i "$a" rate.txt > /dev/null 2>&1
                if [ $? -eq 0 ]
                then
                        rate=`grep -i "$a" rate.txt`
                        if [ `cat $rate | wc -l` > 1 ]
                        then
                                echo "There are duplicates for Rate Zone Codes $rate"
                                exit 1
                        fi
                fi
        fi
echo "$b,\"$rate\"" >> output.csv
done
 

can someone tell me why it showing errors and how to avoid them

Maybe if you included the error messages and tell us what shell you are using , it could help us...

Below are the errors im getting

 
 
awk: Cannot find or open file 5600050.
 The source line number is 1.
awk: Cannot find or open file 5600050.
 The source line number is 1.
cat: cannot open 38NhaVA
cat: cannot open 27BomaY
cat: cannot open 27Bomay

i dont want these messagee to be displayed once i execute the above script. my shell is .sh

Have you even given any thought as to why you are getting these messages?

The error messages clearly states that you are passing the file content rather than the file name as argument to awk & cat

awk: Cannot find or open file 5600050.
 The source line number is 1.
cat: cannot open 38NhaVA

No offence, but your script is poorly written. I suggest you to set xtrace & verbose and debug your script:

#!/bin/sh -xv

Replace the for loop and list from `subshell` by a while loop - simple&safe

while read b a
do
...
done < limit.csv
1 Like