UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 7th, 2005, 02:43 PM
yongho yongho is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 57 yongho User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 38 m 20 sec
Reputation Power: 5
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?

Reply With Quote
  #2  
Old June 7th, 2005, 04:11 PM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,308 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 5 h 20 m 49 sec
Reputation Power: 48
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().
Comments on this post
yongho agrees!

Reply With Quote
  #3  
Old June 8th, 2005, 10:47 AM
yongho yongho is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 57 yongho User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 38 m 20 sec
Reputation Power: 5
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.

Reply With Quote
  #4  
Old June 8th, 2005, 02:14 PM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,308 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 5 h 20 m 49 sec
Reputation Power: 48
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
Comments on this post
SimonGreenhill agrees!

Reply With Quote
  #5  
Old June 8th, 2005, 02:22 PM
vgersh99 vgersh99 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 47 vgersh99 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 Days 5 h 48 m 21 sec
Reputation Power: 4
Send a message via AIM to vgersh99 Send a message via MSN to vgersh99 Send a message via Yahoo to vgersh99
Quote:
Originally Posted by jim mcnamara
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


Jim,
this is invalid awk syntax:
Quote:
syntax error at source line 1
context is
BEGIN { FS = "," } >>> if <<< ($1=="PBR") { print $1}
nawk: bailing out at source line 1


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]

Reply With Quote
  #6  
Old June 8th, 2005, 02:25 PM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,308 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 5 h 20 m 49 sec
Reputation Power: 48
Thanks - you're right I didn't try it.

Reply With Quote
  #7  
Old June 8th, 2005, 05:29 PM
yongho yongho is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 57 yongho User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 38 m 20 sec
Reputation Power: 5
Thumbs up thanks guys

This really helped me today.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Bourne Shell, Korn Shell


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT