SED inside while loop


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers SED inside while loop
# 1  
Old 11-10-2009
SED inside while loop

Hi,


im having problem creating a loop using my code: aside from the fact that the 1st variable (VAR) does not increment, it loops more than the expected output.

for sample purposes, test csv contains 3 lines.

Code:
 
#get number of lines in the file
 lines=$( wc -l < test.csv )
 
  VAR=1
  VAR2=1
 while [ $VAR -le $lines+1 ]
 do
 while [ $VAR2 -le $lines+1 ]
 do
      sed "s/,,/,one"$VAR"too"$VAR2",/" test.csv > test1.csv 
      VAR2=$(( $VAR2 + 1 ))
  done
 
  VAR=$(( $VAR + 1 ))
 
 done

Output of the code is like this, this is caused by the sed command, its like the script executes first the sed command then the loop:

Code:
 
a,b,c,d,e,one1too1,f
a,b,c,d,e,one1too1,f
a,b,c,d,e,one1too1,f
a,b,c,d,e,one1too2,f
a,b,c,d,e,one1too2,f
a,b,c,d,e,one1too2,f
a,b,c,d,e,one1too3,f
a,b,c,d,e,one1too3,f
a,b,c,d,e,one1too3,f
a,b,c,d,e,one1too4,f
a,b,c,d,e,one1too4,f
a,b,c,d,e,one1too4,f

But the output should be:

Code:
a,b,c,d,e,one1too1,f
a,b,c,d,e,one1too2,f
a,b,c,d,e,one1too3,f
a,b,c,d,e,one1too4,f

i hope i explained and posted this question appropriately...

Hope someone could help..really appreaciate it.. thanks!
# 2  
Old 11-10-2009
I'm still not clear on what you are after here, but a couple of obvious things:

Code:
 lines=$( wc -l < test.csv )
 lines=$((lines + 1))
 
  VAR=1
  VAR2=1

 while [ $VAR -le $lines  ]
 do
 while [ $VAR2 -le $lines  ]
 do
      sed "s/,,/,one"$VAR"too"$VAR2",/" test.csv  > test1.csv
      VAR2=$(( VAR2 + 1 ))
  done
 
  VAR=$(( $VAR + 1 ))
 
 done

# 3  
Old 11-11-2009
what i mean is i want to insert incrementing numbers in the varibles $VAR and $VAR2. But the code is making the sed command, which displays all the lines in the file test.csv, $lines number of times before it loops and repeats again.

that is why the result is

Code:
 
a,b,c,d,e,one1too1,f
a,b,c,d,e,one1too1,f
a,b,c,d,e,one1too1,f
 
a,b,c,d,e,one1too2,f
a,b,c,d,e,one1too2,f
a,b,c,d,e,one1too2,f
 
a,b,c,d,e,one1too3,f
a,b,c,d,e,one1too3,f
a,b,c,d,e,one1too3,f
 
a,b,c,d,e,one1too4,f
a,b,c,d,e,one1too4,f
a,b,c,d,e,one1too4,f

when i tried your sugesstion:

the result looks like this:

Code:
 
1
a,b,c,d,e,one1too1,f
a,b,c,d,e,one1too1,f
a,b,c,d,e,one1too1,f
2
a,b,c,d,e,one1too2,f
a,b,c,d,e,one1too2,f
a,b,c,d,e,one1too2,f
3
a,b,c,d,e,one1too3,f
a,b,c,d,e,one1too3,f
a,b,c,d,e,one1too3,f
4
a,b,c,d,e,one1too4,f
a,b,c,d,e,one1too4,f
a,b,c,d,e,one1too4,f
1
2
3
4


what i want the result to be is:

Code:
 
a,b,c,d,e,one1too1,f
a,b,c,d,e,one2too2,f
a,b,c,d,e,one3too3,f
a,b,c,d,e,one4too4,f

The word "one" and "too" are fixed and only the numbers (e.g from 1 to 4) should be incrementing.
# 4  
Old 11-12-2009
Your inner loop only works once since you do not reset the VAR2 variable. So after the next iterations of the outer loop the inner loop no longer gets executed, VAR2 being equal to 4.
For the output you want you do not need two loops. It could be done with one since var1 and var2 are always the same. Further what you have to understand is that sed operates on the entire input file and will output every line of the input file with the two comma's replaced and write the result to test1.csv. For sed to work only on a specific line, you would need to specify that. Another thing that is going on is that every next loop iteration the output file will get overwritten since you use '>' instead '>>' (append).

So then your loop would look something like this:

