Read from a file and use the strings in a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read from a file and use the strings in a loop
# 1  
Old 04-14-2006
SOLVED: Read from a file and use the strings in a loop

Hello all,
I need some help to create a script which contain a few strings on every line, and use those strings in a loop to fire some commands.
for exmaple the file looks like
tom
dave
bill
andy
paul

I want to read one line at a time and use it in loop like
command tom
command dave
......
......
and ignore the last line which will always be empty.

Thanks for your help.

Last edited by xboxer21; 04-14-2006 at 02:42 PM.. Reason: SOLVED
# 2  
Old 04-14-2006
Code:
#! /usr/bin/ksh

exec < file.with.strings
while read string1 ; do
      if [[ $string1 != "" ]] ; then
               command $string1
      fi
done
exit 0

To read 2 strings per line it would be
while read string1 string2 ; do
# 3  
Old 04-14-2006
Solved

Quote:
Originally Posted by Perderabo
Code:
#! /usr/bin/ksh

exec < file.with.strings
while read string1 ; do
      if [[ $string1 != "" ]] ; then
               command $string1
      fi
done
exit 0

To read 2 strings per line it would be
while read string1 string2 ; do
Thank you Perderabo
Worked very well.
# 4  
Old 04-18-2006
You can type this right on the command line too. Now script needed.

for i in $(cat file)
do
command $i
done

-X
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix. I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice. Files:... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

2. Shell Programming and Scripting

Failure using regex with awk in 'while read file' loop

I have a file1.txt with several 100k lines, each of which has a column 9 containing one of 60 "label" identifiers. Using an labels.txt file containing a list of labels, I'd like to extract 200 random lines from file1.txt for each of the labels in index.txt. Using a contrived mini-example: $ cat... (8 Replies)
Discussion started by: pathunkathunk
8 Replies

3. Shell Programming and Scripting

Using awk instead of while loop to read file

Hello, I have a huge file, I am currently using while loop to read and do some calculation on it, but it is taking a lot of time. I want to use AWK to read and do those calculations. Please suggest. currently doing: cat input2 | while read var1 do num=`echo $var1 | awk... (6 Replies)
Discussion started by: anand2308
6 Replies

4. Shell Programming and Scripting

Read file using while loop not reading last line

I have written a script to read the file line by line. It is reading and printing the lines. But it is coming out of loop before reading last line. So I am not able to print last line. How do I solve it. (6 Replies)
Discussion started by: dgmm
6 Replies

5. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

6. Shell Programming and Scripting

Problem reading file in while/read loop

I know I should be able to see a way of doing this easily, but my brain just won't engage. I have a script working on an embedded device that checks to see if an item is in a blacklist before performing some actions. At the moment the code reads thus.... while read BLACKLIST ; do ... (7 Replies)
Discussion started by: Bashingaway
7 Replies

7. Shell Programming and Scripting

Read file and remove special characters or strings

Hello all I am getting data like col1 | col2 | col3 asdafa | asdfasfa | asf*&^sgê 345./ |sdfasd23425^%^&^ | sdfsa23 êsfsfd | sf(* | sdfsasf My requirement is like I have to to read the file and remove all special characters and hex characters ranging form 00-1f from 1st column, remove %"'... (1 Reply)
Discussion started by: vasuarjula
1 Replies

8. Shell Programming and Scripting

How to Read the entire file using while loop

Guys, I am trying to read the whole file using while loop but when i run the ssh part of the script it reads only the first line and exit after that. There are in total 134 lines in the file, but when the output is redirected, it does only for one line and comes to command prompt. pls help..... (11 Replies)
Discussion started by: sdosanjh
11 Replies

9. Shell Programming and Scripting

How to run a loop for assigning strings which are present in a file to an array

Hi Forum, I am struggling with the for loop in shell script. Let me explain what is needed in the script. I have a file which will conatin some strings like file1 place1 place2 place3 checkpoint some other text some more text Now what my requirement is the words ... (2 Replies)
Discussion started by: siri_14
2 Replies

10. UNIX for Dummies Questions & Answers

How to read a file in unix using do....done loop

Hi , can some give me idea about how to use do...done while loop in UNIX to read the contents of a file.. (2 Replies)
Discussion started by: sreenusola
2 Replies
Login or Register to Ask a Question