Loop through the lines in a file?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Loop through the lines in a file?
# 1  
Old 12-03-2001
Question Loop through the lines in a file?

How do I loop through the lines of a file?

Each line of the file will be the name of a server.

Like:
server1.stuff.com
server2.stuff.com

For each server I want to do a secure copy of the file to the other server.

This will be a cron job so I guess it needs to run in just SH. I could be wrong about that also.

THANKS!
# 2  
Old 12-03-2001
try:
Code:
for host in `cat filename`
do
	# secure copy commands
done


Your script does not have to be SH, although that is the default in cron. You can use any shell by using the standard "#!/bin/shellname" notation at the top of your script.
# 3  
Old 12-03-2001
Thanks!!Smilie

Is there a good internet site that has answers to these "newbie" questions?
# 4  
Old 12-03-2001
I should have pointed out that in the loop above, the $host variable will contain the name of the hosts in your file, one at a time as it loops. Sorry if that wasn't clear.

I think this site is one of the best for answering "newbie" auestions. Read through the FAQ section, especially our recommendations on books for beginners.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using sed in a loop/to remove lines contained in variable from file

I've tried numerous commands, but I am not sure how to use sed in a loop. This is what I have: VARZ contains CARD_FILE_LIST and it also contains CARD_FILE_LIST2 so echo "$VARZ" CARD_FILE_LIST CARD_FILE_LIST2 I have a file with 60 lines in /tmp/testfile it and I want those lines deleted... (3 Replies)
Discussion started by: newbie2010
3 Replies

2. Shell Programming and Scripting

ksh Loop through file one, remove lines from file two

Good Afternoon, I start with a file named biglist.txt. I have another file smallerlist. txt I want to remove the lines from smallerlist.txt from biglist.txt and leave those lines that do not reside in smallerlist.txt. Thanks !! (2 Replies)
Discussion started by: popeye
2 Replies

3. Shell Programming and Scripting

Reading line by line from live log file using while loop and considering only those lines start from

Hi, I want to read a live log file line by line and considering those line which start from time stamp; Below code I am using, which read line but throws an exception when comparing line that does not contain error code tail -F /logs/COMMON-ERROR.log | while read myline; do... (2 Replies)
Discussion started by: ketanraut
2 Replies

4. Shell Programming and Scripting

Reading two lines in a while loop and combining the lines

Dear all, I have a file like this: imput scaffold_0 1 scaffold_0 10000 scaffold_0 20000 scaffold_0 25000 scaffold_1 1 scaffold_1 10000 scaffold_1 20000 scaffold_1 23283 and I want the output like this: scaffold_0 1 scaffold_0 10000 scaffold_0 10000 scaffold_0 20000... (6 Replies)
Discussion started by: valente
6 Replies

5. UNIX for Dummies Questions & Answers

analyze lines in a file by loop

Hi Dears, I use the below code to analyze lines in a file: for line in `cat ucsv` do echo $line //analyze statements donehowever, if line contains space char, it will be broken. for example, if file content is: #login,full name,email,project,role,action gmwen,Bruce... (3 Replies)
Discussion started by: crest.boy
3 Replies

6. Shell Programming and Scripting

How to get different lines using a for loop

Hi I am trying to write a small AWK program to extract 324 lines which have a pattern in the line numbers. For examples, I want to extract lines 2, 5, 7, 11 and lines like (2+662), (5+662), (7+662), (11+662) and (2+662+662), (5+662+662), (7+662+662), (11+662+662) and so on for 27 times from... (5 Replies)
Discussion started by: ananyob
5 Replies

7. Shell Programming and Scripting

Loop through between 2 found lines

Hi frnd , hv nice week ahead, in shell script i found 2 words as below 1 ) targ_found=`sed -n \`echo $line_no\`p sesslog.txt | grep -ic 'Targ Rowid'` 2 ) bracket_found=`sed -n \`echo $line_no\`p sesslog.txt | grep -wc ')'` now we got he line number of 2 words. now i want to... (1 Reply)
Discussion started by: Gopal_Engg
1 Replies

8. Shell Programming and Scripting

How to use while loop in bash shell to read a file with 4 lines of gap

Hi , I am currently using the while loop in bash shell, as follows. while read line do echo $line done < file.txt However, i want to use the while loop on file.txt, which will read the file with 4 lines of gap. Ex- if file.txt is a file of 100 lines, then i want to use the loop such... (3 Replies)
Discussion started by: jitendriya.dash
3 Replies

9. Shell Programming and Scripting

exclude lines in a loop

I use while do - done loop in my shell script. It is working as per my expectations. But I do not want to process all the lines. I am finding it difficult to exclude certain lines. 1) I do not want to process blank lines as well as lines those start with a space " " 2) I do not want to... (2 Replies)
Discussion started by: shantanuo
2 Replies

10. Shell Programming and Scripting

Loop through file and write out lines to file(s)

Hi, I am new to ksh scripting so your help will be much appreciated. I have a file called file.txt which looks like this Header 20050702 20050703 ABC DEF Header 20050703 20050704 123 456 Header 20050704 20050705 XXX YYY What I am trying to do is write out each of the record... (7 Replies)
Discussion started by: Jtrinh
7 Replies
Login or Register to Ask a Question