Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

How to handle both a STDIN pipe and file from command line?

by keithw99 (Initiate)
on Mar 13, 2013 at 17:49 UTC ( #1023272=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to use Perl in a command-line fashion where I'm getting a list from a STDIN pipe and using those values to perform a regex substitution on a file. In a script, I know that I can create a different file handle for each input source, but how would I do it at the command line?

In the example below, suppose I have an executable command "print_list" that prints a list of words to STDOUT (separated by newlines), and I want Perl to search through a file called file.txt and replace all instances of these words (from the piped list) with the string "foo". The following command does *NOT* work, but I wanted to at least illustrate what I'm trying to do:

./print_list | perl -i -ne 's/\Q$_\E/foo/g' file.txt

The problem here is that I'm confusing Perl with input sources. There's a STDIN pipe coming from the left, and there's a file argument coming from the right. I would like the '$_' variable to come from the pipe, but when I add the 'file.txt' argument, the '$_' variable gets its value from the file instead. I'm also curious how to control which source the '<>' variable represents.

Is there any way this is even possible?

(if you're wondering about the \Q..\E in the regex, it's because the words may contain many special characters that would be misinterpreted as regex operators, so I need to escape them)

Replies are listed 'Best First'.
Re: How to handle both a STDIN pipe and file from command line?
by NetWallah (Canon) on Mar 13, 2013 at 19:25 UTC
    try something like this:
    echo -e "One\nTwo\nother\mmore\nthan\nthat" | perl -ne 'BEGIN{chomp (@ +arr=<STDIN>);$x=join("|",@arr)};s /$x/FOOOO/; print "FILE:$_" ' file. +txt

                 "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
            -- Dr. Cox, Scrubs

Re: How to handle both a STDIN pipe and file from command line?
by ramlight (Friar) on Mar 13, 2013 at 19:40 UTC

    If you're going for the minimum in terseness; you can always use a temporary file:

    ./print_list >/temp/temp.txt; perl temp.txt file.txt; rm temp.txt would read from the first file, then use the contents to substitute in the second file:

    use strict; use warnings; open(my $file1, '<', $ARGV[0]) or die "Can't open $ARGV[0]: $!\n"; my @substitutions = <$file1>; close($file1); chomp(@substitutions); open(my $file2, '<', $ARGV[1]) or die "Can't open $ARGV[1]: $!\n"; while(my $line = <$file2>) { for my $replace (@substitutions) { next unless $replace; # skip blanks $line =~ s/\Q$replace\E/foo/g; } print $line; } close($file2);
Re: How to handle both a STDIN pipe and file from command line?
by Anonymous Monk on Mar 14, 2013 at 04:04 UTC

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2022-10-04 22:04 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (19 votes). Check out past polls.