i want to delete all the lines after the last occurence of pattern except the pattern itself
file.txt
honor
apple
redmi
nokia
apple
samsung
lg
htc
file.txt what i want
honor
apple
redmi
nokia
apple
what i have tried
sed -i '/apple/q' file.txt
this deletes all the line after the first occurence of pattern -
honor
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…