LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   sed: delete last line matching a pattern (http://www.linuxquestions.org/questions/programming-9/sed-delete-last-line-matching-a-pattern-871302/)

colucix 03-27-2011 12: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:
Code:

#
#  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
Code:

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:
Code:

date=20110325
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 12:28 PM

Hi,

Code:

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

Hope this helps.

colucix 03-27-2011 12: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 01:00 PM

You're welcome :)


All times are GMT -5. The time now is 11:37 AM.