Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 6 down vote accepted

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
share|improve this answer
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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