Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am very new to awk. I am trying to add a new calculated column. But awk insert the column to the next line rather to the end of the current line.

Here is an example of my data:

,2013-11-22,12.9,26.0,26.6,,,NW

What I am trying to do is to add a column at the end containing YES, or NO based on the fifth column (if $5 > 0 YES else NO).

Here is my awk command:

awk -F, '{
    if($5 > 0) print $0, ",YES"; else print $0, ",NO"
}' data.csv

The result is something like this:

,2013-11-22,12.9,26.0,26.6,,,NW
YES

How can I fix this?

share|improve this question
1  
Use printf instead of print ... –  jasonwryan Jul 25 at 5:11

2 Answers 2

up vote 2 down vote accepted

You can assign straight into fields in awk with $N, for N the field number: $9 = "YES" will add a new field at the end with the value "YES":

awk -F, -v OFS=, '{ if ($5 > 5) { $9 = "YES" } else {$9 = "NO"} };1' data

When we assign into $9 we create the ninth field, which is one beyond the end for this data. Putting 1 at the end forces awk's default output to occur, which we'd have suppressed otherwise.

For your sample the above gives you:

,2013-11-22,12.9,26.0,26.6,,,NW,YES

which I think is what you wanted.

If you want it to be the last field, regardless of how many fields there were to start with, we can use the number of fields NF, making sure we go one beyond it:

awk -v OFS=, -F, '{ if ($5 > 5) {$(NF+1)="YES"} else {$(NF+1)="NO"} };1' data

$ accepts any numeric expression, so we can add one to NF to get access to the next available field.

share|improve this answer
    
Thank you very much for the detailed answer. Is there shortcut that refer to last field? I am thinking maybe the number of column will change in the future. The shortcut will help the script to not break in case this occurs. –  Dane Jul 25 at 5:16
    
I just tested the code and I still have the same problem. YES/NO will added but form the next line. –  Dane Jul 25 at 5:19
    
You can use $(NF+1) to access the last field always (see edit). –  Michael Homer Jul 25 at 5:21
    
Not with the data you provided and the command I gave it won't. It just uses awk's default (single-line) output separated by OFS. –  Michael Homer Jul 25 at 5:24
    
I realized what the problem was. In my file I had ^M character at the end of line, which messed everything up. I have to get rid of ^M character, somehow –  Dane Jul 25 at 5:25

A shorter awk solution:

$ awk -F',' '$(NF+1) = $5 > 0 ? "YES" : "NO"' OFS=',' file

If your file contains windows newline character, you can remove it:

$ awk -F',' '{sub(/\r$/,"")} $(NF+1) = $5 > 0 ? "YES" : "NO"' OFS=',' file
share|improve this answer
    
This is great. Thank you –  Dane Jul 25 at 6:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.