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 !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Adding columns of two files chandra321 Shell Programming and Scripting 7 05-06-2009 01:11 PM
adding columns Kelam_Magnus Shell Programming and Scripting 12 02-10-2009 10:56 AM
comparing files - adding/subtracting/formating columns oabdalla Shell Programming and Scripting 7 06-13-2008 04:20 AM
Perl: adding columns in CSV file with information in each dolo21taf Shell Programming and Scripting 1 03-05-2008 02:52 AM
Adding columns to excel files using Perl dolo21taf Shell Programming and Scripting 1 02-20-2008 07:13 AM

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 05-16-2008
figaro figaro is offline
Registered User
  
 

Join Date: Jan 2007
Posts: 271
Adding columns to a file

I want to select the first column from a daily file called foo.csv. The result is written to file foo.txt. Currently the following script is used for that:
cut -d, -f 1 foo.csv > foo.txt

A typical result would yield :
A12
A45
B11
B67

What needs to happen in addition is that two columns need to be added before writing to file, namely a code (say 'abc') and the current date, such that a typical result would yield :
abc 2008-05-20 A12
abc 2008-05-20 A45
abc 2008-05-20 B11
abc 2008-05-20 B67

How can this be achieved in particular if the date (column 2) should be today's date?

Last edited by figaro; 05-16-2008 at 05:37 PM.. Reason: Simplification of problem
  #2 (permalink)  
Old 05-16-2008
rubin's Avatar
rubin rubin is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2007
Posts: 321

Code:
awk -v d="$(date "+%Y-%m-%d")" '{print "abc", d, $1}' foo.csv > foo.txt

  #3 (permalink)  
Old 05-17-2008
figaro figaro is offline
Registered User
  
 

Join Date: Jan 2007
Posts: 271
Thank you for your response, it worked very well. A question:

The delimiter you are suggesting is a space. How can this be changed to a comma ','?

And for the record, the full statement needs to be:

Code:
cut -d, -f 1 foo.csv | awk -v d="$(date "+%Y-%m-%d")" '{print "abc", d, $1}' >foo.txt

If there are more efficiencies in this statement to be made, please let me know.

Thank you in advance
  #4 (permalink)  
Old 05-17-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
awk is perfectly able to do the job of cut at the same time, so yes, that can be optimized a bit. -F , sets awk's field separator to comma and OFS=FS causes it to use it as the Output Field Separator as well as [Input] Field Separator.


Code:
awk -F, -v d="$(date "+%Y-%m-%d")"  '{ OFS=FS; print "abc", d, $1 }' foo.csv >foo.txt

  #5 (permalink)  
Old 06-25-2008
figaro figaro is offline
Registered User
  
 

Join Date: Jan 2007
Posts: 271
I am having the following problem when executing this script. It currently looks as follows:

Code:
awk -F, -v dt="$(date "+%Y-%m-%d")" '{ OFS=FS; print "abc", dt, $1 }' foo.csv > foo.txt

The response is "Illegal variable name."

The first few lines of the input file foo.csv look as follows:
SG70,B3B0M92,ANN8132R7036,25-Jun-08
SG68,B3B2J97,ANN8132R6871,25-Jun-08
ST71,B2Q4T68,ANN8132N4540,25-Jun-08
SG67,B3B2J75,ANN8132R6798,24-Jun-08

so it is a list of codes of which the codes in the first column are needed only. Prepended with the code "abc" and the date, the resulting file foo.txt should hold the following data:

"abc",2008-06-25,SG70
"abc",2008-06-25,SG68
"abc",2008-06-25,ST71
"abc",2008-06-25,SG67

How do I correct the code above and preferably still keep everything on one line?

Thanks in advance
  #6 (permalink)  
Old 07-21-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Try with nawk, bawk, mawk, gawk, or XPG4 awk if you have any of those installed on your system.
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:26 PM.


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