Hi there, if i had a file that looked like this
my_server1
red
green
blue
yellow
blue
my_server2
blue
blue
yellow
green
blue
my_server3
yellow
yellow
blue
and I wanted to be able to grep for the hostname line and every instance of , say the word blue that appear below that hostname (but above the next hostname) ...what tool could i use ...it seems to me that grep doesnt seem to have the ability to be as specific as I require ...is there anything else ?
I want to get an output like this
my_server1
blue
blue
my_server2
blue
blue
blue
my_server3
blue
any help on this would be great
danmero
September 22, 2008, 7:41am
2
grep 'my_server\|blue' file
OK, forgot to mention im running on Solaris, the above command returns nothing at all
grep 'blue' my_server* | awk -F: '$1!=p1{print $1;} {print $2;p1=$1;p2=$2}'
thanks for the reply, it didnt work so i removed the last bit, the awk statement (see below) and even the grep on its own is not returning anything
cat file | grep 'blue' my_server*
to be honest im a bit lost with this one
danmero
September 22, 2008, 9:51am
6
I give you 3 options here:
grep -e my_server -e blue file
awk '$0 ~ "my_server|blue"' file
sed '/myserver\|blue/p;d' file
Thankyou very much for taking the effort to give me those three options..I have tried them all with no success though ... I thank you anyway
# grep -e my_server -e blue /opt/my_file
grep: illegal option -- e
Usage: grep -hblcnsviw pattern file . . .
# awk '$0 ~ "my_server|blue"' /opt/my_file
awk: syntax error near line 1
awk: bailing out near line 1
# sed '/my_server\|blue/p;d' /opt/my_file
# <<< no output
danmero
September 22, 2008, 10:26am
8
/usr/xpg4/bin/grep -e my_server -e blue /opt/my_file
Do I really need to read the Solaris grep man
grep(1) � search a file for a pattern (man pages section 1: User Commands) - Sun Microsystems
Tytalus
September 22, 2008, 11:33am
9
# uname -rv
5.10 Generic_127111-07
# egrep "serv|blue" file
my_server1
blue
blue
my_server2
blue
blue
blue
my_server3
blue
thank you all for your suggestions, I wasnt familiar with egrep, but that certainly has done the trick
thanks again