0

I have 3 columns, I want to create a 4th column that is equal to the 2nd column only when the 3rd column is equal to 1 (otherwise, the value can be 0).

For example,
4 3 1
would become
4 3 1 3
whereas
4 3 2
would become
4 3 2 0

I tried it 3 ways, in all cases the 4th column is all zeroes:

'BEGIN {FS = "\t"}; {if ($3!=1) last=0; else last=$2} {print $1, $2, $3, last}'

'BEGIN {FS = "\t"}; {if ($3!=1) print $1, $2, $3, 0; else print $1, $2, $3, $2}'

'BEGIN {FS = "\t"}; {if ($3==1) print $1, $2, $3, $2; else print $1, $2, $3, 0}' 
5
  • The last one works fine for me. What output are you seeing? (You don't need the ; after the BEGIN block, but it should still work.) Actually, all three ways are working for me. How are you running it? Can you show a small file that doesn't work for you, with the exact command you're using and the output you see? Commented Aug 24, 2015 at 18:44
  • 2
    Or maybe a better question: Are you sure the data is tab separated? Commented Aug 24, 2015 at 18:48
  • for i in *pp1.txt; do echo $i; awk -F, 'BEGIN {FS = "\t"}; {if ($3==1) print $1, $2, $3, $2; else print $1, $2, $3, 0}' $i > ${i%1.txt}2.txt; done this is from one of the files: 7.89947 0 6401 7.89795 0 6402 7.90405 0 6403 7.90253 2 1 7.901 2 2 7.89642 2 3 Output is all zeros on 4th column. Commented Aug 24, 2015 at 19:10
  • you're right, issue was it was not a tab delimited file, thanks! Commented Aug 24, 2015 at 19:22
  • Please read the initial couple of chapters of the book Effective Awk Programming, 4th Edition, by Arnold Robbins as you mis-understand basic awk syntax. Commented Aug 24, 2015 at 21:57

2 Answers 2

2

awk to the rescue

awk '{$(NF+1)=$3==1?$2:0}1'
Sign up to request clarification or add additional context in comments.

4 Comments

For non-gawk awks you will need to use parenthesis around ternary operators.
@iiSeymour It, at works with at least nawk, mawk, plan9[port]'s awk, and gawk.
It's only some awks in some contexts that will fail (syntax error) with unparenthesized ternary expressions. For example try print 1>2?3:4 on OSX awk (I THINK that's one of the failure cases). So this particular expression might work with all awks or just some of them but you should always parenthesize your ternary expressions for portability to other awks and, more importantly, for readability as ternary expressions are hard enough to read without embedding them unparenthesized in longer expressions.
@EdMorton That expression behaves as defined by the standard grammar where print and printf use a reduced set of expressions when called without parenthesis. The reduced grammar includes assignment and ternary expressions, but not integer comparisons. This might be to easily differentiate print arguments from output redirection. gawk fails in a similar way as nawk, and mawk creates a file named 3 containing 1.
2
$  awk '{print $0, ($3==1?$2:0)}' file
4 3 1 3
4 3 2 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.