LinuxQuestions.org
Review your favorite Linux distribution.
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 12-19-2016, 07:13 AM   #16
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,772

Rep: Reputation: 798Reputation: 798Reputation: 798Reputation: 798Reputation: 798Reputation: 798Reputation: 798

Regarding sed
The /2 option for the s command does not work, because the rest of the line is absorbed by the first match, so the 2nd match does not apply.
However, the \(...\)\{n\} reference can do it!
Code:
sed 's/\([^"]*\("[^"]*"\)\)\{1\}.*/\2/' sampleLine
"Appendix"
sed 's/\([^"]*\("[^"]*"\)\)\{2\}.*/\2/' sampleLine
"Full Program Listings"
The \2 references the inner \( \)
 
Old 01-03-2017, 03:27 AM   #17
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,183

Original Poster
Rep: Reputation: 90
(From the Sed and Awk book)
Quote:
Substitution
We have already demonstrated many uses of the substitute command. Let’s look carefully at its syntax:
[address]s/pattern/replacement/flags
where the flags that modify the substitution are:
n A number (1 to 512) indicating that a replacement should be made for only the n th occurrence of the pattern.
It's true that it wouldn't be the nth occurrence in the whole file, but it works for each line where there's a match.
 
Old 01-03-2017, 07:00 AM   #18
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,821

Rep: Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084
Quote:
Originally Posted by vincix View Post
(From the Sed and Awk book)


It's true that it wouldn't be the nth occurrence in the whole file, but it works for each line where there's a match.
Not sure what value this post was with regard to the original question??
 
Old 01-03-2017, 07:08 AM   #19
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,183

Original Poster
Rep: Reputation: 90
The discussion evolved. In the 5th post and later there was the question of how to match a certain occurrence, but it was much simpler than expected with sed. That's the value with regard to the whole discussion.
 
  


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] regex pattern matching, with open braces or quotes being closed ansh007 Linux - General 11 08-31-2016 02:10 AM
Find/grep command to find matching files, print filename, then print matching content stefanlasiewski Programming 9 06-30-2016 05:30 PM
How to show selected string using grep from file and replace it with new input string prasad1990 Linux - Software 2 03-19-2015 08:02 AM
[SOLVED] Matching double quotes " hattori.hanzo Programming 7 11-24-2010 12:49 AM
grep command and quotes metalenkist Linux - Newbie 4 12-16-2009 05:32 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:34 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
Open Source Consulting | Domain Registration