![]() |
|
|
|||||||
| 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 |
![]() |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
|||
|
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
Thank you in advance |
|
|||
|
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
|
|
|||
|
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 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 |
|||
| Google UNIX.COM |