I have a text file containing this :-
# Comment
# Comment
# Comment
property1
# Comment
# Comment
property2
I wanted to use unix command (awk/sed etc.) to search for a pattern with property2 and then delete all the comments before it. Hence, after operation output should be :-
# Comment
# Comment
# Comment
property1
This is what I tried (using awk command) :-
awk -v pat='^property2' -v comment='^#' '$1~pat{p=NR} p && NR>=p-3{del=($1~comment)} del{next} 1' test.txt
Basically, the logic I tried to use was :-
- Search for property2
- and then loop over previous 3 lines
- Search if it is a comment (starts with #)
- Delete those lines (including the searched pattern and the comments above).
Can someone help me achieve this? Thanks.