trying to create a script to read a flie and loop a command...


 
Thread Tools Search this Thread
Special Forums UNIX Desktop Questions & Answers trying to create a script to read a flie and loop a command...
# 1  
Old 03-24-2008
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.
# 2  
Old 03-24-2008
Something like this?

Code:
sed 's/ /\n/g' file

Regards
# 3  
Old 03-25-2008
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.
# 4  
Old 03-25-2008
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.
# 5  
Old 03-25-2008
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"
# 6  
Old 03-26-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read function is going in infinite in another script having while loop

Hello Experts, I have created one user confirmation process that will ask for user input. I have created one func for it. The issue is if i call it as normal then it works fine but if i am calling it in another script(in while loop) . It is going in infinite loop and not asking for user input. ... (8 Replies)
Discussion started by: looney
8 Replies

2. Shell Programming and Scripting

Script to read through a file and create new users/assign them to groups in Ubuntu

Hi all. I need a shell script that can, in short, read through a text file line by line and create a new user in Ubuntu, as well as assign that user to a group. The format of the text file is not important but preferably: 'username:group'. I don't have much programming knowledge no matter shell... (3 Replies)
Discussion started by: LewisWeekly
3 Replies

3. Shell Programming and Scripting

How to loop read command and print valid invalid states.?

My question is how would i loop a read command to keep asking the user for input and eventually print the no. of valid invalid inputs after a specified control input typed i.e. (-3). (1 Reply)
Discussion started by: Flowoftruth
1 Replies

4. Shell Programming and Scripting

How to create a shell script to read a foldername from a text file and go to the folder?

Hi, I am trying to write a shell script which can read folder names from a text file and then go to the folder and picks up a xml file and write on my sipp script so that I can run the sipp script. For example: I have a text file called thelist.txt where I have provided all the folders... (7 Replies)
Discussion started by: pm1504
7 Replies

5. UNIX for Dummies Questions & Answers

Read a flat file, evaluate and create output. UNIX SCRIPT.

Hi all, I have a flat file as below; 470423495|1||TSA-A000073800||1|||1 471423495|1||TSA-A000073800||5|||5 472423495|1||TSA-A000073800||2|||7 473423495|1||TSA-A000073800||3|||3 I like to create a Unix script. The script have to valuate the last two columns, if the values are... (4 Replies)
Discussion started by: mrreds
4 Replies

6. Shell Programming and Scripting

How to create DB2 Connections in While loop using shell script

Hi , I have to create a db2 connection for the while loop in shell scripting. Below is the connection for DB2 I have to establish connect to TABLESCHENMA user $USERID using $PASSWORD Below is the while loop. while read TABLE; do db2 LOAD CLIENT FROM $DIRECTORY/$TABLE.ixf OF ixf INSERT... (1 Reply)
Discussion started by: vikyalex4
1 Replies

7. Shell Programming and Scripting

how to create variables in loop and assign filename after set command?

Hi, does anybody knows how to manage, that the filenames are assigned to a variable in a loop afer getting them with set command in a ksh, like: set B*.txt i=1 c=$# x=$((c+1)) echo "$x" while ] ; do _ftpfile$i="$"$i echo "$_ftpfile$i" i=$((i+1)) done The first echo returns,... (2 Replies)
Discussion started by: spidermike
2 Replies

8. Shell Programming and Scripting

read command (input) inside the while loop

Hi, 'read' command is not working inside the while loop, How can I solve this? Rgds, Sharif. (2 Replies)
Discussion started by: sharif
2 Replies

9. UNIX for Advanced & Expert Users

trying to create a script to read a flie and loop a command...

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., "dog car mop red pin") I want to create a... (1 Reply)
Discussion started by: Italy87
1 Replies

10. UNIX for Dummies Questions & Answers

script to count a word in a flie

hi All, I have one question to have a answer with your help. i need to have a script for searching a file with .log extension form all directories. search for a word " Pass" in the files and to count the occurence of the word "Pass". i need all your help to solve the problem. Thanks,... (3 Replies)
Discussion started by: sipmohan
3 Replies
Login or Register to Ask a Question