Tuesday, March 1, 2011

Why does my perl one-liner ignore the first line of input?

I'm trying to strip some content from an HTML file automatically, and I'm using the following command to strip everything up to the useful data:

perl -pi.bak -e 'undef $/; s/^.*?<pre>//s' $file

However, for some reason this leaves the first line of the HTML file (the DOCTYPE declaration) alone.

From stackoverflow
  • By the time you undef $/, the first line has already been read. Use the -0 option to set $/ before anything has been read.

    perl -p0i.bak -e 's/^.*?<pre>//s'
    
    hobbs : Supposing that the `-0` switch wasn't there you could do `-e 'BEGIN { undef $/ } s/^.*?
    //s'`. 
                                        
    Chris Johnsen : Yes, a `BEGIN` can also do the work early enough.

0 comments:

Post a Comment