LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-27-2011, 12:15 PM   #1
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,362

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
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.
 
Old 03-27-2011, 12:28 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

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

Hope this helps.
 
1 members found this post helpful.
Old 03-27-2011, 12:41 PM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,362

Original Poster
Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
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!
 
Old 03-27-2011, 01:00 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
You're welcome
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Pattern matching with sed vinaytp Linux - Newbie 3 05-24-2010 07:33 AM
sed: pattern matching with newlines anjanesh Linux - General 2 02-20-2009 06:36 AM
SED - Delete line above or below as well as matching line... OldGaf Programming 7 06-26-2008 11:51 PM
Sed pattern matching digitalbrutus Programming 1 08-20-2006 01:37 PM
pattern matching problem in sed digitalbrutus Programming 4 08-20-2006 04:40 AM


All times are GMT -5. The time now is 12:35 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration