in reply to elementary hash, STDIN questions

You are treading dangerously close to 1 liner territory here! I shall resist the temptation, though I predict others won't ;-)

Here is how I would do it :-

#!/usr/bin/perl -w -p -i.bak use strict; my $kill = join "|", qw{ wahwah blahblah yadayada }; while (<>) { s/$kill//go; # remove unwanted strings s/^\s+//g; # blank lines }
Pass a list of files you want changed in on the command line - this will edit them in place (the -i flag) saving the old ones with a .bak extension.

Replies are listed 'Best First'.
(jcwren) RE: Re: elementary hash
by jcwren (Prior) on Sep 12, 2000 at 02:03 UTC
    The final working version I came up with was:
    #!/usr/local/bin/perl -w -p -i.bak use strict; my $kill = join "|", qw{ wahwah blahblah yadayada }; s/$kill//go; # remove unwanted strings s/^\s+//g; # blank lines
    Without the -p option the working version was:
    #!/usr/local/bin/perl -w -i.bak use strict; my $kill = join "|", qw{ wahwah blahblah yadayada }; while (<>) { s/$kill//go; # remove unwanted strings s/^\s+//g; # blank lines print; }
    I've not used the -p option before, but -p forces printing at the end, while -n explicitly disables printing. -p will override -n, if both are present. 'perldoc perlrun' talks about these.

    --Chris

    e-mail jcwren
RE: Re: elementary hash
by chromatic (Archbishop) on Sep 12, 2000 at 03:01 UTC
    Here's a fun one-liner:

    perl -pi.bak -e 's/(^\s+$)?|blahblah|wahwah|yadayada//g' <filename>

    If you run it twice, it'll squash blank lines. The regex could be more robust, but I hadn't seen any one-liner yet.

    Update: I still don't understand tilly's followup, but let me explain what I mean a little further. My one-liner strips out the words mentioned above, but retains the line-ending newlines. tilly's dual-regex below fixes that. If you run mine twice, it'll get rid of the newlines (as the first part of the regex squashes whitespace). It has the (unlikely) side effect of getting rid of a string that, in the original file, would resemble 'blahyadayadablah'.

    Not that you should be doing this with a one-liner.... :)

      Actually that doesn't match the original code. If the entire text of the original was one of the matches, then it should be squashed. Here is a one-liner that works and is shorter, but only because of what the strings are:
      perl -pi.bak -e 's/(blah|wah|yada)\1//g;s/^\s+$//' $file
RE: (2) elementary hash (uninitialized value...)
by ybiC (Prior) on Sep 12, 2000 at 01:57 UTC
    Hmmm... responds with "Use of uninitialized value at line while (<>) {".   The .bak file is created, but the original is left as a zero-byte file.

    Perl 5.005.03
        cheers,
        ybiC

      I dumped the -p switch and put an explicit print statement in and that worked...but it seems like the -p _ought_ to work. I _think_ it doesn't, though, because the Camel book says -p puts an assumed loop around the script with a print in the equivalent of a continue block, so the print would only be seen once. Now, if you remove the while loop and just do this:
      #!/usr/bin/perl -p -i.bak my $kill = "what|ever"; s/$kill//go; s/^\s+//g;
      I believe it'll work..it's weird, but it works.