The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Special Forums > UNIX Desktop for Dummies Questions & Answers
Google UNIX.COM


UNIX Desktop for Dummies Questions & Answers Questions regarding GNOME, KDE, CDE, Open Office, etc go here. All UNIX and Linux Newbies Welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Create a script using DATE command? thecoffeeguy Shell Programming and Scripting 1 05-15-2008 03:33 PM
trying to create a script to read a flie and loop a command... Italy87 UNIX for Advanced & Expert Users 1 03-25-2008 05:44 AM
script to count a word in a flie sipmohan UNIX for Dummies Questions & Answers 3 02-21-2008 11:39 PM
ls while read loop - internal read picking up wrong input dkieran Shell Programming and Scripting 2 05-14-2007 12:02 PM
AWK create loop for fields tonet Shell Programming and Scripting 1 07-07-2005 06:50 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-24-2008
Registered User
 

Join Date: Mar 2008
Location: EHT, NJ
Posts: 7
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
trying to create a script to read a flie and loop a command...

I tried to explain this earlier, but did a poor job of it. So I am trying again but taking a different approach.....

I have a file called test_words. In this file I have one line with 5 three letter words, with a single space seperating the words.(I.e., "cat car mop red pin")

I want to create a script that will read the file and loop each word individually into a command until all words in the file have been used. Use the echo command for arguements sake.

the output would look like this:

cat
car
mop
red
pin

The file test_word may change, by adding words or subtracting words, but always use a space as the delimiter.

can anyone help with this?

Thank you in advance for any and all help on this.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-24-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,394
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Something like this?

Code:
sed 's/ /\n/g' file
Regards
Reply With Quote
  #3 (permalink)  
Old 03-25-2008
Registered User
 

Join Date: Mar 2008
Location: EHT, NJ
Posts: 7
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Thank you for helping with seperating it, Franklin, but how to put it in a loop to run a command with each word? that is where I really need the help.
Reply With Quote
  #4 (permalink)  
Old 03-25-2008
Registered User
 

Join Date: Mar 2008
Location: Toronto, Canada
Posts: 66
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Try:

tr ' ' '\n' <file |sed "s/^/$CMND /" | sh

The above pipe-lined command will put $CMND (i.e. whatever is in CMND variable) before each word as one per line and then will execute all of them, again in sequence.

I hope this is what you wanted to do.
Reply With Quote
  #5 (permalink)  
Old 03-25-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 383
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Code:
for word in $(<file)
do
   echo your_cmd_goes_here $word
done
...remove the echo and put your command in place of "your_cmd_goes_here"
Reply With Quote
  #6 (permalink)  
Old 03-26-2008
Registered User
 

Join Date: Mar 2008
Location: EHT, NJ
Posts: 7
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Shamrock, Thank you that worked perfect!!!! Sham-rocks!!

Thank you all, for your help.

for some reason I could not get it to work with what unilover suggested. not sure why but here is the command that worked with Shamrock.


for word in $(<file)
do response=command {$word is located within command}
echo
echo "$response"
echo "$response" >> ./return-data
file=return-data
done

cat "$file" | lpr
echo "All data has been sent to the following file."
echo "file"
echo "Script complete."



Thanks a million all. And I am sorry for posting in two forums. I did not get a sponse, so I thought it was ok, to post in an advance forum. Again, sorry, I am a newb to this forum.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
"inappropriate ioctl for device" 421 service not available, remote server has closed connection autosys awk trim bash eval bash exec bash for loop boot: cannot open kernel/sparcv9/unix close_wait command copy/move folder in unix curses.h cut command in unix dead.letter find grep find null character in a unix file grep multiple lines grep or grep recursive inaddr_any inappropriate ioctl for device logrotate.conf lynx javascript mailx attachment mget mtime ping port remove first character from string in k shell replace space by comma , perl script scp recursive segmentation fault(coredump) sftp batch sftp script snoop unix stale nfs file handle syn_sent tar exclude unix unix .profile unix com unix date command unix forum unix forums unix internals unix interview questions unix mtime unix simulator unix.com vi select all vi+substitute+end+of+line+character while loop within while loop shell script


All times are GMT -7. The time now is 11:26 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101