LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-02-2005, 11:16 AM   #1
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092
Blog Entries: 1

Rep: Reputation: 90
how can I run a command on each line of a file


Suppose a file has a list of files that I have to run a couple of commands onto.

How can I do that? maybe with a for?
 
Old 06-02-2005, 11:24 AM   #2
signalno9
LQ Newbie
 
Registered: Jun 2005
Posts: 7

Rep: Reputation: 0
Using linux/bash:

for i in `cat file`;do command $i;done
 
Old 06-02-2005, 11:50 AM   #3
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092
Blog Entries: 1

Original Poster
Rep: Reputation: 90
That makes very much sense. Now... consider the case when you have the filename and options to be sent to the command for every filename. Suppose it's a move. The original filename and the target name after it (separated by a space). Like

Code:
a.txt targeta.txt
b.txt /tmp/
c.txt whatever.txt
This is not exactly what I intend to do... but it's a simple example that I guess I will be able to customize to my needs.
 
Old 06-02-2005, 12:03 PM   #4
freegianghu
Member
 
Registered: Oct 2004
Location: somewhere in the street
Distribution: Window$
Posts: 192

Rep: Reputation: 30
Quote:
Originally posted by eantoranz
That makes very much sense. Now... consider the case when you have the filename and options to be sent to the command for every filename. Suppose it's a move. The original filename and the target name after it (separated by a space). Like

Code:
a.txt targeta.txt
b.txt /tmp/
c.txt whatever.txt
This is not exactly what I intend to do... but it's a simple example that I guess I will be able to customize to my needs.
Extract from
http://www.linuxquestions.org/questi...hreadid=230081
Code:
exec < $file
while read line
do
  echo $file, $line
done
Hope this helps
Giang Hu
 
Old 06-02-2005, 01:24 PM   #5
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092
Blog Entries: 1

Original Poster
Rep: Reputation: 90
Thanks for your help.

I'm trying to get this to work... but instead of running getting the lines from a file, I'm getting them from a grep invocation. It doesn't work. :'(

Code:
grep '^[ \t]*[a-zA-Z]' /etc/dhep/interfaces | exec
while read an_interface_line; do
        echo Interface Line: $an_interface_line
done
It dies there. The script doesn't advance. It's like it's idle. (It's a dhclient-script hook).

The file it's reading from has this content (just for testing purposes):
Code:
eth0
#eth1
eth2 weight 1
#eth3
eth4
 eth5
My guess is that it should print a line at a time. Any ideas what's wrong?
 
Old 06-02-2005, 01:57 PM   #6
signalno9
LQ Newbie
 
Registered: Jun 2005
Posts: 7

Rep: Reputation: 0
$cat test
eth0
#eth1
eth2 weight 1
#eth3
eth4
eth5

$grep '^[ \t]*[a-zA-Z]' test | while read line
> do
> echo Blah: $line
> done

Blah: eth0
Blah: eth2 weight 1
Blah: eth4
Blah: eth5
 
Old 06-02-2005, 02:00 PM   #7
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092
Blog Entries: 1

Original Poster
Rep: Reputation: 90
Man... I'm gonna build you a shrine at the backyard!
 
  


Reply


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
What can/can't run from the command line? redhat beginer Red Hat 4 04-20-2005 10:26 AM
How To Run Program From Command Line? neuropulse Linux - Newbie 2 04-16-2005 05:46 PM
Command to output file content line by line aznluvsmc Programming 2 09-12-2004 07:45 PM
how to run XF86Config from the command line Pacemaker Linux - Newbie 5 04-04-2004 04:39 AM
How to run XF86Config from command line Pacemaker Red Hat 2 04-03-2004 04:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:18 AM.

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