delimiting using sed command

Hi,

I have an output after performing a grep:
FAN [NO_FAULT]

and i did awk '{print $2}' it shows:
[NO_FAULT]

is there a way, by using sed that it will only show output as NO_FAULT without the brackets [ ] ?

i tried doing sed 's/^[\[]*//' but the output is
NO_FAULT] with the ] at the end..

Any help please?

Can you show the input (sample, maybe) from where you got that string and also the grep pattern? We may be able to show you better ways of selecting what you want without using so many utilities (grep,awk,sed,etc.) in a pipeline.

The part of the SYSSTAT txt file contains the following:

Fan Bank :
----------

Bank                        Status
----                        -------
FAN                         [NO_FAULT]

and my current command is:
FAN_STAT=`grep "FAN" prt.log| awk '{print $2}' | sed 's/^[\[]*//' `

and the printout is:
FAN_STAT=NO_FAULT]

Assuming that in the line containing FAN, only the Status column has square brackets:

FAN_STAT=`awk -F'[][]' '/FAN/{print $2}' prt.log`
1 Like
awk -F'[][]' '/FAN/{print $2}' prt.log
NO_FAULT]

hmm... it still has the ] at the output..

uname -i
SUNW,Sun-Fire-280R

OK...will you replace awk with nawk or /usr/xpg4/bin/awk and let us know?

Hi!! thanks!!

/usr/xpg4/bin/awk

works but

nawk

doesnt print anything.
is there a difference between /bin/awk (which is the default) and /usr/xpg4/bin/awk ??

/usr/xpg4/bin/awk -F'[][]' '/FAN/{print $2}' prt.log
NO_FAULT
nawk -F'[][]' '/FAN/{print $2}' prt.log


Check this thread http://www.unix.com/solaris/164073-what-difference-between-xpg4-bin-usr-bin.html.

A similar way in sed - select lines starting with "FAN" and print the last word, minus the surrounding brackets, would be:

sed -n '/^FAN/ {; s/^FAN[<spc><tab>]*//; s/\]//; s/\[//;p'

Replace "<spc>" and "<tab>" with literal spaces/tabs.

I hope this helps.

bakunin

sed -n '/^FAN/ s/.*\[\([^][]*\)\]$/\1/p' <file

--
Bye

1 Like

Another approach with sed:

sed -n '/^FAN/s/.*\[\(.*\)\]/\1/p' file
1 Like

Hi elixir,

Can i check with you why is it you did awk -F '' ?
I know -F is the field separator, but why the square brackets twice...?

Many thanks!

That means 2 field separators an open [ and a closed ] square bracket.