Problem iterating in while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem iterating in while loop
# 1  
Old 05-21-2010
Problem iterating in while loop

I am facing a problem in replacing the file contents by iterating through the list.

My present code:



Code:
Code:
#!/bin/bash#

TFILE="/tmp/vinay/testb_1.txt"
while read linedo  
aline="$line"                
echo $aline   
code=`echo $aline|cut -d ',' -f1`   
country=`echo $aline|cut -d ',' -f 2`
sed "s/$code/$country/g" testb_1.txt>testb_1.txt
sed 's/$/| testb_1.txt/g' "$TFILE" > "output1.txt"
echo $codeecho $countrydone<country_code.txt


and my

country code file contains some county code on line basis, seperated with commas.

Here after executing the script, only last country code is replacing with its country name. And if I change > (overwriting) to >> (appending) then same contents, multiple times with changes are appending to output file. I request you to suggest a solution for this.

And my second concern is that I have to append the name of the file at the end of each line, which I hav done using the commented line. Request you to suggest to perform both actions at a stretch.

Thanks.,

Last edited by av_vinay; 05-21-2010 at 05:48 AM..
# 2  
Old 05-21-2010
please post some lines from "country_code.txt"
what the expect output.

you still didn't check the last line formatting,

Code:
#!/bin/bash

#TFILE="/tmp/vinay/testb_1.txt"
while read line
do
 aline="$line"
 echo $aline
 code=`echo $aline|cut -d ',' -f1`
 country=`echo $aline|cut -d ',' -f 2`
 sed "s/$code/$country/g" testb_1.txt>testb_1.txt
 #sed 's/$/| testb_1.txt/g' "$TFILE" > "output1.txt"
 echo $codeecho $country
done < country_code.txt

# 3  
Old 05-21-2010
Hi, you are reading from and witing to the same file at the same time:
Code:
sed "s/$code/$country/g" testb_1.txt>testb_1.txt

You should specify a different output file.
# 4  
Old 05-21-2010
Here is the revised code, formatted:

Code:
 
#!/bin/bash

#TFILE="/tmp/vinay/testb_1.txt"

while read line
do
  aline="$line"
  echo $aline
   code=`echo $aline|cut -d ',' -f1`
   country=`echo $aline|cut -d ',' -f 2`

sed "s/$code/$country/g" testb_1.txt > "output1.txt"
#sed 's/$/| testb_1.txt/g' "$TFILE" > "output1.txt"
echo $code
echo $country
done<country_code.txt

And my country code file looks like:

001,ABC
002,PQR
003,XYZ
.
.
.

and so on.

ANd my i/p file is of the formatSmilieeg) [file name: testb_1.txt]

Code:
 
REX|12/01/2010|2. Desc|Author
A|1A|ID|001|Sachin|Tendulkar|||||||Y
A|2B|ID|003|Rahul|Dravid|||||||Y
A|3C|ID|001|Suresh|Raina|||||||Y
3 Records

And my desired o/p is of the format:


Code:
 
REX|12/01/2010|2. Desc|Author
A|1A|ID|ABC|Sachin|Tendulkar|||||||Y|testb_1.txt
A|2B|ID|XYZ|Rahul|Dravid|||||||Y|testb_1.txt
A|3C|ID|ABC|Suresh|Raina|||||||Y|testb_1.txt
3 Records

# 5  
Old 05-21-2010
Quote:
Originally Posted by av_vinay
Code:
#!/bin/bash#

TFILE="/tmp/vinay/testb_1.txt"
while read linedo  
aline="$line"                
echo $aline   
code=`echo $aline|cut -d ',' -f1`   
country=`echo $aline|cut -d ',' -f 2`
sed "s/$code/$country/g" testb_1.txt>testb_1.txt
sed 's/$/| testb_1.txt/g' "$TFILE" > "output1.txt"
echo $codeecho $countrydone<country_code.txt

First off, it would have been nice to show at least some sample lines from country_code.txt, which you read in. It is hard to predict what your code is doing when one is missing the input data it works on.

Second, you should write your code in an understandable way. You concatenate keywords (which isn't allowed at all), have several commands in one line without separating them by semicolons (which isn't allowed either) and don't indent properly (which is allowed, but makes it overly hard to read):

Code:
#!/bin/bash#

TFILE="/tmp/vinay/testb_1.txt"
while read line ; do  
     aline="$line"                
     echo $aline   

     code=`echo $aline|cut -d ',' -f1`   
     country=`echo $aline|cut -d ',' -f 2`
     sed "s/$code/$country/g" testb_1.txt>testb_1.txt
     sed 's/$/| testb_1.txt/g' "$TFILE" > "output1.txt"

     echo $code
     echo $country
done < country_code.txt

Third you should indeed use ">>" instead of ">", otherwise your output file will be overwritten with every pass of the loop. Anyhow, a lot of your loop is absolutely superfluous. The variable "aline" is not needed at all. Additionally you are writing to the input file "sed" reads from (for which you define a variable but never use it), which is not forbidden, but will not give the results you want:

Code:
#!/bin/bash#

