|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
AWK: simple pgm.
At my new internship, they know I haven't ever done UNIX scripting before but they do know I've done PHP/MYSQL and some C++ and Java/Jsp before so they've asked me to try my hand at Unix scripting. They threw me a book and I've read a few pages on syntax.
Since I'm new to the language I just want some guidance on the assignment they just put me on today. I'm to code a script that parses a CSV file. The comma-separated-value file has a fixed number of spaces for every value e.g. 10 spaces for client and 3 spaces for region. And each line is one record, no record can take two or more lines. Which commands do you think I need to use for this? Would GREP work? |
|
#2
|
|||
|
|||
|
First off, IMO that's not exactly a beginner assignment.
Actually awk is your best choice for CSV files. It's a very C-like (php-like) language with no datatyping. If there are no commas embedded inside the csv data then it is easy. Set FS (this tells awk what character separates fields) to the comma character. The awk statement Code:
{ print $1 $2 }
will print field 1 and field 2. Get an awk reference and put all this together like this Code:
awk ' code you write ' filename.csv That will print the fields. I'm sure they will want the data formatted, which is easy to do, because awk supports C-style format strings in printf(). |
|
#3
|
|||
|
|||
|
Since I'm new to syntax and all I'm having some difficulties even getting simple, useless commands to work. Could you take a look and point out the error of my ways.
app_kpi_001.pgm <-- program file (I'm using telnet, so I put in the dollar symbols even though there are none at the left side) $ awk -f app_kpi_001.pgm records.csv $ awk: syntax error near line 1 $ awk: bailing out near line 1 contents of app_kpi_001.pgm: BEGIN { FS = "," } $1 ~ /PBR/ { print $1} trying to perform: 'If first column of the row has a string value of "PBR", print the first column.' I'm obviously doing something stupid here. Missing a semi-colin? The reason why I'm doing it as PGM file is because I need to repeat some complicated set of commands to calculate lots of data based on the csv file. |
|
#4
|
|||
|
|||
|
In unix it's more usual to use file extensions like ".awk" for this code - not .pgm
Try something like this: Code:
awk ' BEGIN { FS = "," } if($1=="PBR") { print $1} ' <filename>
It's basically a one-liner in awk To make it a program substutie <filename> for $1 -- then the first command line parameter becomes the CSV file your a playing with. ie., myfile.awk somefile.csv |
|
#5
|
||||
|
||||
|
Quote:
Jim, this is invalid awk syntax: Quote:
awk ' BEGIN { FS = "," } { if($1=="PBR") print $1}' filename OR awk ' BEGIN { FS = "," } $1=="PBR" { print $1}' filename [but I'm sure you already know that] |
|
#6
|
|||
|
|||
|
Thanks - you're right I didn't try it.
|
|
#7
|
|||
|
|||
|
This really helped me today.
|
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Bourne Shell, Korn Shell |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|