How to run a perl file in a terminal pipeline -


probably simple question: have perl file sed.perl takes input string, makes substitutions there , prints on standard output.

#!/usr/bin/perl use warnings; use strict; use diagnostics; use feature 'say'; #use cwd;  ($text) = @argv;   $text =~ s/\.\)\n/'\.'\)\n/;  print $text; 

i want feed script string output terminal pipeline. let's in way:

cat input.txt | perl sed.perl 

but doesn't work: use of uninitialized value $text in substitution (s///) at

using score symbol doesn't works either:

cat input.txt | perl sed.perl - 

@argv doesn't think does. it's literally arguments passed perl.

e.g. :

myscript.pl arg 

@argv host 'some', 'arg'.

what want stdin file handle.

e.g.

#!/usr/bin/perl use strict; use warnings;  while ( <stdin> ) {     s/something/somethingelse/g;      print; } 

now doing reading stdin line line. pattern includes \n. need it? looks you're 'just' using line anchor, , use:

s/\.\)$/'\.'\)/g; 

$ regex "end of line" - see perlre more.

however, noted in comments reinierpost - there's thing that's useful know - perl has "diamond operator" <> 2 things:

  • if filenames specified script, opens them , reads them.
  • if no arguments specified, reads stdin.

so do:

while ( <> ) {     s/something/somethingelse/g;      print; } 

and script can either invoked by:

cat input.txt | ./yourscript.pl 

or:

./yourscript.pl input.txt 

and you'll have same result.


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -