However, I get a syntax error when the date field is the second field (or has any other columns following):
Quote:
> cat b.dat
a,12/14/2011
b,
c,11/11/2012
> awk 'BEGIN {FS=","}{ print $1, if ( $2=="" ) print $2; else print substr($2,7,4)"-"substr($2,1,2) "-" substr($2,4,2)}' b.dat
syntax error The source line is 1.
The error context is
BEGIN {FS=","}{ print $1, >>> if <<< ( $2=="" ) print $2; else print substr($2,7,4)"-"substr($2,1,2) "-" substr($2,4,2)}
awk: The statement cannot be correctly parsed.
The source line is 1.
I can use a ";" but then it puts each column on separate lines which is not what I want:
Quote:
> awk 'BEGIN {FS=","}{ print $1; if ( $2=="" ) print $2; else print substr($2,7,4)"-"substr($2,1,2) "-" substr($2,4,2)}' b.dat
a
2011-12-14
b
c
2012-11-11
Any ideas as to what I'm doing wrong?? I need my data to be in this format:
a,2011-12-14,123,456,...eol
b,2011-11-11,321,789...eol
c,,...eol
etc.
syntax error The source line is 1.
The error context is
BEGIN {FS=","}{ printf >>> % <<< s, $1 ; if ( $2=="" || $2==" ") print $2; else print substr($2,7,4)"-"substr($2,1,2) "-" substr($2,4,2), $3, $4, $5, $6 }
awk: The statement cannot be correctly parsed.
The source line is 1.
When I did this:
I got this:
Quote:
a2011-12-14 123 abc 10.00
b
c2012-11-11 789 cde N
Maybe awk can't handle this. It also looks like I'd have to repeat the 'print' command for each 'else' or I won't get anything on a line where $2 is blank - which is way too tedious because my actual file has 35 columns with dates in various columns to reformat. Also, I'm not just reformatting dates but other fields as well in (i.e. if blank don't reformat) but this solution would help me to do the other columns.
Thanks. It worked but I may have to ditch awk and do a while read instead on all million+ lines and format it that way. I wanted to use awk since it's always been super fast but doesn't seem like it is possible for this situation. Oh well.
I wanted to help you find the problem with the awk script you were writing, but that does not mean it cannot be done with awk.
Would this be more in the direction?
If not please specify the kind of output you are looking for..
This User Gave Thanks to Scrutinizer For This Post:
Thank you so much! That's what I was looking for actually.
I thought we needed to show what we wrote to get help. My awk skill is apparently not very good but this works for me!
Hi forum members,
I'm trying to get an average of multiple columns in a csv file using awk. A small example of my input data is as follows:
cu,u3o8,au,ag
-9,20,-9,3.6
0.005,30,-9,-9
0.005,50,10,3.44
0.021,-9,8,3.35
The following code seems to do most of what I want
gawk -F","... (6 Replies)
Hello All;
I have two files with below conditions:
1. Entries in file A is missing in file B (primary is field 1)
2. Entries in file B is missing in file A (primary is field 1)
3. Field 1 is present in both files but Field 2 is different.
Example Content:
File A
... (4 Replies)
Hi guys,
I have hundreds file like this, here I only show two of them:
file 1
feco4_s_BB95.log ZE_1=-1717.5206260
feco4_t_BB95.log ZE_1=-1717.5169250
feco5_s_BB95.log ZE_1=-1830.9322060... (11 Replies)
Hi everyone!!
I need to apply a simple command to extract columns from a matrix, but I need to extract contemporary from the first to the tenth columns, than from the eleventh to the twentyth and so on...
how can i do that? (1 Reply)
Hi,
I'm trying to reformat a file to create a new columns reflecting the previous 2 over and over. By that I mean currently each observation has two columns and I want to create a third which has a value equal to 1 minus the sum of the previous two.
This is slightly complicated as 1) I... (6 Replies)
Hello,
I have a CSV-like dataset where some of the columns contain HTML snippets which I need to convert to XHTML. For any given snippet, I have a functioning config for the text processor 'tidy' such that
tidy -config tidy.cfg example.html
does the job I need done.
I would like to process... (10 Replies)
AWK subtraction in multiple columns
Hi there,
Can not get the following:
input: 34523 934
9485 3847
394 3847
3456 9384
awk 'NR==1 {for (i = 1; i <= NF; i++) {n=$i; next}; {n-=$i} END {print n}' input
output: 21188 first column only,... (2 Replies)
Hi all,
I have three files, one is a navigation file, one is a depth file and one is a file containing the measured field of gravity. The formats of the files are;
navigation file:
2006 320 17 39 0 0 *nav 21.31542 -157.887
2006 320 17 39 10 0 *nav 21.31542 -157.887
2006 320 17 39 20 0... (2 Replies)