LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-21-2012, 09:07 AM   #1
StupidNewbie
Member
 
Registered: Dec 2007
Posts: 71

Rep: Reputation: 16
Sed/awk/cut to pull a repeating string out of a longer string


Hi everyone,

I'm looking for a sed or awk statement to do something like the following:

With a string like dc=some,dc=domain,dc=dot,dc=com I can use tr to change all the commas to dots very easily, but then I'm left with dc=some.dc=domain.dc=dot.dc=com. Now I want to remove all the "dc=" to be left with some.domain.dot.com.

When I used sed e.g.
Code:
sed 's/[dc=]//'
it did not replace all of the instances, and also is replacing the characters individually so if there is a "c" or a "d" anywhere in the domain name it will break it. I could use cut with "dc=" as the delimiter but cut doesn't support multi-character dlimiters.

The other caveat is that I am doing this for a list of strings like this, and they might not all have 4 domain components. Some might have 6, some might have just 2. It really is unknown how many there will be as it depends on the domain.

Can anyone offer some insight on how I can dynamically cut these things out? The ultimate goal is to attach this to the end of a url e.g. http://www.example.some.domain.dot.com.

Thanks!
 
Old 03-21-2012, 09:13 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
sed 's/dc=//g'
 
Old 03-21-2012, 09:46 AM   #3
StupidNewbie
Member
 
Registered: Dec 2007
Posts: 71

Original Poster
Rep: Reputation: 16
That did it! thanks!
 
Old 03-21-2012, 01:47 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
To clarify your mistake, the [] brackets in regular expressions constitute a list of individual characters that can be matched at that location. On its own the whole bracket expression matches only a single character on the line.

Code:
sed 's/^[lLbB]oobar/Foobar/g' infile
This will match any line starting with "loobar", "Loobar", "boobar", or "Boobar", and change that string to "Foobar". But it will not match "bBoobar" or any other multi-character combination.

Similarly. 's/[dc=]//' will match the first "d", the first "c", or the first "=" on the line (whichever is encountered first), and remove it. It will not match the whole string"dc=", although it could match one of the individual characters, if it's the first instance on the line.

I say only the first, because you need to add the "g" option at the end of the s/// command to do multiple substitutions on a single line.


I suggest you take some time to really learn how to use regular expressions. You'll be glad you did. Here are a couple of tutorials:

http://mywiki.wooledge.org/RegularExpression
http://www.grymoire.com/Unix/Regular.html


And more about using sed here:

http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

Last edited by David the H.; 03-21-2012 at 01:51 PM. Reason: minor rewording
 
1 members found this post helpful.
Old 09-13-2018, 03:41 AM   #5
DavidFW1960
LQ Newbie
 
Registered: Sep 2018
Distribution: Debian
Posts: 18

Rep: Reputation: Disabled
That was an awesome explanation. I learned a lot from that. Thank you.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Need help with string manipulation. (sed or awk) beckss Programming 7 03-06-2012 10:56 AM
Need to cut the string in the result generated by AWK naveensankineni Programming 4 08-05-2010 10:26 AM
cut part of a string using awk m4rtin Programming 2 09-03-2009 07:32 PM
Using sed/awk to replace a string at a given position in anoopvraj Linux - Newbie 6 05-30-2009 07:59 AM
Remove everything up to the last numbers of a string w/ sed or awk OutThere Linux - General 4 04-23-2009 07:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:03 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