For loop, awk command issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For loop, awk command issue
# 1  
Old 04-24-2013
For loop, awk command issue

Code:
 
limit.csv data
--------------
5600050 38Nhava
400077 27Bomay

Code:
 
rate.txt data
-------------
38NhaVA
27BomaY
27Bomay

below is my script:

Code:
 
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
# 2  
Old 04-24-2013
Maybe if you included the error messages and tell us what shell you are using , it could help us...
# 3  
Old 04-24-2013
Below are the errors im getting
Code:
 
 
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
# 4  
Old 04-24-2013
Quote:
Originally Posted by p_satyambabu
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
Code:
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:
Code:
#!/bin/sh -xv


Last edited by Yoda; 04-24-2013 at 12:21 PM..
# 5  
Old 04-24-2013
Replace the for loop and list from `subshell` by a while loop - simple&safe
Code:
while read b a
do
...
done < limit.csv

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Issue with cat command on a for loop

Good day to all, I'd like to ask for your advice with regards to this. Scenario : I have here a file named TEST.tmp wherein the value inside is below; "ONE|TWO|FIVE|THREE|FOUR|SIX~SEVEN~EIGHT" "NINE" But when I'm trying to use this in a simple command like; for TESTING in $(cat... (4 Replies)
Discussion started by: asdfghjkl
4 Replies

2. Shell Programming and Scripting

awk command with a loop

Dear all, I would be grateful for your help with the following. I have the following file (file.txt), which is about 10,000 lines long: ID1 ID2 0 1 0.5 0.6 ID3 ID4 0 0 0.4 0.8 ID1 ID5 0 1 0.5 0.3 ID6 ID2 1 0 0.4 0.8 The IDs in the first two columns can occur between... (12 Replies)
Discussion started by: aberg
12 Replies

3. UNIX for Beginners Questions & Answers

Issue with awk command

I am new to unix.I have a requirement to get few fields from the log file as below. Log app9/cc-gr_base.log.2017-07-19.gz: hostname 1500523166993 NA:NA:NA http-nio-8080-exec-56 INFO Points balance from MIS for user with userId: 19651069, first name: DEREK RICHARD and last name: BOUDREAU is... (3 Replies)
Discussion started by: nextStep
3 Replies

4. Shell Programming and Scripting

Issue in awk parsing under while loop

Hi I am trying to parse a grep output using awk. It works fine individually and not working under the loop with variable name assigned. cat > file.txt dict=/dictr/abcd/d1/wq:/dictr/abcd/d2/wq:/dictr/abcd/d3/wq: sample tried code Nos=`grep -w "dict" file.txt | awk -F"=" '{print... (10 Replies)
Discussion started by: ananan
10 Replies

5. Shell Programming and Scripting

awk - 2 files comparison without for loop - multi-line issue

Greetings Experts, I need to handle the views created over monthly retention tables for which every new table in YYYYMMDD format, there is equivalent view created and the older table which might be dropped, the view over it has to be re-created over a dummy table so that it doesn't fail.... (2 Replies)
Discussion started by: chill3chee
2 Replies

6. Shell Programming and Scripting

Variable value substitution issue with awk command issue

Hi All, I am using the below script which has awk command, but it is not returing the expected result. can some pls help me to correct the command. The below script sample.ksh should give the result if the value of last 4 digits in the variable NM matches with the variable value DAT. The... (7 Replies)
Discussion started by: G.K.K
7 Replies

7. Shell Programming and Scripting

awk loop issue

Hi I am not able to solve this minor issue even after a lot of trial! Will be thankful if you can help me out. This is part of a awk script and the loop is self-explanatory - num_null_key=0 #for counting the number of null keys num_non_null_key=0 #for counting the number of non null... (2 Replies)
Discussion started by: nsinha
2 Replies

8. Shell Programming and Scripting

While loop with awk issue

Hi folks. I am trying to use a while loop along with awk to get the colums where 2 specific words are found. Here is the output of the command i run where i need to pull the column numbers if specific words are found: State Type Rebal Sector Block AU Total_MB Free_MB ... (3 Replies)
Discussion started by: Stephan
3 Replies

9. Shell Programming and Scripting

awk command issue

Hi, Please could someone advise the issue i have with my awk command ? my command is : export NUM_SCENARIOS=`awk -F= '!/^#/ && /NUM_SCENARIOS/{print $2}' /home/environment.properties` when I echo $NUM_SCENARIOS this comes back with : 100 10 The issue I have is, there is... (9 Replies)
Discussion started by: venhart
9 Replies

10. UNIX for Dummies Questions & Answers

Awk command in while loop

Hello! I've got a loop in which I am processing a list of values gotten through a file with read command. It seems that instead of processing the lines (values) one by one, I process them all together. the input file is: 20 20 20 80 70 70 20 The code is: (2 Replies)
Discussion started by: haaru
2 Replies
Login or Register to Ask a Question