awk and greping between lines

i want to grab lines from a file that are between two patterns (including the lines that contain the pattern).

here's what im currently doing:

data.txt

aaa
bbb
cccc
ddd
eee
ffff
ddd

code:

awk '/bbb/,/fff/ && $0 !~ /ddd/' cdsnmp.sh

I want to grab lines between and including bbb and ffff, but i want to avoid the lines containing ddd

Hello SkySmart,

Could you please try following and let me know if this helps you.

awk '($0 ~ /ddd/){next} ($0 ~ /bbb/){A=1} {if(A){print}} ($0 ~ /ffff/){A=0}'  Input_file

Output will be as follows.

bbb
cccc
eee
ffff

Thanks,
R. Singh

2 Likes

Try (which I think is simplest / the most straight forward):

awk '/bbb/,/fff/{if(!/ddd/) print}' file

or otherwise:

awk '/bbb/{p=1} p && !/ddd/; /fff/{p=0}' file

----
@ravindersingh: your approach could be further reduced to:

awk '/ddd/{next} /bbb/{A=1}A; /ffff/{A=0}' file
2 Likes

In sed:

 sed -ne '/ddd/d;/bbb/,/fff/p' file
1 Like

Hi.

Also:

perl -wn -e 'if ( /bbb/../fff/ ) { print unless /ddd/ } ;' input-file

producing:

bbb
cccc
eee
ffff

On a system:

OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.4 (jessie) 
perl 5.20.2

Best wishes ... cheers, drl

The shortest awk resolution:

$ awk '/ddd/{next}/aaa/,/fff/' data.txt
aaa
bbb
cccc
eee
ffff
1 Like