Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-04-2008, 02:07 PM   #1
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Rep: Reputation: 33
shell script loop though text file:-1 line at a time.

Good day,

I have a file with ip address.
Code:     pc1     pc2     pc4
I have to do certain action on each of the ip address.

sleep 10
sleep 10
I know cat/awk/cut/grep treat one lie at a time, can those be used?
If yes how.

Old 05-04-2008, 02:21 PM   #2
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Without any of these programs, you can read a file line by line in your script using read and a while loop:
while read ip pc
   ping $ip
   sleep 10
done < <(cat testfile)
Here the read command reads two fields per line from the file and uses the value $ip for the subsequent commands. The content of the file is passed to the loop using "process substitution" but you can pipe it as well:
cat testfile | while read ......
Old 05-04-2008, 06:58 PM   #3
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
nice post colucix .. i'll take note of the separation of fields by adding another variable :-).. gotta use these with arrays !

btw you can also use for loops like this but here you'll also use cut:
IFS_=${IFS}; IFS=$'\n'
for ip in $(cut -f 1 testfile); do
   ping $ip
   sleep 10s
or perhaps since there are no spaces within the lines just do for
for ip in $(cut -f 1 testfile); do
   ping $ip
   sleep 10s

Last edited by konsolebox; 05-04-2008 at 06:59 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
How to read a single line from a text file into a shell script. SkipHuffman Linux - Software 2 08-16-2006 02:10 PM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 07:15 PM
Shell script - how to show a specific line of a text file davi_cabral Linux - Software 3 09-28-2004 01:39 PM
How to delete a line from a text file with shell script programming Bassam General 1 01-28-2004 08:51 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:36 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration