a perl question

Jonathan McKeown j.mckeown at ru.ac.za
Tue Jan 4 13:11:48 UTC 2011


On Tuesday 04 January 2011 12:32:00 S Mathias wrote:
> cat asdf.txt
> bla-bla
> bla-bla
> bla[XYZ]
> importantthing
> another important thing
> [/XYZ]
> bla-bla
> bla-bla
> [XYZ]
> yet another thing
> hello!
> [/XYZ]
> bla-bla
> etc.
> $ SOMEPERLMAGIC asdf.txt > output.txt
> $ cat output.txt
> importantthing
> another important thing
> yet another thing
> hello!

This could mean almost anything (witness another response which excludes lines 
containing blah or XYZ, which gives the desired output on your test input).

Are you actually trying to extract all the lines inside [XYZ]...[/XYZ] tags?

are the tags guaranteed not to occur on the lines you need to extract, as they 
appear here?

Because (all on one line)

perl -ne 'print if ($check = m{\[XYZ\]} .. m{\[/XYZ\]}) > 1 and 
$check !~ /E0$/' asdf.txt >output.txt

produces the same output as you have above for the test input. (The .. range 
operator in scalar context is true as soon as the left-hand expression is 
true, and false as soon as the right-hand expression is true. It returns 1 
each time it becomes true, incrementing integers as it stays true, and 
appends E0 to the last number as it becomes false, which lets you exclude 
both endpoints).

Jonathan



More information about the freebsd-questions mailing list