1

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.

Ryan Griffith
  • 1,591
  • 17
  • 41
  • 3
    See [What is the best way to slurp a file into a string in Perl?](http://stackoverflow.com/questions/206661/what-is-the-best-way-to-slurp-a-file-into-a-string-in-perl) – Håkon Hægland Dec 21 '15 at 23:16
  • @HåkonHægland, please see my update. Thank you for your input but unfortunately, not what I'm looking for. – Ryan Griffith Dec 22 '15 at 00:42
  • *"where I can perform a regex against the final output of my cat command"* I'm not sure what this means. Do you mean the *last* line of the piped data? – Borodin Dec 22 '15 at 01:06
  • I read your update as saying that you're looking for a way to perform a match against streaming input (e.g. buffered input from a pipe) while taking into account that a matching string may end up split across buffer boundaries. Is that correct? – Slade Dec 22 '15 at 04:41
  • @Slade that's how I took it too. Ryan, if you're considering an HTTP connection that is always open (for instance a socket), then what you're desiring may not be desired unless you aggregate your stream and continually search the aggregation at some interval or event. Imagine a chat log, where the chat is on-going. You could certainly use a multi-line flag (`//s`), but that'd get expensive if your string is always starting from the beginning. You need another mechanism to control the text string, but I think you're looking for that `s` modifier (http://perldoc.perl.org/perlre.html#Modifiers). – vol7ron Dec 22 '15 at 07:38
  • I should add, if you don't aggregate the string, you will only be searching the buffer. Which means you may have text you want to match, but spans the two buffers. I'm not sure of how to search that, without aggregating/concatenating the string. – vol7ron Dec 22 '15 at 07:43
  • Can you give some example input and output? – Sobrique Dec 22 '15 at 07:53

1 Answers1

1

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'
Boying
  • 1,404
  • 13
  • 20