Parse String Using Sed

Hi,

I am wondering if there's a simpler way to extract the second occurrence of a word enclosed in [ ] that matches my search criteria.

Sample Input is as follows:

[main] Error installing feature [fmx] - com.er.nms.cif.ist.NoMatchingUpgra
[main] Error installing feature [fmav] - com.er.nms.cif.ist.NoMatchingUpgra

Based on the Input, I need to get the following output:

fmx 
fmav

Here's my code:

while read data; do
echo $data | sed -e 's/\([.[a-z]*] \)//' | sed 's/[A-Za-z ]*//' | sed 's/-.*//' | sed 's/]//' | sed 's/\[//'
done < data.txt

Any help is greatly appreciated.

//racbern

sed -e 's/.*\].*\[//' -e 's/\].*//' data.txt

The first substitution matches any string, closing square bracket (to skip past the first pair of square brackets), anything, followed by opening square bracket, and removes that. This should remove everything before your string. Then the second substitution replaces from closing square bracket to end of line with nothing.

As a side remark, you should always double quote any variables.

echo $str | sed -e 's/^\[.\[//' -e 's/].$//'

echo '[main] Error installing feature [fmx] - com.er.nms.cif.ist.NoMatchingUpgra' | nawk -F'[][]' '{print $4}'

Hi,

Thanks guys for your help.

//racbern