Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Enclosing one-liner in a bigger loop - unix

by baxy77bax (Deacon)
on Apr 24, 2015 at 08:53 UTC ( [id://1124498]=perlquestion: print w/replies, xml ) Need Help??

baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:

hi
This is probably a well know thing but somehow I missed it. Let say I have the following oneliner:
perl -lne '/^([^\t]*)\t/; $hash{$1}++}{print $_ foreach(keys %hash)' i +nfile.txt
My question is : What would be the simplest way to apply this one-liner to each file in my directory: ls dir | for each file execute: perl -lne '/^([^\t]*)\t/; $hash{$1}++}{print $_ foreach(keys %hash)' thnx

Replies are listed 'Best First'.
Re: Enclosing one-liner in a bigger loop - unix
by hippo (Bishop) on Apr 24, 2015 at 08:56 UTC

    Just replace the filename with a wildcard such that your command line becomes

    perl -lne '/^([^\t]*)\t/; $hash{$1}++}{print $_ foreach(keys %hash)' *

    Is that all you were after?

      Ok, this is embarrassing. Thank you though... And the answer is yes

      UPDATE:

      Attempt to save the post.(Though I'll probably embarrass my self one more time)

      How would one do this if one would like to know what was matched in each file .
      Let the expected output be :

      file1: match match match file two: match match ...
      the same as grep function does. (Ano no, I don't want to use grep since I cannot hash)

        Look at $ARGV which contains the current file name.

        And you could avoid the eskimo kiss if you want with something like: perl -lne '/^([^\t]*)\t/ and not $h{$1}++ and print $1'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (8)
As of 2024-03-28 09:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found