Ryan Griffith
Ryan Griffith

Reputation: 1640

Perl - Read input pipe as single variable

I have a very simple perl script which I perform some regex on the value of the data piped to my perl command. ex:

 cat /tmp/myfile.txt | perl -wnE"say for /my_pattern/gi"

Note, I do realize in my current setup that the -n option wraps my command ex:

while(<>){
    say for /my_pattern/gi
}

...thus iterating over each line of input.

I'd like to change this where I can perform a regex against the final output of my cat command. All examples I find show processing input line by line.

Any help would be appreciated.

Update:

To be clear, I'm referring to any pipe, not only reading from a file as in my example (think curl, wget, echo, etc...) I'm not sure it is even possible given the fact that the originating command could be long-lived or run for an "indefinite" period of time.

Upvotes: 1

Views: 576

Answers (1)

Boying
Boying

Reputation: 1434

To answer your question directly:

cat aaaa.txt | perl -ne 'BEGIN{local $/};print for /a/gi'

To get what your stuff work:

cat aaaa.txt | perl -ne ';print if /aaaa/gi'

Upvotes: 1

Related Questions