LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-12-2009, 11:04 AM   #1
gxw
LQ Newbie
 
Registered: Apr 2007
Posts: 6

Rep: Reputation: 0
Question How do I search for the last occurence of a string?


Hi,

I have a file I need to do a fully scripted update to. Basically I need to insert a block of code AFTER the last occurrence of a particular string.

How would I go about doing that with sed or awk for example?

Any advice appreciated,
Thanks.
 
Old 08-12-2009, 11:35 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Code:
num=$(grep -n <pattern> filename|tail -n1|cut -d: -f1);sed "$num a <string>" filename
replace <pattern>, <filename>, and <string> with your stuff.

$10 says that someone will produce a better answer before 12 noon PDT.......
 
Old 08-12-2009, 10:47 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
No-one took the bet!! You could have made an easy $10.....

And no-one had a better idea.....

And the OP is gone........

What is the world coming to??......
 
Old 08-13-2009, 12:16 AM   #4
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
Smile

Quote:
Originally Posted by pixellany View Post
Code:
num=$(grep -n <pattern> filename|tail -n1|cut -d: -f1);sed "$num a <string>" filename
replace <pattern>, <filename>, and <string> with your stuff.

$10 says that someone will produce a better answer before 12 noon PDT.......
Code:
num=`awk '/<pattern>/ { print NR }' filename | tail -1`; sed "$num a <string>" filename
Three commands is better than four. What a minute is three better than four? I know I still lost the bet'
 
Old 08-13-2009, 07:58 AM   #5
gxw
LQ Newbie
 
Registered: Apr 2007
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks to both of you. I'm sure the 1st one would work fine too, but I actually implemented the following and it worked a treat:

num=`awk '/<mystring>/ { print NR }' myfile | tail -1`
sed "$num a <my 1st Line>\n<my 2nd line>" <myfile >mynewfile

The us of NR and then tail is obviously the key, so thanks as that just didn't occur to me to do it that way!
 
Old 08-13-2009, 08:14 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
No....the key is to use SED, and then add kludges on top of it until it works.......... (micxz just has better kludges)

My method was to start with the first command that I could think of that would allow me to get to the last instance of the pattern---very scientific

micxz;
Noone took the bet, so there's nothing to win or lose!! (Actually, there's a loophole.....)
Yes, your version is better------fewer characters to type and surely runs MUCH faster than mine......
 
Old 08-13-2009, 04:33 PM   #7
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
Quote:
Originally Posted by pixellany View Post
micxz;
Noone took the bet, so there's nothing to win or lose!! (Actually, there's a loophole.....)
Your post was after the bet by time?
 
Old 08-13-2009, 05:17 PM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I specified a time deadline but no date........

But, now I've spent the $10 on something else........
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can you make search ...search a string in a link....a url...a web address aus9 LQ Suggestions & Feedback 4 04-16-2008 09:37 AM
C....Search a string for a string Scrag Programming 4 06-14-2004 04:15 PM
[c++] Search in a string hylke Programming 5 06-14-2004 11:20 AM
string search in C h/w Programming 17 10-13-2003 06:26 PM
Search for a string TheOriginalH Linux - General 5 09-24-2003 10:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:20 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration