mosies
September 17, 2012, 4:59am
1
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.
mosies
September 17, 2012, 5:10am
3
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
mosies
September 17, 2012, 5:19am
5
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?
mosies
September 17, 2012, 5:33am
7
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
bakunin
September 17, 2012, 5:53am
9
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
Lem
September 17, 2012, 6:10am
10
sed -n '/^FAN/ s/.*\[\([^][]*\)\]$/\1/p' <file
--
Bye
1 Like
Another approach with sed:
sed -n '/^FAN/s/.*\[\(.*\)\]/\1/p' file
1 Like
mosies
September 17, 2012, 11:41am
12
elixir_sinari:
Assuming that in the line containing FAN, only the Status column has square brackets:
FAN_STAT=`awk -F'[][]' '/FAN/{print $2}' prt.log`
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.