The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-24-2008
thewench thewench is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 2
Help please awk or cut

Hi I'm new to unix programming so struggling with something thats probably simple to many of you

I have data files of the format :

ID, date, value1, value2, blank

on each line either value1 or value2 will be zero. I need my output file to contain

ID, date, non-zero value

The input and output filenames need to be the same (extension can possibly be different). The ID is of variable length.

I've tried using cut and if to identify the non-zero field and have also played with summing value1 and value2 (if one is always zero then the result is correct). I've spent today trying to learn awk and have come up with a few wrong answers, can anybody help please

Thanks in anticipation
  #2 (permalink)  
Old 01-24-2008
jaduks's Avatar
jaduks jaduks is offline
Registered User
  
 

Join Date: Aug 2007
Location: Assam,India
Posts: 167
A quick solution would be this, but I fell awk would have been better


Code:
$ cat thefile.out
ID1,23-12-1983,32,12,
ID2,12-12-1982,,21,
ID3,14-06-2007,12,,
$ sed -e 's/,,/,/' -e 's/,$//' thefile.out > thefile.tmp
$ cat thefile.tmp
ID1,23-12-1983,32,12
ID2,12-12-1982,21
ID3,14-06-2007,12

  #3 (permalink)  
Old 01-24-2008
thana thana is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 55
I am haven't understood it wrong then this is the soln

cat filename:

ID1,23-12-1983,2,0,
ID2,12-12-1982,0,21,
ID3,14-06-2007,12,0,
ID4,14-09-2008,0,33,


[code :]

awk -F',' '{print $1","$2","($3+$4)};' filename

[code :]
  #4 (permalink)  
Old 01-25-2008
thewench thewench is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 2
Thanks

That's great, thanks....as it happens I wasn't too far off!
Closed Thread

Bookmarks

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:45 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0