Helpful Information
 
 
Category: Other Programming Languages
Using awk !!!

Hello I have a line like this
Product Name: eserver xSeries 336 -[abcdefgh]-
I'd like to get the string abcdefgh in shell using sed, awk or whatever else fits
I tryed awk /\[/,/\]/p but it doesn't work. I tryed many things but awk or sed without any success.

Any advice on how to do it ?

perl :), something like

perl -pe 's/\[(.+)\]/$1/;'

Thanx, penguin but I'd like to know how to do it in shell.

You can use sed to strip off everything up to and including the '[', and everything from the ']' to the end of line:



$ echo 'eserver xSeries 335 -[abcdefgh]-' | sed 's/.*\[//;s/\].*//'
abcdefgh

I think using ';' to separate two sed commands is undocumented - to be on the safe side you could put them on separate lines:



$ echo 'eserver xSeries 335 -[abcdefgh]-' | sed 's/.*\[//
s/\].*//'
abcdefgh

Dave

You can use sed to strip off everything up to and including the '[', and everything from the ']' to the end of line:



$ echo 'eserver xSeries 335 -[abcdefgh]-' | sed 's/.*\[//;s/\].*//'
abcdefgh

I think using ';' to separate two sed commands is undocumented - to be on the safe side you could put them on separate lines:



$ echo 'eserver xSeries 335 -[abcdefgh]-' | sed 's/.*\[//
s/\].*//'
abcdefgh

Dave You could also use two -e statements :) You can build up a multiline script like that

Thanx you very much for this solution










privacy (GDPR)