0

I have a perl oneliner:

echo -e 'sla\ndfjAAAAk\nsaljdfla\nsjdfBBBBsadf\njasdf' | perl -E 'my $_ = do { local $/; <> }; say join qq!\n!, m!\QAAAA\E\K(.*?)(?=\QBBBB\E)!gs'
k
saljdfla
sjdf

it works, but I need this output:

dfjAAAAk
saljdfla
sjdfBBBBsadf

so I need the lines that contains "AAAA" and "BBBB" too - not just "from" and "to" them. How can I do this?

6

You want the range operator. You don't need to "slurp" your file either.

STR='sla\ndfjAAAAk\nsaljdfla\nsjdfBBBBsadf\njasdf' 
echo -e ${STR} | perl -nE 'm{^dfjAAAAk}..m{^sjdfBBBB} and print'
dfjAAAAk
saljdfla
sjdfBBBBsadf
  • 1
    +1. or print if /AAAA/../BBBB/ is a little tidier IMO. – glenn jackman Sep 24 '12 at 13:41
  • perl -nE 'print if /AAAA/../BBBB/' works :D – gasko peter Sep 25 '12 at 4:44
  • 1
    @gaskopeter TMTOWTDI – JRFerguson Sep 25 '12 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.