Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A beginner with parser problems 1

Status
Not open for further replies.

Karsinogeeni

Programmer
May 8, 2004
2
FI
Hello everybody!

I used AWK, Perl and Sed in some university courses and they seemed to be good tools making parsers. I would bang my head to a wall with this thing for longer (I know it is a question which I could solve by reading tutorials and just trying it out) but I am in a hurry with this - deadline is on monday. Besides I hate that banging head to a wall -stuff.

The thing I need to do is to extract player statistics and values from a file and write them to another file in a different format. Here are examples of input file and output how it should look like:
_____________________________________________

Down below I have a sample of a player file:
_____________________________________________

8. Julien Barbe 60 000 (opening price)
Belongs to: FcFlipperi
Age: 26 years
Value: 48 000
Deadline: 8.5.2004 at 10.06
Stamina: passable Keeper: disastrous
Playmaking: weak Passing: passable
Winger: weak Defending: passable
Scoring: poor Set Pieces: solid

9. Abraham Goodrich 580 000 (opening price)
Belongs to: Etu-Töölön Kanuuna
Age: 25 years
Value: 173 000
Deadline: 8.5.2004 at 10.13
Stamina: excellent Keeper: disastrous
Playmaking: poor Passing: poor
Winger: inadequate Defending: weak
Scoring: excellent Set Pieces: weak
_____________________________________________

And here is a sample the output file should look like:
(Note that getting keeper skill here is not neccessary)
________________________________________

48000;passable;weak;passable;weak...
173000;excellent;poor;poor;inadequate...
________________________________________

I already made a Sed script which converts textual stats to numeric stats, so its not going to be a problem.

Here's the code I wrote this far:

______________________________________________________

awk '/Value/ {print $2$3}' player.txt >> data.txt
awk '/Stamina/ {print $2 }' player.txt >> data.txt
awk '/Playmaking/ {print $2 }' player.txt >> data.txt
awk '/Playmaking/ {print $4 }' player.txt >> data.txt
awk '/Winger/ {print $2 }' player.txt >> data.txt
awk '/Winger/ {print $4 }' player.txt >> data.txt
awk '/Scoring/ {print $2 }' player.txt >> data.txt
awk '/Scoring/ {print $5 }' player.txt >> data.txt
______________________________________________________

The problems:

1) I haven't found out yet how it is possible to put value and statistics to one row and separate them with ";".

2) The code prints first values for each player, then stamina for each player etc. The player statistics should be printed separately.

Thanks for your time and help. It is really appreciated!
 
Try something like this:
awk '
/Value/{printf "%d;",$2$3}
/Stamina/{printf "%s;",$2}
/Playmaking/{printf "%s;%s;",$2,$4}
/Winger/{printf "%s;%s;",$2,$4}
/Scoring/{printf "%s;%s\n",$2,$5}
' player.txt > data.txt
I already made a Sed script which converts textual stats to numeric stats
I guess the sed job can be done in the awk program.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Beautiful! It works! Thanks for the help and a quick reply!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top