来自awk中多个输入的输出匹配列 [英] output matching column from multiple input in awk

查看:47
本文介绍了来自awk中多个输入的输出匹配列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我只需要这两个输入中的一些数据,它们分别是inputA.txt中的"A"和inputB.txt中的"B"

Assumes there are some data from these two input which I only want, which is "A" from inputA.txt and "B" from inputB.txt

==> inputA.txt <==
A 10214027 6369158
A 10214028 6369263
A 10214029 6369321
A 10214030 6369713
A 10214031 6370146
A 10214032 6370553
A 10214033 6370917
A 10214034 6371322
A 10214035 6371735
A 10214036 6372136

所以我只想要带A的数据

So I only want the data with A's

==> inputB.txt <==
B 50015214 5116941
B 50015215 5116767
B 50015216 5116577
B 50015217 5116409
B 50015218 5116221
B 50015219 5116044
B 50015220 5115845
B 50015221 5115676
B 50015222 5115512
B 50015223 5115326

这里也一样,只想要B的

Same goes here, only want B's

并且我已经构建了脚本,但是由于使用了多个输入,因此脚本已经翻倍了.

and I've built the script, but it's been doubled due to using multiple inputs.

#!/bin/awk -f
BEGIN{
    printf "Column 1\tColumn 2\tColumn 3"
}
/^A/{
    c=substr($2,1,4)
    d=substr($2,5,3)
    e=substr($3,1,4)
    f=substr($3,5,3)
}
{
    printf "%4.1f %4.1f %4.1f %4.1f\n",c,d,e,f > "outputA.txt"
} 
/^B/{
    c=substr($2,1,4)
    d=substr($2,5,3)
    e=substr($3,1,4)
    f=substr($3,5,3)
}
{
    printf "%4.1f %4.1f %4.1f %4.1f\n",c,d,e,f > "outputB.txt"
}

让我知道您对此的想法.

Let me know your thought on this.

预期输出

==> outputA.txt <==
Column 1 Column 2 Column 3 Column 4
1021 4027 6369 158
1021 4028 6369 263
1021 4029 6369 321
1021 4030 6369 713
1021 4031 6370 146
1021 4032 6370 553
1021 4033 6370 917
1021 4034 6371 322
1021 4035 6371 735
1021 4036 6372 136

==> outputB.txt <==
Column 1 Column 2 Column 3 Column 4
5001 5214 5116 941
5001 5215 5116 767
5001 5216 5116 577
5001 5217 5116 409
5001 5218 5116 221
5001 5219 5116 044
5001 5220 5115 845
5001 5221 5115 676
5001 5222 5115 512
5001 5223 5115 326

推荐答案

能否请您尝试以下操作.

Could you please try following.

awk '
FNR==1{
  sub(/[a-z]+/,"",FILENAME)
  file="output"FILENAME".txt"
  print "Column 1 Column 2 Column 3 Column 4" > (file)
}
{
  print substr($0,3,4),substr($0,7,4),substr($0,12,4),substr($0,16,3) > (file)
}
'  inputA inputB

说明:

awk '                                                                                ##Starting awk program here.
FNR==1{                                                                              ##Checking condition if FNR==1, line number is 1 then do following.
  sub(/[a-z]+/,"",FILENAME)                                                          ##Substituting all small letters from file name with NULL.
  file="output"FILENAME".txt"                                                        ##Creating variable file whose value is string output FILENAME and .txt
  print "Column 1 Column 2 Column 3 Column 4" > (file)                               ##Printing headers to output file.
}
{
  print substr($0,3,4),substr($0,7,4),substr($0,12,4),substr($0,16,3) > (file)       ##Printing substrings values as per OP need to output files.
}
'  inputA inputB                                                                     ##Mentioning multiple Input_file names here.

这篇关于来自awk中多个输入的输出匹配列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