Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need a little 'sed' help

Status
Not open for further replies.

awingnut

Programmer
Feb 24, 2003
759
US
I have a series of lines in which there is string data I need to extract that is enclosed in parens "()". There is other "stuff" outside the parens and each set of data is separated with a comma. I cannot seem to come up with a working 'sed' command(s) to effectively delete everything except what is in parens and separating commas. In other words a line might look like:

blah blah blah (good stuff here) maybe some blah, more blah (more good stuff here), ...

Can one of you regexp experts help me with this? TIA.
 
Something like this ?
sed 's!)[^)]*$!)!;s![^(][^(]*(\([^)][^)]*\))!\1,!g;s!,$!!' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
ouch.

this works in Perl:-

Code:
[b]#!/usr/bin/perl[/b]

$_ = 'blah blah blah (good stuff here) maybe some blah, more blah (more good stuff here), ...';

s/[^(]*\(([^)]*)\)[^(]*/$1\n/g;

print;

i am not a sed head - but i hope this helps!

outputs:-

[red]good stuff here
more good stuff here[/red]



Kind Regards
Duncan
 
PHV:

WOW! It works. Thanks. Yikes, what a cryptic mess. I figure it will take me a month to figure out what you did.

duncdude:

Thanks but I am not writing a perl script. However, looking a yours and PHV's, I'm wondering why. :)
 
A simplified version:
sed 's![^(]*(\([^)]*\))[^(]*!\1,!g;s!,$!!' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi awingnut

It gets even easier, in Perl, with an array:-

Code:
[b]#!/usr/bin/perl[/b]

$_ = 'blah blah blah (good stuff here) maybe some blah, more blah (more good stuff here), ...';

@matches = $_ =~ m/(\([^)]+\))/g;

print join("\n", @matches);

I realise you are not writing this in Perl - i just thought it would be interesting for you to see the far simpler regex


Kind Regards
Duncan
 
My I impose for another sed expression?

I have a series of lines that have the word 'case' followed by more data. I need to insert a space between 'case' and whatever follows it.

I used to know regexp but using it in sed seems to be slightly different (or my memory is failing which is more likely). Anyway what I can't get to work is:

sed 's!^case*$!^case *$!'

TIA.
 
You attempt suggests that you're interested on lines beginning with 'case':
sed 's!^case\([^ ]\)!case \1!' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks but could I impose for a little tutoring here?

!^case -- I get this, it means the beginning of the line starts with'case'

\( --escape the '('

[^] -- I don't get this. I would expect to use \*$ meaning anything to the eol?

\) -- excape the matching ')'

!case -- puts back case at the beginning

\1 -- ???? A sed thing that refers to what was collected by [^] which explains the above ?????
 
A starting point:
man sed

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
If you don't have the time to answer a specific question then don't. The sarcasm was unnecessary.
 
Absolutely no sarcasm but a pointer for you to get the answer.
 
Sorry, I just don't understand why you would think I am unaware of the man pages or would have asked here if I found a clear answer there, in a reasonable time. Are there really shell script writers here that do know know about man pages?
 
Are there really shell script writers here that do know know about man pages?
Unfortunately YES !!!
's!^case\([^ ]\)!case \1!'
\(...\) is a subexpression
\1 is a back-reference to the 1st subexpression
[^ ] match any character but a space

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi

Yes, we use the man pages regularly, but sadly, the [tt]sed[/tt] man page is not complete. The full list of features seems to be on it's web site :

Beside this, I think you find [tt]sed[/tt]'s man page unclear because you are not enough familiar with regular expressions. Take a look at this too :

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top