LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-08-2003, 03:50 PM   #1
Astro
Member
 
Registered: Jan 2003
Location: Ballston Lake, NY
Distribution: Slackware, Debian
Posts: 660

Rep: Reputation: 30
Two field seperators in awk??


Need to parse a file with two different field seperators. Here's an example line form the file...

FOSS:05-23-00:Paper,9.95:Game,45.99:Hub,39.95:Patch Cable,29.95

format is name:date:item,price...etc

there can be any # of item,price's at the end

in the beginning of the awk script the field seperator needs to be set to ":" for the main seperation of the fields.... but how can I extract the items and prices based on the "," field seperator??
 
Old 11-08-2003, 06:12 PM   #2
dkloes
Member
 
Registered: Oct 2003
Posts: 66

Rep: Reputation: 15
Unless I misunderstand what you want:

Extract the field and treat the items separately:
awk -F: '{print $3}' file1 | awk -F, '{print $1, $2}'
Paper 9.95
 
Old 11-09-2003, 10:12 AM   #3
Astro
Member
 
Registered: Jan 2003
Location: Ballston Lake, NY
Distribution: Slackware, Debian
Posts: 660

Original Poster
Rep: Reputation: 30
Good idea, but I think I figured out what needed to be done.... did a little reading and found the split operator...

Code:
for (i=3;i<=NF;i++)
{
     split($i,Arr,",")
     total+=Arr[2]
}
Set i=3 cause thats always the first item,price...
<= to the # of fields so it can get the last value
split $i using "," and add it up to get my total.

Arr[2] being the price and Arr[1] being the item...


 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
quick hand with awk multiple field separators pld Programming 10 05-28-2010 07:51 AM
TOS field value lucs Slackware 1 03-31-2005 10:55 AM
AWK: print field to end, and character count? ridertech Linux - Newbie 1 05-07-2004 05:07 PM
Multiple Field Seperators in Awk... TheDarktrooper Programming 6 05-06-2004 04:50 AM
My field separator changes when using awk Helene Programming 3 05-01-2004 08:10 AM


All times are GMT -5. The time now is 07:33 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration