Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 03-21-2012, 10:07 AM   #1
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

When I used sed e.g.
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.

Old 03-21-2012, 10:13 AM   #2
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian except for netbook still on Slackware 13.37 (and I have to support Ubuntu)
Posts: 8,572
Blog Entries: 30

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

Original Poster
Rep: Reputation: 16
That did it! thanks!
Old 03-21-2012, 02:47 PM   #4
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954
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.

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:

And more about using sed here:

Last edited by David the H.; 03-21-2012 at 02:51 PM. Reason: minor rewording


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] Need help with string manipulation. (sed or awk) beckss Programming 7 03-06-2012 11:56 AM
Need to cut the string in the result generated by AWK naveensankineni Programming 4 08-05-2010 11:26 AM
cut part of a string using awk m4rtin Programming 2 09-03-2009 08:32 PM
Using sed/awk to replace a string at a given position in anoopvraj Linux - Newbie 6 05-30-2009 08:59 AM
Remove everything up to the last numbers of a string w/ sed or awk OutThere Linux - General 4 04-23-2009 08:01 PM

All times are GMT -5. The time now is 11:02 PM.

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