Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

new to perl

by kroz (Initiate)
on Jul 13, 2012 at 13:36 UTC ( #981621=perlquestion: print w/ replies, xml ) Need Help??
kroz has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I'm new to perl but i'm learning.
I want to create a perl that reads a file, gets the last word from each line and executes a command like so:

File being read

-rw-r--r-- 1 134529024 Jul 12 15:51 My_file1.TMP -rw-r--r-- 1 1036349440 Jul 13 11:03 myfile2.TXT
and so on

output desired:
Mypath\MyPerl_2.pl My_file1.TMP Mypath\MyPerl_2.pl myfile2.TMP
where Myperl_2.pl is another perl routine (that i didn't write) that i am running

I know this can be done with regex but i can't yet get my head around it
Can you help with the syntax - would greatly apreciate it.
Thank you

Comment on new to perl
Select or Download Code
Re: new to perl
by brx (Pilgrim) on Jul 13, 2012 at 14:09 UTC

    Quick and dirty answer:

    ls -l *.TMP | perl -anle 'print "Mypath\\MyPerl_2.pl $F[-1]"'

    You can replace 'print' by system to execute commands. Caveat: if a filename contains a whitespace, this oneliner is wrong.

    Here, the real code executed (oneliners are not easy for beginners):

    $ perl -MO=Deparse -anle 'print "Mypath\\MyPerl_2.pl $F[-1]"' BEGIN { $/ = "\n"; $\ = "\n"; } LINE: while (defined($_ = <ARGV>)) { chomp $_; our(@F) = split(' ', $_, 0); print "Mypath\\MyPerl_2.pl $F[-1]"; }

    As you can see, it use split.

    You could want to use open to open a file.

    http://perldoc.perl.org/perlop.html#I%2fO-Operators to understand while ( ... = <>)

    And see how regex works in perlretut...

      Oneliner - didn't work, i already had a pipe in my command and i didn't bother to find out why.
      The deparsed command was great, worked like a charm - Thank you!

Re: new to perl
by monsoon (Pilgrim) on Jul 13, 2012 at 14:44 UTC
    perl -e 'while(<>){ @words = split /\s/; print "Mypath\\MyPerl_2.pl @words[-1]\n"; }' inputfile.txt

      Tried this, didn't work - most likely i've done something wrong. Thank you for the help

Re: new to perl
by zentara (Archbishop) on Jul 13, 2012 at 15:12 UTC
    I don't know if anyone noticed, but in the sample file given, there was a space between the last word and the bracketed br in the first line, and no space in the second. I don't know if it was a typo, or if it was an intentional fine point. In any event, it screwed up the spilt on space to array, giving 1 extra element.

    There may be a clever regex to do this, but here is a simple way a beginner can understand.

    #!/usr/bin/perl use warnings; use strict; open (my $fh, "< test.txt") or die "$!\n"; #input file open (my $oh, "> $0-out.txt") or die "$!\n"; #output file my $script = '/home/whoever/bin/myscript.pl'; while (<$fh>){ my $string = $_; # strip off trailing <br> and anything after it $string =~ s/<br>.*$//; #strip whitespace at end in case space preceded the <br> $string =~ s/\s+$//; ## split on space my @words = split / /, $string; #print join "\n",@words,"\n"; my $lastword = $words[-1]; print $oh "$script $lastword\n"; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      The bracket was not intentional - i must have missed it when i was writing the post. There is no space or bracket there.

Re: new to perl
by davido (Archbishop) on Jul 13, 2012 at 17:28 UTC

    Helping with the syntax would involve looking at what code you've already produced and discussing where to make changes to get it working. Where's the code that represents your best effort so far?


    Dave

      It was pretty much a oneliner for which i was searching for regex, not much code behind it yet. A lot of thinking though :)
      The idea is simple really, list the contents of a server to a file and then delete the list.
      All in all, thank you for all the help - job is done :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://981621]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2014-09-24 03:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (245 votes), past polls