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 want to start grepping from the lines that has "CK" at the end of line and stop grepping when the line has "D" at the end. I tried grep "$CK" "$D" file..txt , but it didnt work.

Input:

kkkkkkkkkkk   
jjjjjjjjjjjjjjjjjj  
gggggggggggg/CK  
JHGHHHHHHHH   
HJKHKKLKLLL   
JNBHBHJKJJLKKL  
JLKKKLLKJLKJ/D  
GGGGGGGGGGGGGG  
GGGGGGGGGGGGGG

The desired output

gggggggggggg/CK  
JHGHHHHHHHH   
HJKHKKLKLLL   
JNBHBHJKJJLKKL  
JLKKKLLKJLKJ/D
share|improve this question

1 Answer 1

You are better off using or

awk '/CK$/,/D$/' file.txt

OR

sed -n '/CK$/,/D$/p' file.txt

If you insist on , here's a GNU grep way

grep -oPz '(?s)(?<=\n)\N+CK\n.*?D(?=\n)' file.txt

Here

-P activates perl-regexp

-z sets line separator to NUL. This forces grep to see the entire file as one single line

-o prints only matching

(?s) activates PCRE_DOTALL, so . finds any character or newline

\N matches anything except newline

.*? finds . in nongreedy mode

(?<=..) is a look-behind assertion

(?=..) is a look-ahead assertion

share|improve this answer
    
why not added slash ? like this awk '/\/CK/,/\/D/' input –  Rahul Patil Nov 8 '13 at 17:37
    
@RahulPatil, that's because the OP stated 'start grepping from the lines that has "CK" at the end of line and stop grepping when the line has "D"' –  1_CR Nov 8 '13 at 17:38
    
it seems me, you have strong understanding of regex.. I didn't understand that level, so could you please explain that last of PCRE grep –  Rahul Patil Nov 8 '13 at 17:41
    
@RahulPatil, added some explanation :-) –  1_CR Nov 8 '13 at 18:14
    
@RohitPatil, I want to delete the matching selection from the file. can i do that? –  Rana Khan Nov 8 '13 at 18:18

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.