-   Programming (
-   -   sed: delete last line matching a pattern (

colucix 03-27-2011 01:15 PM

sed: delete last line matching a pattern
Hi all,

I have a question about sed programming, actually a one-liner for which I cannot find a solution, right now. I need to delete a line matching a specific pattern only if it is the last line. In practice, I would put together the following:

#  This deletes the last line of a file

sed -i \$d file
#  This deletes any line matching the pattern

sed -i /pattern/d file

Real example: the last lines of the original file are

2011-03-24 12:00  2570.13
2011-03-25 12:00  2285.03
2011-03-26 12:00  2109.07

Th following code deletes the second line from the last:

sed -i /$(date -d "$date" +%Y-%m-%d)/d file

in this case I want to avoid the deletion, since the pattern is not in the last line. How can I accomplish this? Thank you.

druuna 03-27-2011 01:28 PM



sed '${/'$(date -d $date +%Y-%m-%d)'/d}' infile
[address]{ command1 }

Hope this helps.

colucix 03-27-2011 01:41 PM

Great! I didn't know you can put an address inside the brackets (or maybe is the pattern part of the delete command?). I should re-read the grymore's tutorial! ;) Thanks, druuna! :)

druuna 03-27-2011 02:00 PM

You're welcome :)

All times are GMT -5. The time now is 05:33 PM.