LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 05-29-2013, 01:06 PM   #1
Mann_engg
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 .

Example:
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 01:44 PM.
 
Old 05-29-2013, 01:13 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200
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, 06: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: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
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:

Code:
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.
Code:
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 07:00 PM. Reason: minor code alteration
 
  


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



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 05:25 AM
BASH: replace strings in on file by the strings in another one cristalp Programming 5 10-28-2011 10:47 AM
how to replace occurrence of a string in many files in one command tkmsr Programming 1 10-30-2010 07:29 AM
SED replace string by occurrence uttam_h Programming 5 03-05-2008 11:02 PM
Replace every other occurrence of pattern Wynd Linux - General 8 12-14-2005 04:43 PM


All times are GMT -5. The time now is 04:15 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration