CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
how to run command on each line of file

 



regex2012
User

Sep 22, 2015, 7:57 AM

Post #1 of 4 (905 views)
how to run command on each line of file Can't Post

I am trying to find out how to run a command on each line of a file.
There is a file, outputfile.txt with 4 lines in it. I am trying to get the command line command /usr/bin/reportlist to run on each line in that file. I know how to do this in bash, but not in perl. Can someone point me in the right direction?
Here's an example

Code
$dir = "/tmp/dev_rmv"; 
my $file = </tmp/dev_rmv/outputfile.txt>;
open(my $outfh1, '>', '/tmp/dev_rmv/outputfile3.txt') or die "cannot open file";
do
`/usr/bin/reportlist "$LINE"`
#print $outfh1 "$LINE\n";
}
close $outfh1



FishMonger
Veteran / Moderator

Sep 22, 2015, 8:12 AM

Post #2 of 4 (901 views)
Re: [regex2012] how to run command on each line of file [In reply to] Can't Post

You need to open outputfile.txt in read mode, then use a while loop to loop over it line-by-line and within each iteration execute your reportlist program.

If you don't need to capture the output of that command, then execute it via the system() function instead of ` ` backticks.

Based on your code here and in your other question, it's clear that you don't know perl. You really should do a little study. The best beginner level book is "Learning Perl, 6th Edition".
http://shop.oreilly.com/product/0636920018452.do


regex2012
User

Sep 22, 2015, 8:12 AM

Post #3 of 4 (899 views)
Re: [FishMonger] how to run command on each line of file [In reply to] Can't Post

Thanks for helping!


regex2012
User

Sep 22, 2015, 9:17 AM

Post #4 of 4 (889 views)
Re: [regex2012] how to run command on each line of file [In reply to] Can't Post

actually I found already the answer in the pages of the book!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives