http://qs1969.pair.com?node_id=1023272

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 filter.pl temp.txt file.txt; rm temp.txt

    filter.pl 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