简体   繁体   English

使用awk替换列

[英]Replace column using awk

I have 3 files 我有3个档案

1file 1文件

 14/09/15
 14/09/15
 14/09/15
 14/09/15
 14/09/15

2file 2file

14/09/01 
14/09/01
14/09/01
14/09/01
14/09/01

and 3file 和3file

15/09/14,11-37,01/09/14,1224A,0G,71%,RGS
15/09/14,11-41,01/09/14,2700A,0G,94%,RAN
15/09/14,11-43,01/09/14,2701A,0G,100%,RAN
15/09/14,11-44,01/09/14,2701B,0G,92%,RAN
15/09/14,11-46,01/09/14,2708A,0G,88%,RAN

I need replace the column 1 from 3f with the column 1 from 1f and the third from 3f with the column 1 of 2f 我需要将3f中的第1列替换为1f中的第1列,并将3f中的第3列替换为2f中的第1列

How can I replace using awk? 如何使用awk替换?

The following awk code will be usefull 以下awk代码将非常有用

$ awk   'BEGIN{OFS=FS=","}NF==1{line[FNR]=line[FNR]","$0} NF>1{split(line[FNR], a); $1=a[2]; $2=a[3]; print $0}' 1file 2file 3file
14/09/15,14/09/01,01/09/14,1224A,0G,71%,RGS
14/09/15,14/09/01,01/09/14,2700A,0G,94%,RAN
14/09/15,14/09/01,01/09/14,2701A,0G,100%,RAN
14/09/15,14/09/01,01/09/14,2701B,0G,92%,RAN
14/09/15,14/09/01,01/09/14,2708A,0G,88%,RAN

What it does?? 它能做什么??

  • OFS=FS="," sets output field separator OFS and field separator FS as , OFS=FS=","将输出字段分隔符OFS和字段分隔符FS,

  • NF==1{line[FNR]=line[FNR]","$0} If number of fields/columsn , NF is one, save the value in line variable seperated by commas NF==1{line[FNR]=line[FNR]","$0}如果字段数/列数NF为1,则将该值保存在以逗号分隔的line变量中

  • NF>1{split(line[FNR], a); $1=a[2]; $2=a[3]; print $0} NF>1{split(line[FNR], a); $1=a[2]; $2=a[3]; print $0} takes the action when NF greater than one NF大于1时执行NF>1{split(line[FNR], a); $1=a[2]; $2=a[3]; print $0}

    • split(line[FNR], a); split the line variable to array a line变量拆分为数组a

    • $1=a[2]; $2=a[3]; sets the first and second column 设置第一列和第二列

    • print $0 prints the entire record print $0打印整个记录

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

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