Remove all lines between two strings
问题 In a sh shell script. Given data in a text file: string1 string2 gibberish gibberish string3 gibberish string4 How could you use awk or sed to remove all lines between string2 (inclusive) and string3 (not including string3 )? to end up with: string1 string3 string4 回答1: you can try this. Anything before "string2" will not be deleted. awk 'BEGIN{f=0} { match($0,"string2") if(RSTART){ print substr($0,1,RSTART-1) f=1 next } match($0,"string3") if(RSTART){ $0=substr($0,RSTART) f=0 } } f==0{print}