The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
total output from a file created in a while loop Pablo_beezo Shell Programming and Scripting 7 01-29-2009 11:26 AM
Input file redirect in output path and want name as inputfilename_new.txt Sandeep_Malik Shell Programming and Scripting 6 09-17-2008 06:16 PM
for loop syntax trouble visitorQ Shell Programming and Scripting 9 04-13-2008 03:53 AM
Loop Trouble RSymphony Shell Programming and Scripting 2 11-26-2007 11:50 AM
Start more than one database - trouble with for loop dave-mentor Shell Programming and Scripting 5 05-30-2006 03:58 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-30-2009
afroCluster afroCluster is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 5
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 (permalink)  
Old 09-30-2009
danmero danmero is online now Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,434
Quote:
Originally Posted by afroCluster View Post
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

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

  #3 (permalink)  
Old 09-30-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,130
Quote:
Originally Posted by danmero View Post
Sorry but this is nonsense, maybe you try to win the Useless Use of Cat Award

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 (permalink)  
Old 09-30-2009
danmero danmero is online now Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,434
Quote:
Originally Posted by vgersh99 View Post
no need for the oIFS:
Just in case
  #5 (permalink)  
Old 09-30-2009
afroCluster afroCluster is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 5
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 (permalink)  
Old 09-30-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,784
No it is shell script only
  #7 (permalink)  
Old 09-30-2009
afroCluster afroCluster is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 5
Wow, well I need to hit the books.
Reply

Bookmarks

Tags
awk, path, script

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:42 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0