LinuxQuestions.org
Help answer threads with 0 replies.
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 01-08-2018, 04:56 AM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,032

Rep: Reputation: 76
understanding sed patterns


How are patterns in sed handled in this situation?

source: http://gilmation.com/articles/regexp...attern-in-sed/
The following example finds words surrounded by the same number (ab is surrounded by 1 and ef is surrounded by 82) and removes the surrounding number:

Code:
$ echo "4 9 1 ab 1 cd 13 82 ef 82" | sed 's/\([0-9]*\) \([a-z]*\) \1/\2 /g'
Code:
4 9 ab cd 13 ef

Last edited by vincix; 01-08-2018 at 05:05 AM.
 
Old 01-08-2018, 05:01 AM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,171
Blog Entries: 3

Rep: Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061
The substitute command is s/// as in

Code:
s/old/new/
So the \1 you have in your formula above is in the 'old' part. The 'new' part has only \2

So if you have a pattern along the lines of 'A B A' then it is replaced with 'B'
 
Old 01-08-2018, 05:02 AM   #3
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,171
Blog Entries: 3

Rep: Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061
Hey, don't delete stuff. Someone searching for a similar problem won't see the key part when this thread turns up in their search results.
 
Old 01-08-2018, 05:04 AM   #4
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,032

Original Poster
Rep: Reputation: 76
Sorry, I didn't know how to handle it It was actually much easier than I had thought and I deleted it before you replied, otherwise I wouldn't have deleted it. I'll just rewrite the post then.
 
Old 01-08-2018, 05:15 AM   #5
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,171
Blog Entries: 3

Rep: Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061
Thanks.
 
Old 01-08-2018, 05:19 AM   #6
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,032

Original Poster
Rep: Reputation: 76
I had interpreted the slash between \1 and \2 as the third occurrence, because I hadn't seen \1 in the first part of the substitution before, and I automatically thought that there was a another slash before \1. Thanks.
 
  


Reply

Tags
sed, sed regex


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
LXer: Understanding Docker Adoption Patterns LXer Syndicated Linux News 0 08-06-2017 12:36 AM
sed - using variable values as patterns danielbmartin Programming 7 01-23-2012 02:20 PM
[SOLVED] sed - interleaving remembered patterns danielbmartin Programming 4 01-23-2012 09:10 AM
Sed. Delete blank lines between two patterns supersoni3 Programming 5 07-29-2010 10:40 AM
Remembering patterns and printing only those patterns using sed bernie82 Programming 5 05-26-2005 05:18 PM

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

All times are GMT -5. The time now is 11:11 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration