Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-19-2005, 04:39 PM
|
#1
|
|
Member
Registered: Nov 2003
Distribution: RHEL 5.5
Posts: 390
Rep:
|
Reading a file and running a command for each line.
Can anyone give me an example of how to run an argument past each line of a file and run a command? For example...
I have a file with these lines in it at /home/mijohnst/filelist.txt
# A list of Files
/home/mijohnst/dir1/testfile1.sh
/home/mijohnst/dir2/testfile2.sh
/home/mijohnst/dir3/testfile3.sh
/home/mijohnst/dir4/testfile4.sh
/home/mijohnst/dir5/testfile5.sh
/home/mijohnst/dir6/testfile6.sh
What command would I run to read each line of this file and run a command? Say that I wanted to copy all of those files into the /tmp directory...
I'm thinking something like "cat /home/mijohnst/filelist.txt | ..." something...
I know this is easy, but I'm really drawing a blank...
Thanks in advance!
|
|
|
|
08-19-2005, 04:53 PM
|
#2
|
|
Senior Member
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,591
|
cat /home/mijohnst/filelist.txt | awk '{print "somecommand",$1}' | bash
|
|
|
|
08-19-2005, 05:07 PM
|
#3
|
|
Member
Registered: Nov 2003
Distribution: RHEL 5.5
Posts: 390
Original Poster
Rep:
|
Thanks macemoneta! For some reason I don't remember doing this at all...
|
|
|
|
08-19-2005, 05:22 PM
|
#4
|
|
Senior Member
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,591
|
There are probably a dozen ways (or more) to accomplish the same thing. I prefer to remember the use of a few commands (grep, cut, paste, sed, awk, tr), and find that I can accomplish almost anything I need with them. There are more elegant ways to do things, but they tend to use "one trick ponies" instead of more flexible tools.
|
|
|
|
08-19-2005, 05:55 PM
|
#5
|
|
Member
Registered: Nov 2003
Distribution: RHEL 5.5
Posts: 390
Original Poster
Rep:
|
Well, Linux hasn't been my main OS until recently so I haven't mastered these commands yet. I'm learning as I go thanks to people like you.
I'm trying to understand the flow of the command you just showed me, but I guess that I'm thinking of it wrong.
When I do:
cat /home/mijohnst/filelist.txt | awk '{print "cp /tmp",$1}' | bash
I get an error for each line that says "Omitting /tmp" and nothing is copied... Did I misunderstand what you were trying to tell me?
|
|
|
|
08-19-2005, 09:42 PM
|
#6
|
|
Senior Member
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,591
|
The command, as you entered it, is trying to do the following:
cp /tmp /home/mijohnst/dir1/testfile1.sh
...
Leave off the last pipe (" | bash") to see the results before you try to execute.
If you want to copy each of the files to /tmp, then you need to rework the pipe to:
cat /home/mijohnst/filelist.txt | awk '{print "cp",$1,"/tmp"}' | bash
Each entry in the file /home/mijohnst/filelist.txt is passed to awk. It will print "cp" a space (because of the comma) the first space delimited word from the file (for example, "/home/mijohnst/dir1/testfile1.sh") another space and then "/tmp".
Run this instead:
cat /home/mijohnst/filelist.txt | awk '{print "cp",$1,"/tmp"}'
Without the final bash command, the output of awk will simply be displayed at the terminal. Once you have the commands the way you want them, you can send them to bash for execution by adding the pipe to a bash shell " | bash".
|
|
|
|
08-20-2005, 09:42 AM
|
#7
|
|
Senior Member
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802
Rep:
|
for i in `cat filelist`;do echo "$i";done
Replace (echo "$i") with the command you want to run.
I use this command all the time to work on multiple files.
|
|
|
|
08-21-2005, 10:20 AM
|
#8
|
|
Member
Registered: May 2005
Posts: 378
Rep:
|
Quote:
Originally posted by macemoneta
cat /home/mijohnst/filelist.txt | awk '{print "somecommand",$1}' | bash
|
Why are you guys so obsessed with cat()ing files into filters that take filename arguments?
Code:
awk '{print "somecommand",$1}' /home/mijohnst/filelist.txt | bash
Anyway, if the file only has the commands in it (as posted)
Code:
bash </home/mijohnst/filelist.txt
is the easiest way to run it.
|
|
|
|
08-21-2005, 02:49 PM
|
#9
|
|
Senior Member
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802
Rep:
|
Quote:
eddiebaby1023
Anyway, if the file only has the commands in it (as posted)
bash </home/mijohnst/filelist.txt
is the easiest way to run it.
|
The way I read the question
Quote:
I have a file with these lines in it at /home/mijohnst/filelist.txt
# A list of Files
/home/mijohnst/dir1/testfile1.sh
/home/mijohnst/dir2/testfile2.sh
/home/mijohnst/dir3/testfile3.sh
/home/mijohnst/dir4/testfile4.sh
/home/mijohnst/dir5/testfile5.sh
/home/mijohnst/dir6/testfile6.sh
What command would I run to read each line of this file and run a command? Say that I wanted to copy all of those files into the /tmp directory...
|
mijohnst doesn't want to run all the scripts, but wants to run a command on each one, such as copy them to the /tmp directory
|
|
|
|
08-22-2005, 11:54 AM
|
#10
|
|
Member
Registered: Nov 2003
Distribution: RHEL 5.5
Posts: 390
Original Poster
Rep:
|
That's exactly right /bin/bash.
Thank you guys so much! Your help has provided the exact means of getting my job done and it works!
I need a good linux command example site for newbies so that I don't have to post so many dumb questions. Thanks again all!
|
|
|
|
08-22-2005, 12:11 PM
|
#11
|
|
Moderator
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,505
Rep: 
|
I think this is simpler, and allows for comments in the source file:
Code:
egrep -v '^\W*#' /SOME/FILE | xargs -i cp {} /tmp
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:00 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|