Transform CSV column by a function using command line tools

0

Given a csv file such as

h1 h2
a  0
b  1
c  0

how to rename 0 to YES and 1 to NO in the second column.

elm

Posted 2016-01-14T11:36:04.073

Reputation: 155

Answers

2

One of these:

awk 'BEGIN {word[0]="YES"; word[1]="NO"} NR>1 {$2=word[$2]} 1' file
perl -pe 's/(\d)$/ qw(YES NO)[$1] /e if $.>1' file

To replace the file, you can use

gawk -i inplace '...' file
perl -i -pe '...' file

glenn jackman

Posted 2016-01-14T11:36:04.073

Reputation: 18 546

Thanks! To ask version where instead of 0 we have w and instead of 1 we have v ... – elm – 2016-01-14T12:03:10.100

awk: BEGIN {word["v"]="something"; word["w"]="something"} -- need to quote the strings so awk does not try to interpret them as variables. – glenn jackman – 2016-01-14T14:02:38.190