Retrieve string between parentheses?

 
   Computer Help Archives Forum Index -> AWK
View previous topic :: View next topic  
AuthorMessage
Jan Danielsson
Guest





PostPosted: Fri Nov 17, 2006 6:03 pm    Post subject: Retrieve string between parentheses?

Hello all,

(Is the group alt.comp.lang.sed dead?)

I need to parse the file name from an "md5" output (the file name is
enclosed in parenthesis). With my very limited shell fu, I managed to
tell sed the following:

$ md5 * | sed 's/.*(//; s/).*=\ /:/'

However, this will - obviously - fail if the file name contains
parenthesis. I need to scan for a ")" backwards from the end of the line.

Can sed do it, or do I need awk?

--
Kind regards,
Jan Danielsson
Back to top
Paddy
Guest





PostPosted: Fri Nov 17, 2006 6:08 pm    Post subject: Re: Retrieve string between parentheses?

Jan Danielsson wrote:

Quote:
Hello all,

(Is the group alt.comp.lang.sed dead?)

I need to parse the file name from an "md5" output (the file name is
enclosed in parenthesis). With my very limited shell fu, I managed to
tell sed the following:

$ md5 * | sed 's/.*(//; s/).*=\ /:/'

However, this will - obviously - fail if the file name contains
parenthesis. I need to scan for a ")" backwards from the end of the line.

Can sed do it, or do I need awk?

--
Kind regards,
Jan Danielsson

I don't know md5 format.

Could you give a few lines of input and expected output that covers the
range of input edge cases?

- Paddy.
Back to top
Janis Papanagnou
Guest





PostPosted: Fri Nov 17, 2006 6:23 pm    Post subject: Re: Retrieve string between parentheses?

Jan Danielsson wrote:
Quote:
Hello all,

(Is the group alt.comp.lang.sed dead?)

I need to parse the file name from an "md5" output (the file name is
enclosed in parenthesis).

(My md5sum does not output parenthesis.)

Quote:
With my very limited shell fu, I managed to
tell sed the following:

$ md5 * | sed 's/.*(//; s/).*=\ /:/'

However, this will - obviously - fail if the file name contains
parenthesis. I need to scan for a ")" backwards from the end of the line.

Can sed do it, or do I need awk?


If there's only one parenthesis on the line...

awk -F[()] '{print $2}'

....otherwise adjust the field number.

Janis
Back to top
Jan Danielsson
Guest





PostPosted: Fri Nov 17, 2006 6:24 pm    Post subject: Re: Retrieve string between parentheses?

Paddy wrote:
Quote:
I need to parse the file name from an "md5" output (the file name is
enclosed in parenthesis). With my very limited shell fu, I managed to
tell sed the following:

$ md5 * | sed 's/.*(//; s/).*=\ /:/'

However, this will - obviously - fail if the file name contains
parenthesis. I need to scan for a ")" backwards from the end of the line.

Can sed do it, or do I need awk?
I don't know md5 format.

Could you give a few lines of input and expected output that covers the
range of input edge cases?

Sure,

$ md5 test.sh
MD5 (test.sh) = 5387c69d47140869a1707f343f11019a

The problem is, like I mentioned above, that a file name can contain
'(' and ')', and they - obviously - need not be nested.

So this is a (theoretical) possibility:

MD5 (foo(bar)))))())(()(()(.txt) = 5387c69d47140869a1707f343f11019a

--
Kind regards,
Jan Danielsson
Back to top
Janis Papanagnou
Guest





PostPosted: Fri Nov 17, 2006 6:38 pm    Post subject: Re: Retrieve string between parentheses?

Jan Danielsson wrote:
Quote:
Paddy wrote:

I need to parse the file name from an "md5" output (the file name is
enclosed in parenthesis). With my very limited shell fu, I managed to
tell sed the following:

$ md5 * | sed 's/.*(//; s/).*=\ /:/'

However, this will - obviously - fail if the file name contains
parenthesis. I need to scan for a ")" backwards from the end of the line.

Can sed do it, or do I need awk?

I don't know md5 format.

Could you give a few lines of input and expected output that covers the
range of input edge cases?


Sure,

$ md5 test.sh
MD5 (test.sh) = 5387c69d47140869a1707f343f11019a

The problem is, like I mentioned above, that a file name can contain
'(' and ')', and they - obviously - need not be nested.

So this is a (theoretical) possibility:

MD5 (foo(bar)))))())(()(()(.txt) = 5387c69d47140869a1707f343f11019a


$ awk '{print substr($0,6,length($0)-41)}'

MD5 (test.sh) = 5387c69d47140869a1707f343f11019a
test.sh

MD5 (foo(bar)))))())(()(()(.txt) = 5387c69d47140869a1707f343f11019a
foo(bar)))))())(()(()(.txt


Janis
Back to top
Jan Danielsson
Guest





PostPosted: Fri Nov 17, 2006 6:38 pm    Post subject: Re: Retrieve string between parentheses?

Janis Papanagnou wrote:
[---]
Quote:
MD5 (foo(bar)))))())(()(()(.txt) = 5387c69d47140869a1707f343f11019a

$ awk '{print substr($0,6,length($0)-41)}'
[---]


Hmm... I think I'd better stop staring at sed, and start learning
awk. It seems much better suited for these kind of things.

Thanks!

--
Kind regards,
Jan Danielsson
Back to top
Display posts from previous:   
   Computer Help Archives Forum Index -> AWKAll times are GMT - 5 Hours
Page 1 of 1

 
Jump to: