Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-29-2013, 12:06 PM   #1
LQ Newbie
Registered: May 2013
Posts: 4

Rep: Reputation: Disabled
Get nth occurrence of two strings and replace it

I want to use sed command for this .

Fruit apple banana test
Fruit mango banana test
Fruit mango test

I want 2nd occurrence of Fruit mango banana Fruit

sed -n '/fruit/,/test/p;/test/q' filename > output

This command gives me first occurrence

Desired Output :

Fruit mango banana test (2nd occurrence of fruit and test)

Last edited by Mann_engg; 05-29-2013 at 12:44 PM.
Old 05-29-2013, 12:13 PM   #2
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218
Hi and welcome to LQ.

I'm not sure I understand what your desired output should be. Can you post a desired output string? What exactly do you want the 2nd occurrence of?
Old 05-30-2013, 05:56 PM   #3
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques. Thanks.

Your requirements are still not clear, and the explanation doesn't match the posted text. Do you want the second line that starts with Fruit and ends with test? If so, I think this will do what you want:

sed -n '0,/^Fruit.*test/d ; /Fruit.*test/{p;q}' input.txt
It deletes the first match, then prints the next one and exits.

awk is actually a bit easier here, logically, if slightly longer, and you can use it to match any arbitrary entry. Just load the matching lines into an array and print the one you want.
awk '/^Fruit.*test/ { a[++n]=$0 } n==2 { quit } END { print a[2] }' input.txt
The n==2 test ends the processing after you've reached the line you're interested in.

Last edited by David the H.; 05-30-2013 at 06:00 PM. Reason: minor code alteration


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

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] replace the nth column date as MM/DD/YYYY anshaa Linux - Newbie 1 08-13-2012 04:25 AM
BASH: replace strings in on file by the strings in another one cristalp Programming 5 10-28-2011 09:47 AM
how to replace occurrence of a string in many files in one command tkmsr Programming 1 10-30-2010 06:29 AM
SED replace string by occurrence uttam_h Programming 5 03-05-2008 10:02 PM
Replace every other occurrence of pattern Wynd Linux - General 8 12-14-2005 03:43 PM

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

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