Issue with for loop, help me with a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue with for loop, help me with a script
# 1  
Old 04-30-2013
Issue with for loop, help me with a script

Hi, i have to compare the second column of the each line of the file1.csv with the data in file2.csv file and replace if a match is found.
below is data. Can someone please help me with a script.
Quote:
file1.csv
----------
34,BANDHI
45,Ramonih
789,BALAGAH
921,Revathi
Quote:
file2.csv
----------
Bandhi
balagah
Vadanga
SRAVAH
RAMONIH
REVATHI
so output file should lokk like below
Quote:
output.csv
---------
34,Bandhi
45,RAMONIH
789,balagah
921,REVATHI
# 2  
Old 04-30-2013
what is your loop doing??

Can you post the snippet here??
# 3  
Old 04-30-2013
Here is my code, its not generating output as mentioned above

Code:
 
file=file1.csv
        if test -s "$file"
        then
                cat $file|while read line
                do
                        a=`cat $line | awk '{print $1}' `
                        b=`cat $line | awk '{print $2}' `
                        rate=`grep -i "$b" file2.csv`
                        echo "$a, \"$rate\"" >> output.csv
                done
        else
                echo "no need of correction."
        exit 0
        fi

# 4  
Old 04-30-2013
Note that values for a and b will be the same
Code:
a=`cat $line | awk '{print $1}' `
b=`cat $line | awk '{print $2}' `

the above code will get you the values as a=34,BANDHI and b=<nothing>

awk's default field separator is <space>. In your case, its comma. so try setting FS as "," Smilie

Code:
a=`cat $line | awk -F"," '{print $1}' `
b=`cat $line | awk -F"," '{print $2}' `

# 5  
Old 04-30-2013
HI, i have replaced the commands for a,b as mentioned by you,

but still no luck, not able to getting the output in the required format Smilie
# 6  
Old 04-30-2013
Code:
file=file1.csv
        if test -s "$file"
        then
                while read line
                do
                        a=$(echo $line | awk -F"," '{print $1}' )
                        b=$(echo $line | awk -F"," '{print $2}' )
                        rate=$(grep -i "$b" file2.csv)
                        echo "$a, \"$rate\"" >> output.csv
                done < $file
        else
                echo "no need of correction."
        exit 0
        fi

I have made few changes to your code which I didn't notice last time. Moreover there is a possibility that the rate=$(grep may not find an entry in file2.csv. In that case you might need to do a test here as well.

Hope this helps Smilie
This User Gave Thanks to PikK45 For This Post:
# 7  
Old 04-30-2013
You might also try this simple awk script:
Code:
awk '
BEGIN { FS = OFS = "," }
FNR == NR {
        # Set up translation table indexed by string converted to lower case.
        t[tolower($1)] = $1
        next
}
{       if((l = tolower($2)) in t)
                # We found a case insensitive match, make the translation.
                $2 = t[l]
        print   # Print the original or updated record.
}' file2.csv file1.csv . output.csv

If you're using a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk.

PS When posting code and file samples, please use CODE tags instead of QUOTE tags.
This User Gave Thanks to Don Cragun 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 when doing a loop

Hi, I just have started learning shell scripting (sh). Why do i only get the date? while read dt do echo "Date : ${dt} sed -n '/${dt}/,/${dt}/p' file1.log | grep -w ERROR done < date1.dat INPUT - date1.dat 2019-04-05 04:58:25 2019-04-05 04:58:26 2019-04-05 05:00:56... (3 Replies)
Discussion started by: margel
3 Replies

2. Shell Programming and Scripting

Issue with for loop

Hi Team, I have for loop in my shell script. Which basically loop through all files in the directory, When some files are in the directory it works just fine. But if there are no files at all..still the for loop try to execute. Please help. Below is the code. #!/bin/ksh echo "Program... (5 Replies)
Discussion started by: bharath561989
5 Replies

3. Shell Programming and Scripting

Issue with while loop?

Hi, I have prepared a script to search for backup file information on the Linux server. Script works fine for the most part except the echo statement inside an IF conditional block displays the message ''snapshot directory not found on xxxxx" even though the .snapshot directory is found a... (11 Replies)
Discussion started by: svajhala
11 Replies

4. Homework & Coursework Questions

Bash Script for Dice Game; Issue with if...else loop to verify user guess is within range

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have written a script for a dice game that: (1) tells user that each of the 2 die are 6 sided (Spots=6); (2)... (3 Replies)
Discussion started by: LaurenRose
3 Replies

5. Shell Programming and Scripting

While Loop issue

Hi, i=0 t5=6000001 while do i=`expr $i + 1` t5=`expr $t5 + 1` echo $t5 done I am able to increment "col3" value but unable to get col1,col2 value. Input: t1=10001 t2=abc t3=ghkc (5 Replies)
Discussion started by: onesuri
5 Replies

6. Shell Programming and Scripting

UNIX shell script for loop issue

Guys, Please help on the below. sample.prm /u/test/: mail=123@gmail.com purgedays=30 zipdays=7 purgefile=log.gz zipfile=log /u/test/: mail=123@gmail.com purgedays=30 purgefile=txt.gz zipfile=txt zipdays=7 (1 Reply)
Discussion started by: AraR87
1 Replies

7. Shell Programming and Scripting

Issue with using While loop

Hi, I am trying to move a file from remote server to local server and when the transfer completes successfully i call a script in remote server to remove the file which was successfully transferred. I do this by first getting the list of file in remote server and move the text file to local... (8 Replies)
Discussion started by: funonnet
8 Replies

8. Shell Programming and Scripting

for-while loop issue

sup experts..i had a script which was bugging me..was hoping someone could point out the issue here Input file: space separated 2 columns I wanted to print out the 2 columns after assigning them to variables ( bascially the same output but iterate through line by line ). The code worked... (7 Replies)
Discussion started by: foal_newbie
7 Replies

9. Shell Programming and Scripting

loop issue

function ext { echo "THANKS & WELCOME BACK" } function upc { echo "TO EXPORT UPROC GIVE UPROC NAME PER LINE IN THE input.txt and PRESS Y" echo "TO GO BACK PRESS 99" read parm0 if ; then start elif ; then for i in `cat input.txt` ; do echo $i $UXEXE/uxext upr upr=$i... (0 Replies)
Discussion started by: kojo
0 Replies

10. Shell Programming and Scripting

while loop issue

Hi, Following is my code and the file FILE_LIST_EXCESS.txt has 40 file names in it while read LineIn do echo ${LineIn} `ftp -vin << END_INPUT >> ${PID}_DS_GET_Log.log 2>&1 open servername user userid password cd FileDir get ${LineIn} END_INPUT`... (4 Replies)
Discussion started by: mgirinath
4 Replies
Login or Register to Ask a Question