TFILE="/tmp/vinay/testb_1.txt"
while read line ; do  
     echo $line   

     code="$(echo $line|cut -d ',' -f1)"
     country="$(echo $line|cut -d ',' -f2)"
     sed "s/$code/$country/g" "$TFILE" >> "${TFILE}".new
     sed 's/$/| testb_1.txt/g' "$TFILE" >> "output1.txt"

     echo $code
     echo $country
done < country_code.txt


I still don't get the meaning of the second sed-statement, because several passes of the loop will add "| testb_1.txt" to the end of each line.

Just in case you have a list of country-codes and what to replace them by the countries names, your code won't do it anyhow:

Code:
#!/bin/bash

TFILE="/tmp/vinay/testb_1.txt"
cat country_code.txt | while read line ; do  
     code="$(echo "$line" | cut -d ',' -f1)"
     country="$(echo "$line" | cut -d ',' -f2)"

     sed 's/'"$code"'/'"$country"'/g' "$TFILE" > "${TFILE}".tmp
     mv $"${TFILE}.tmp" "$TFILE"

     echo LINE=${line} CODE=${code} COUNTRY=${country}   
done
sed 's/$/| testb_1.txt/g' "$TFILE" > "output1.txt"

I won't expand on the recurring theme of replacing the "echo ... | cut ..." with simple shell variable expansions, i suppose this is enough for one session to devour.

I hope this helps.

bakunin
# 6  
Old 05-21-2010
Thanks for providing me the solution in arriving at the desired output.
I got the correct output.

As I don't have much exp. in Shell scripting, I just want to know what this line of code does in the execution :

mv $"${TFILE}.tmp" "$TFILE"

renaming the file back to original file name?. And here one thing I want to understand about why it is required to create the temp file.


Thanks.,
Vinay
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Question on iterating array elements

Hi, I am trying to do something similar to the for loop example from KSH For Loop Array: Iterate Through Array Values $: cat y.ksh #!/bin/ksh # set array called nameservers set -A nameservers 192.168.1.1 192.168.1.5 202.54.1.5 # print all name servers for i in ${nameservers} do ... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

Iterating awk over a directory of files?

I need to run the same awk function over an entire directly of files. This is the awk: awk '{$(NF+1)=1}1' Is there a way that I can run this command once over all of the files, along the lines of: awk '{$(NF+1)=1}1' * so that I do not have to run this several times? My main concern is... (2 Replies)
Discussion started by: owwow14
2 Replies

3. Shell Programming and Scripting

Bash Script Iterating Question

I am trying to look through one of my directories to remove certain files. I am pretty new to Unix and bash so I just need a little help in starting this. I know I would have to write two loops one to iterate the directories and one to iterate the files. How would I write the loops? (3 Replies)
Discussion started by: totoro125
3 Replies

4. Shell Programming and Scripting

Iterating over a list using awk, when variable

Hi, I've recently started using Awk commands as i need it to pull information out of a file and give me the mean value of a series of numbers. Here is the code i run on my Infile and it works just fine. awk '{if ($1 == "Mam189") print $0}' Infile | awk '{if ($1 != $2) print $0}' | awk... (5 Replies)
Discussion started by: cavanac2
5 Replies

5. Shell Programming and Scripting

Iterating over subdirectories and dealing with files within them

Hello, I am working on a coding project for a class and to test the program I have created, I have come up with 100 different test cases. The program takes four text files as input, so each of the test cases is contained in a folder with four files. I have a folder called 'tests', within which... (1 Reply)
Discussion started by: dpryor
1 Replies

6. Programming

Iterating and calloc questions.

Whilst creating the function readjust_descr I have stumble across what may be a problem or something that might just work. I was hoping someone could look at the code below and tell me if readjust_descr will clear all null pointers from the structure descr_list. struct descr descr_list =... (6 Replies)
Discussion started by: Errigour
6 Replies

7. Shell Programming and Scripting

Iterating through two arrays in a single loop

Hey everyone. Is it possible to use two arrays in a loop? Basically what I am trying to do is iterate through the elements in an array, and, based on a condition, use the current position in the array to point to that index in the array. Here's the loop structure I'm looking for: ... (1 Reply)
Discussion started by: msarro
1 Replies

8. Shell Programming and Scripting

iterating over results from sqlplus

hi all, i am writing a ksh script, i am logging into an oracle db via sqlplus and running a select statement but i dont know how i can store the results from the sql so i can iterate over it and do more operations over it. I dont want to write a stored procedure with a cursor since i need to... (2 Replies)
Discussion started by: cesarNZ
2 Replies

9. Shell Programming and Scripting

Problem iterating through PATH entries with spaces

I have a Bash script on Cygwin that tries to iterate through the directory entries in PATH. I need to convert the PATH value to a form that I can iterate through with "for var in $list; do". For instance, an excerpt from my PATH value is this: :/c/Program Files/Windows Imaging/:/c/Program... (2 Replies)
Discussion started by: dkarr
2 Replies

10. Shell Programming and Scripting

Perl - Iterating a hash through a foreach loop - unexpected results

i've reworked some code from an earlier post, and it isn't working as expected i've simplified it to try and find the problem. i spent hours trying to figure out what is wrong, eventually thinking there was a bug in perl or a problem with my computer. but, i've tried it on 3 machines with the... (5 Replies)
Discussion started by: quantumechanix
5 Replies
Login or Register to Ask a Question