Trouble with a file path as awk output in for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trouble with a file path as awk output in for loop
# 1  
Old 09-30-2009
Trouble with a file path as awk output in for loop

When I run the following command in the shell it works fine. It prints a city name and then a path for a file.

Code:
~$ for i in `awk -F':' '{print $0}' /home/knoppix/Desktop/data/subs | grep -m 1 $ city | sed "s/:/ /"`
>do
>echo $i
>done

Now, when I place it in this shell script (sh) it prints the first entry, a city name, but it just give me blank for the path.

Code:
for sub in `awk -F':' '{print $0}' $datsub | grep -m 1 $city | sed "s/:/ /"g`
do

if [ "`echo $sub`" == "`echo $city `" ]
then
echo "Substituting files in $projectname now"

elif [ "`echo $sub | grep .jpg`" ]
then
cp -r $sub $projectname/images
echo "$sub replaced"

elif [ "`echo $sub | grep .gif`" ]
then
cp -r $sub $projectname/images
echo "$sub replaced"

elif [ "`echo $sub | grep .png`" ]
then
cp -r $sub $projectname/images
echo "$sub replaced"

else
cp -r $sub $projectname
echo "$sub replaced"
fi
done

I'm really confused as to why. I know it's getting to the entry, I don't know why it's printing blank. I even tried escaping the "/" characters and all it did was print them as well.

The subs file is written like:

Code:
City1:/home/knoppix/subs/file1
City2:/home/knoppix/subs/file2
City3:/home/knoppix/subs/file3
...


Last edited by afroCluster; 09-30-2009 at 01:12 PM.. Reason: Making it a bit more readible.
# 2  
Old 09-30-2009
Quote:
Originally Posted by afroCluster
Code:
~$ for i in `awk -F':' '{print $0}' /home/knoppix/Desktop/data/subs | grep -m 1 $ city | sed "s/:/ /"`

Sorry but this is nonsense, maybe you try to win the Useless Use of Cat Award Smilie
Code:
oIFS=IFS
IFS=":" 
while read city path
    do 
          echo $city $path
          # Insert your IF's here
done < sub.file
IFS=$oIFS

# 3  
Old 09-30-2009
Quote:
Originally Posted by danmero
Sorry but this is nonsense, maybe you try to win the Useless Use of Cat Award Smilie
Code:
oIFS=IFS
IFS=":" 
while read city path
    do 
          echo $city $path
          # Insert your IF's here
done < sub.file
IFS=$oIFS

no need for the oIFS:
Code:
while IFS=: read city path
    do 
          echo $city $path
          # Insert your IF's here
done < sub.file

# 4  
Old 09-30-2009
Quote:
Originally Posted by vgersh99
no need for the oIFS:
Just in case Smilie
# 5  
Old 09-30-2009
Yea, it looks that way, but it worked. I just removed the -r from in front of the cp commands.

I'm always trying to learn though. Are those awk scripts?
# 6  
Old 09-30-2009
No it is shell script only
# 7  
Old 09-30-2009
Wow, well I need to hit the books.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print awk output in same line ,For loop

My code is something like below. #/bin/bash for i in `ps -ef | grep pmon | grep -v bash | grep -v grep | grep -v perl | grep -v asm | grep -v MGMT|awk '{print $1" "$8}'` do echo $i ORACLE_SID=`echo $line | awk '{print $2}'` USERNAME=`echo $line | awk '{print $1}'` done ============= But... (3 Replies)
Discussion started by: tapia
3 Replies

2. Shell Programming and Scripting

Considerable trouble with for loop in combination with awk

I have the text file where each line has the format: chr10 101418889 101418904 0.816327 Right now the interval between column 2 and 3 is 15. I only want the two consecutive positions starting at position 1, write it to a file, then move up one position write to file etc. So that: ... (1 Reply)
Discussion started by: jfern
1 Replies

3. UNIX for Dummies Questions & Answers

Print each output of loop in new column using awk or shell

I have this output from a loop a11 1,2 3,4 5,6 7,8 12,8 5,4 3,6 a12 10,11 12,13 15,18 20,22 a13 ... (3 Replies)
Discussion started by: maryre89
3 Replies

4. Shell Programming and Scripting

Recursive file processing from a path and printing output in a file

Hi All, The script below read the path and searches for the directories/subdirectories and for the files. If files are found in the sub directories then read the content of the all files and put the content in csv(comma delimted) format and the call the write to xml function to write the std... (1 Reply)
Discussion started by: Optimus81
1 Replies

5. Shell Programming and Scripting

awk: too many output files created from while loop

I am using awk to read lines from a CSV file then put data into other files. These other files are named using the value of a certain column. Column 7 is a name such as "att" or "charter" . I want to end up with file names with the value of column 7 appended to them, like this: ... (5 Replies)
Discussion started by: dodgerfan78
5 Replies

6. Shell Programming and Scripting

awk output error while loop through array

Have built this script, the output is what I needed, but NR 6 is omitted. Why? Is it an error? I am using Gawk. '{nr=$2;f = $1} END{for (i=1;i<=f;i++) if (nr != i) print i, nr }' input1.csv >output1.csvinput1.csv 1 9 3 5 4 1 7 6 8 5 10 6 output1.csv > with the missing line number 6. 6 is... (5 Replies)
Discussion started by: sdf
5 Replies

7. Shell Programming and Scripting

unexpected output within a for loop using awk

Hi all, after hours of playing around with this and scouring the web I decided to ask my fellow UNIX operators as I can't wrap my head around this. First off, I want to parse an input file with tabs (I could pull this off easily with different delimiters) but I was trying to make nicer... (2 Replies)
Discussion started by: Keepcase
2 Replies

8. Shell Programming and Scripting

for loop returns more output with awk

I need to get total number of hdisk not assigned to any VGs. PDC # lspv |grep None |awk '{print $1}' |wc 131 131 1099 So, it shows 131 hdisks. I need to look at the individual hdisk fget_config info like below: PDC # fget_config -Av |grep hdisk230 hdisk230 dac1 229... (4 Replies)
Discussion started by: Daniel Gate
4 Replies

9. Shell Programming and Scripting

Shell script to loop for a particular file in predefined path

Hi , i need a shell script to poll a particular file in a predefined directory , once that file is found , i need to launch another shell script . Eg i need to poll for A.txt in /home/username/Desktop once A.txt is created by Desktop(Created by another Script) , i need to launch another... (2 Replies)
Discussion started by: Vinod H C
2 Replies

10. Shell Programming and Scripting

Input file redirect in output path and want name as inputfilename_new.txt

not required this time (6 Replies)
Discussion started by: Sandeep_Malik
6 Replies
Login or Register to Ask a Question