简体   繁体   English

AWK 根据其他列的 if else 写入新列

[英]AWK write to new column base on if else of other column

I have a CSV file with columns A,B,C,D.我有一个包含 A、B、C、D 列的 CSV 文件。 Column D contains values on a scale of 0 to 1. I want to use AWK to write to a new column E base in values in column D. D 列包含 0 到 1 范围内的值。我想使用 AWK 将 D 列中的值写入新的 E 列。

For example:例如:
if value in column D <0.7, value in column E = 0.如果 D 列中的值 <0.7,则 E 列中的值 = 0。
if value in column D>=0.7, value in column E =1.如果D列中的值>=0.7,则E列中的值=1。

I am able to print the output of column E but not sure how to write it to a new column.我能够打印 E 列的输出,但不确定如何将其写入新列。 Its possible to write the output of my code to a new file then paste it back to the old file but i was wondering if there was a more efficient way.可以将代码的输出写入新文件,然后将其粘贴回旧文件,但我想知道是否有更有效的方法。 Here is my code:这是我的代码:

awk -F"," 'NR>1 {if ($3>=0.7) $4= "1"; else if ($3<0.7) $4= "0"; print $4;}' test_file.csv

下面的 awk 命令应该给你预期的输出

cat yourfile.csv|awk -F "," '{if($4>=0.7)print $0",1";else if($4<0.7)print $0",0"}' > test_file.csv

您可以使用:

awk -F, 'NR>1 {$0 = $0 FS (($4 >= 0.7) ? 1 : 0)} 1' test_file.csv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM