sed: delete lines after last occurrence of a pattern in a file
Hello,
I'm having trouble finding how to delete all lines after the last occurrence of a pattern. I know this deletes all lines after it finds PATTERN: sed '/PATTERN/q' file.in > file.out but if I have a file like: qwe PATTERN rty PATTERN uiop the result is: qwe PATTERN when in fact I want: qwe PATTERN rty PATTERN Can anyone tell me how I achieve this? I found lots of references with google to replacing the last occurrence of one word with another within a line, but not something like the case I have. Thanks! |
I'm not entirely sure that sed is the best tool for this job. The problem is that sed is so line-oriented, and you need to maintain state (at least at some level) to do this.
A quick stab at how I might do this (probably in Perl): track through the file line by line; every time I see PATTERN, note the line number (just one variable for this $last_seen); then rewind to the top of the file and only print back out up to the line number that I was left with. The general problem is that whatever tool you use has no obvious way of knowing whether the next line is another occurrence of PATTERN or EOF. An example, not very fancy: Code:
#!/usr/bin/env perl |
Thanks!
Hi Telemachos,
I suppose the difficulty of doing this with sed is why I was unable to find an appropriate sed-based solution with google. Thank you for your kind help and demonstrating a perl-based solution. In the end, taking note about what you said about "rewind", I have written a fortran program to selectively read in data, using the "backspace" command to go back through the file when needed. Thank you again! |
@OP,you should learn how to use gawk instead.
Code:
$ more file |
tac can simplify the problem for sed by temporally reversing the order of the lines.
The '0' address is a GNU sed extension and is needed here in case PATTERN is on line 1 of the reversed file. Code:
echo 'qwe Code:
echo 'qwe |
All times are GMT -5. The time now is 09:53 AM. |