system
October 22, 2008, 6:28pm
1
Hi guys, looking for a bit of advise, and as I am a complete novice, please excuse the daft questions!!
I have a list of events and of which entry looks like this;
#
# Event 1
# NAME = Event 1
#
12345 : 123 : 1 : 1 : L,1,N : 1,0 : Event
#
# Event 2
# NAME = Event 2
#
12346 : 124 : 1 : 1 : L,1,N : 1,0 : Event
I am looking for a grep which can search for the various event code (in this case 12345) and print the Event "NAME" to screen. I have been racking my brains for a while now, but cant seem to think of a way to do this. Can anyone suggest a way?
So I will have something like;
"Event code $eventCode is $eventName" printed to the screen if that makes sense? Obviously using read to enter the event code in each time the script is ran...
Thank you in advanced.
awk -v v=12345 '$1==v{print s}$2=="NAME"{s="Event code" FS v FS "is" FS substr($0,10)}' file
rubin
October 22, 2008, 9:08pm
3
#!/bin/bash
read -p " Enter id: " id
awk -v id="$id" '$1==id{ printf "Event code %s is %s\n", s, $1 } /NAME =/{ s=$(NF-1) FS $NF }' file
nawk -F"[=|:|#]" -v e="$1" '{
if ($2==" NAME ")
t=$3
if($1==e)
print t
}' filename
system
October 23, 2008, 4:52am
5
Thanks for all your help guys.
Here is what I am using at the moment;
#!/bin/bash
nawk -F"[=|:|#]" -v e="$1" '{
if ($2==" NAME ")
t=$3
if($1==e)
print t
}' comps
However this is printing duplicates (x4), e.g;
Event 1
Event 1
Event 1
Event 1
Event 2
Event 2
Event 2
Event 2
Event 3
Event 3
Event 3
Event 3
Could this have something to do with the source file displaying the event name twice, e.g;
#
# Event 1
# NAME = Event 1
#
Any advise would be great.
Thanks again all!
How about something like:
# awk '/NAME/{FS="=";t="Event "$2" is ";getline;getline;getline;print t$0}' infile
Event Event 1 is 12345 : 123 : 1 : 1 : L,1,N : 1,0 : Event
Event Event 2 is 12346 : 124 : 1 : 1 : L,1,N : 1,0 : Event
or
# awk '/NAME/{FS="=";t="Event "$2" is ";getline;getline;getline;FS=" ";print t$1}' infile
Event Event 1 is 12345
Event Event 2 is 12346
or am I misreading what you're after ?
system
October 23, 2008, 5:39am
7
Thanks Tytalus,
I have had a little play around and;
awk '/NAME/{FS="=";t="Event "$2" is event id ";getline;getline;FS=" ";print t$3}' comps
seems to output the id and relevent event name perfectly. And looks something like
Event Event 1 is event id 587
Is there anyway to prompt the script to ask the user for an event id and then pull back the name of that event, such as;
#!/bin/sh
echo "Enter the event id"
read event
awk '/NAME/{FS="=";t="Event "$2" is event id ";getline;getline;FS=" ";print t$3}' comps
What would be needed to link that user input to the awk command?
# ./get-event
Enter the event id
1
Event 1 is event id 12345
# ./get-event
Enter the event id
2
Event 2 is event id 12346
# cat get-event
#!/bin/sh
echo "Enter the event id"
read event
nawk -v var=$event '/NAME/&&($5==var){FS="=";t="Event "var" is event id ";getline;getline;getline;FS=" ";print t$1}' infile
HTH
system
October 23, 2008, 7:03am
9
Sorted now;
#!/bin/sh
echo "Enter the competition id"
read comp
echo "*****************************************************************"
awk '/NAME/{FS="=";t=""$2" is comp id ";getline;getline;FS=" ";print t$3}' comps | grep $comp
echo "*****************************************************************"
thank you for all your help