TARGET
How to delete all lines in a text file before a matching one, including this one?
Input file example:
apple pear banana HI_THERE lemon coconut orange Desired output:
lemon coconut orange The aim is to do it in sed to use the "-i" option (direct editing).
CLEAN SOLUTION ?
Most answers for similar problems propose something like:
sed -n '/HI_THERE/,$p' input_file But the matched line is not deleted:
HI_THERE lemon coconut orange Then, knowing this will delete all from matched line (including it) to end of file:
sed '/HI_THERE/,$d' input_file I tried something like this:
sed '^,/HI_THERE/d' input_file But then sed complains:
sed: -e expression #1, char 1: unknown command: `^' DIRTY SOLUTION
The last (dirty) solution is using pipeline:
sed -n '/HI_THERE/,$p' input_file | tail -n +2 but then, direct edit of the file doesn't work:
sed -n '/HI_THERE/,$p' input_file | tail -n +2 > input_file cat input_file # returns nothing and one must use a temporary file like that...
sed -n '/HI_THERE/,$p' input_file | tail -n +2 > tmp_file mv tmp_file input_file
1, not^. Does that work for you?