Code:
# get number of lines in the file
  lines=$( wc -l < test.csv )
 lines=$((lines + 1))
  VAR=1
  while [ $VAR -le $lines ]
  do
     echo sed -n "${VAR}s/,,/,one${VAR}too${VAR},/p"
     sed -n "${VAR}s/,,/,one${VAR}too${VAR},/p" test.csv >> test1.csv
     VAR=$(( $VAR + 1 ))
  done

But the preferred way of going to that file would be like this:
Code:
VAR=1
  while read line
  do
     echo "$line"| sed "s/,,/,one${VAR}too${VAR},/"
     VAR=$(( $VAR + 1 ))
  done < test.csv > test1.csv

You could then further optimize by replacing sed with shell variable expansion...

Code:
  VAR=1
  while read line
  do
    echo "${line%,,*},one${VAR}too${VAR},${line#*,,}"
    VAR=$(( $VAR + 1 ))
  done < test.csv > test1.csv


Last edited by Scrutinizer; 11-12-2009 at 02:26 AM..
# 5  
Old 11-12-2009
Hi,
I am not sure how your input file is.
Assuming your input file test.csv as
a,b,c,d,e,,f
a,b,c,d,e,,f
a,b,c,d,e,,f
a,b,c,d,e,,f

the following code will give you the desired output.
cnt=1
while read line; do
sed -n ''$cnt' p' test.csv | sed 's/\,\,/\,one'$cnt'too'$cnt'\,/' >> test1.csv
cnt=$(echo "$cnt + 1" | bc)
done < test.csv

output:
a,b,c,d,e,one1too1,f
a,b,c,d,e,one2too2,f
a,b,c,d,e,one3too3,f
a,b,c,d,e,one4too4,f

Hope this helps.
# 6  
Old 11-17-2009
Thank you so much guys for all the help. Now i know why my code was acting like that!! .. this was a great help!!! ... really really appreciated it!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Write a while loop inside for loop?

I'm taking a unix class and need to countdown to 0 from whatever number the user inputs. I know how to do this with a while or until loop but using the for loop is throwing me off.... I know I can use an if-then statement in my for loop but can I include a while loop in my for loop? (3 Replies)
Discussion started by: xxhieixx
3 Replies

2. Shell Programming and Scripting

If inside If loop

Hi All, Below is the very simple code snippet but it si giving me syntax error #!/bin/bash #To ensure If JMS directory exists or not ServerName=$(hostname) #To ensure If JMS directory exists or not echo $ServerName if ; then echo "Inside First If" if ; then echo 'JMS... (4 Replies)
Discussion started by: sharsour
4 Replies

3. Shell Programming and Scripting

help using EOF inside a for loop

I'm having problems with this example: #!/bin/bash for i in server1 server2 server3 do ssh $i <<EOF >> logfile.txt dat1=`date '+%m/%d/%y'` hst=`hostname` df -Ph | awk -v OFS=',' -v dat="$dat1,$hst" ' {print dat,$1,$6,$3,$4 } ' exit EOF done I'm... (1 Reply)
Discussion started by: kuliksco
1 Replies

4. Shell Programming and Scripting

sed inside sed for replacing string

My need is : Want to change docBase="/something/something/something" to docBase="/only/this/path/for/all/files" I have some (about 250 files)xml files. In FileOne it contains <Context path="/PPP" displayName="PPP" docBase="/home/me/documents" reloadable="true" crossContext="true">... (1 Reply)
Discussion started by: linuxadmin
1 Replies

5. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

6. Shell Programming and Scripting

Need help using sed inside the loop.

Hi, i have written a script. it collects data based on the sql queries executed by it. i have multiple output files. after the output file is made i need to do some cosmetic changes in the files and then store them. i am unable to use sed conditions inside the loop. see below code for... (3 Replies)
Discussion started by: dazdseg
3 Replies

7. Shell Programming and Scripting

Using 'su' inside a loop

Hi, I am using su within a for loop. As you might expect, it prompts for password during each loop execution. Here is my piece of code: for i in $LIST do if then DATABASE=`echo $i | awk -F "|" '{ print $1 }'` USER_ID=`echo $i | awk -F "|" '{ print $2 }'` su - apstage -c... (1 Reply)
Discussion started by: sugan
1 Replies

8. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

9. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

10. UNIX for Advanced & Expert Users

remsh inside of while loop

I have a space delimited file containing: hostname OracleSID connectstring I want to loop through the file and execute remsh to check the database processes. cat $filename | while read HOST SID CONNECT do { result=`remsh $HOST "ps -ef | grep pmon_${SID}$| grep -v grep"` if ........ (1 Reply)
Discussion started by: joettacm
1 Replies
Login or Register to Ask a Question