Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: perl onliner on multiple files

by BrowserUk (Pope)
on Feb 08, 2013 at 19:44 UTC ( #1017883=note: print w/ replies, xml ) Need Help??


in reply to perl onliner on multiple files

This (untested) may do what you want:

perl -nE"/: / and $c+= (split/: /)[0]; if( eof(ARGV) ){ say qq[SUM.$AR +V =$c]; $c=0 }" tmp_*
  • -n:

    provides the while(<>){} loop.

  • the if( eof(ARGV) )

    checks for the end of each individual file, prints the results and resets $c to 0.

  • $ARGV:

    gives you the names of the individual files in the output.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re: perl onliner on multiple files
Select or Download Code
Replies are listed 'Best First'.
Re^2: perl onliner on multiple files
by abhay180 (Acolyte) on Feb 08, 2013 at 21:16 UTC
    When i run this i get following error mesg:
    % perl -nE "/: / and $c += (split/: /)[0]; if( eof(ARGV) ){ say qq[SUM +.$ARGV =$c]; $c=0 }" tmp_* c: Undefined variable.

      Not from Perl you don't. That must be coming from your shell.

      Try switching the quotes if you are on linux. Ie:

      perl -nE'/: / and $c+= (split/: /)[0]; if( eof(ARGV) ){ say qq[SUM.$AR +V =$c]; $c=0 }' tmp_*

      If you are a perl/*nix user, you ought to know that.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      In a bash shell, I get:
      $ perl -nE "/: / and $c += (split/: /)[0]; if( eof(ARGV) ){ say qq[SUM +.$ARGV =$c]; $c=0 }" tmp_* syntax error at -e line 1, near "+=" syntax error at -e line 1, near "; =" Execution of -e aborted due to compilation errors.

      And:

      $ c=10 $ echo c c $ echo $c 10 $ echo "The total is: $c" The total is: 10 $ echo 'The total is: $c' The total is: $c
      So within double quotes, the shell expands $variables, which of course happens *before* the shell hands the text over to the perl command. That is similar to the way the shell expands your glob: tmp_* and replaces it with a list of file names, which is what the perl command ends up seeing.
        ok. i figured that out...but since i was using perl version 5.8, the -E option and "say" keyword were not recognized. Once i use the perl version 5.10 and above...the above line works exactly the way i want it to. Thanks a lot.
      :) Then give up the oneliners, save it to a file

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (15)
As of 2015-07-07 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls