Awk if-else syntax with multiple columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk if-else syntax with multiple columns
# 1  
Old 03-13-2012
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:

Quote:
>cat a.dat
12/14/2011

11/11/2012
Quote:
> awk 'BEGIN {FS=","}{ if ( $1=="" ) print $1; else print substr($1,7,4)"-"substr($1,1,2) "-" substr($1,4,2)}' a.dat
2011-12-14

2012-11-11
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.

Thanks.
Gianni
# 2  
Old 03-13-2012
Try: print $1, if. Try: printf "%s",$1; if

Last edited by Scrutinizer; 03-13-2012 at 04:21 PM..
# 3  
Old 03-13-2012
Perhaps it's AIX but it didn't work:
Quote:
>more a.dat
a,12/14/2011,123,abc,10.00,
b, ,456,,5.00,Y
c,11/11/2012,789,cde,,N
Code:
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:
Code:
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:
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. Smilie 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
# 4  
Old 03-13-2012
Hi, there should be double quotes around "%s". To get the space you can use
Code:
printf "%s" $1 OFS ;

or use
Code:
printf "%s " $1;

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 03-13-2012
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. Smilie

Code:
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

Quote:
a,2011-12-14,123,abc,10.00,
b,
c,2012-11-11,789,cde,,N
Thanks for your help though.

Gianni
# 6  
Old 03-13-2012
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?
Code:
awk -F, '$2~"/"{split($2,T,"/");$2=T[3]"-"T[1]"-"T[2]}1' OFS=, infile

Code:
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..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 03-13-2012
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:
Quote:
>cat a.dat
a,12/14/2011,123,abc,10.00,12/01/2012,a
b, ,456,,5.00,12/12/2011,b
c,11/11/2012,789,cde,,11/11/1111,c
Code:
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:
Quote:
a,2011-12-14,123,abc,10.00,2012-12-01,a
b, ,456,,5.00,2011-12-12,b
c,2012-11-11,789,cde,,1111-11-11,c
Thank you!!
Gianni
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Appending different columns of multiple files in awk

Hello All, I have three input files cat file1 col1|col2|col3 a|1|A b|2|B cat file2 col1|col2|col3 c|3|C cat file3 col1|col2|col3 d|4|D e|5|E i want below output file4 col1|col2 a|1 (6 Replies)
Discussion started by: looney
6 Replies

2. Shell Programming and Scripting

Average across multiple columns - awk

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)
Discussion started by: theflamingmoe
6 Replies

3. Shell Programming and Scripting

Comparing multiple columns using awk

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)
Discussion started by: mystition
4 Replies

4. Shell Programming and Scripting

Awk: is it possible to print into multiple columns?

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)
Discussion started by: liuzhencc
11 Replies

5. Shell Programming and Scripting

Extracting multiple columns with awk

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)
Discussion started by: gabrysfe
1 Replies

6. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

7. Shell Programming and Scripting

Generating multiple new columns with awk

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)
Discussion started by: reformatplink
6 Replies

8. Shell Programming and Scripting

awk modify multiple columns with pipes

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)
Discussion started by: bstamper
10 Replies

9. Shell Programming and Scripting

AWK subtraction in multiple columns

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)
Discussion started by: awkward
2 Replies

10. Shell Programming and Scripting

awk 3 files to one based on multiple columns

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)
Discussion started by: andrealphus
2 Replies
Login or Register to Ask a Question