Awk if-else syntax with multiple columns

I can't seem to get this to work.
I can reformat the date field if it's the first field (and only field) in the file:

However, I get a syntax error when the date field is the second field (or has any other columns following):

I can use a ";" but then it puts each column on separate lines which is not what I want:

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.

Thanks.
Gianni

Try: print $1, if . Try: printf "%s",$1; if

Perhaps it's AIX but it didn't work:

awk '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 } ' a.dat

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:

awk 'BEGIN {FS=","}{ printf $1 ; if ( $2=="" || $2==" ") print $2; else print substr($2,7,4)"-"substr($2,1,2) "-" substr($2,4,2), $3, $4, $5, $6 } ' a.dat

I got this:

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. :frowning: 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.
Gianni

Hi, there should be double quotes around "%s". To get the space you can use

printf "%s" $1 OFS ;

or use

printf "%s " $1;
1 Like

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. :frowning:

awk 'BEGIN {FS=",";OFS=","}{ printf "%s",$1 OFS; if ( $2=="" || $2==" ") print $2; else print substr($2,7,4)"-"substr($2,1,2) "-" substr($2,4,2), $3, $4, $5, $6 } ' a.dat 

Thanks for your help though.

Gianni

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?

awk -F, '$2~"/"{split($2,T,"/");$2=T[3]"-"T[1]"-"T[2]}1' OFS=, infile
a,2011-12-14,123,abc,10.00,
b, ,456,,5.00,Y
c,2012-11-11,789,cde,,N

If not please specify the kind of output you are looking for..

1 Like

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!

Sample Input:

awk -F, '$2~"/"{split($2,T,"/");$2=T[3]"-"T[1]"-"T[2]}$6~"/"{split($6,T,"/");$6=T[3]"-"T[1]"-"T[2]}1' OFS=, a.dat

Output:

Thank you!!
Gianni

You're welcome! There are many ways of coding the same solution. My last suggestion is just a different approach. I was focussing on the error message, also because there was no input file specified in the first post. Come to think of it, if you only want to change dates around, sed might be more natural choice for the job..

sed 's|\(..\)/\(..\)/\(....\)|\3-\1-\2|g' infile
1 Like

Thank you. I'll save for when I have to reformat just the dates in a file. For this task, I had to reformat other fields at the same time if they exist like phone number, zip codes, etc., while printing out only needed columns, that's why I was looking at awk.
Thanks again!