从两个文件打印列

时间:2019-03-29 05:49:01

标签: awk

如何从各种文件中打印列? 我尝试根据Awk: extract different columns from many different files

paste <(awk '{printf "%.4f   %.5f ", $1, $2}' FILE.R ) <(awk '{printf "%.6f      %.0f.\n", $3, $4}' FILE_R )

FILE.R == ARGV[1] { one[FNR]=$1 }
FILE.R == ARGV[2] { two[FNR]=$2 }
FILE_R == ARGV[3] { three[FNR]=$3 }
FILE_R == ARGV[4] { four[FNR]=$4 }

END {
    for (i=1; i<=length(one); i++) {
        print one[i], two[i], three[i], four[i]
    }
}

但我不知道如何使用此脚本。

FILE.R

56604.6017    2.3893    2.2926    2.2033
56605.1562    2.3138    2.2172    2.2033

FILE_R

56604.6017   2.29259 0.006699      42.
56605.1562   2.21716 0.007504      40.

需要输出

56604.6017    2.3893 0.006699      42.
56605.1562    2.3138 0.007504      40.

谢谢

1 个答案:

答案 0 :(得分:3)

这是一种方式:

$ awk -v OFS="\t" 'NR==FNR{a[$1]=$2;next}{print $1,a[$1],$3,$4}' file1 file2

输出:

56604.6017      2.3893  0.006699        42.
56605.1562      2.3138  0.007504        40.

解释:

$ awk -v OFS="\t" '       # setting the field separator to a tab
NR==FNR {                 # process the first file
    a[$1]=$2              # hash the second field, use first as key
    next
} 
{
    print $1,a[$1],$3,$4  # output 
}' file1 file2

如果带制表符的字段间距不够,请与样本中的修饰符一起使用printf

相关问题