I have one requirement. Let’s say my file contents are
a
b
c
d
e
f
d
e
f
I want to print the lines from b
to 2nd time repeated d
. Is there any command using awk to do this?
Output should be
b
c
d
e
f
d
If it is first time b
, I know the command
awk '/b/,/d/' file.txt
But I want to print the text to the matching point if it repeats second time.
I want to have the command with in one line.
Best Answer
One way is to use flags and counter to keep track
/b/{f=1; c=0}
set flag for starting match and initialize counterf;
print input record as long flag is set/d/ && ++c==2{f=0}
clear the flag if ending string is matched for second timeawk '/b/{f=1} f; /d/ && ++c==2{exit}'
if there is only one set to be extractedNote that if ending match doesn't have 2 matches, it will print until end of input