The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM
Home Forums Register Rules & FAQ Members List Arcade Search Today's Posts Mark Forums Read


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


Other UNIX.COM Threads You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
comparing files - adding/subtracting/formating columns oabdalla Shell Programming and Scripting 7 3 Weeks Ago 12:20 AM
Perl: adding columns in CSV file with information in each dolo21taf Shell Programming and Scripting 1 03-04-2008 10:52 PM
Adding columns to excel files using Perl dolo21taf Shell Programming and Scripting 1 02-20-2008 03:13 AM
Adding columns of two files chandra321 Shell Programming and Scripting 6 04-06-2007 06:36 AM
adding columns Kelam_Magnus Shell Programming and Scripting 9 01-25-2002 06:35 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-16-2008
Registered User
 

Join Date: Jan 2007
Posts: 19
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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 01:37 PM. Reason: Simplification of problem
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-16-2008
rubin's Avatar
Registered User
 

Join Date: Nov 2007
Posts: 119
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Code:
awk -v d="$(date "+%Y-%m-%d")" '{print "abc", d, $1}' foo.csv > foo.txt
Reply With Quote
  #3 (permalink)  
Old 05-17-2008
Registered User
 

Join Date: Jan 2007
Posts: 19
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
  #4 (permalink)  
Old 05-17-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,203
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
  #5 (permalink)  
Old 1 Week Ago
Registered User
 

Join Date: Jan 2007
Posts: 19
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
Google UNIX.COM
Reply



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 05:17 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102