[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: regex question



On 04/13/2008 07:00 AM, Alex Samad wrote:
Yes I did (thanks twice), but my question which arose from this was why
this did not work
perl -nle 'next if ( /^\s*;/);  print' sip.conf

why do I need the length statement[?]

perl -nle 'next if ( /^\s*;/);  print  if length $_ > 0' sip.conf

[...]

First, this might be easier to understand:

perl -nle 'print unless /^\s*(#|$)/' sources.list
(Replace "#" with ";".)

Secondly, the "length" statement was needed for your earlier code
because /^\s*;/ won't match blank lines.

Take this code:
perl -nle 'next if ( /^\s*#/);  print  if length $_ > 0' sources.list

Simplify it a bit and use more regex's:
perl -nle 'next if (/^\s*#/ or /^\s*$/); print' sources.list

Integrate the regex'es into a single one:
perl -nle 'next if /^\s*(#|$)/; print' sources.list

Eliminate "next":
perl -nle 'print unless /^\s*(#|$)/' sources.list


HTH



Reply to: