awk - 来自awk中多个输入的输出匹配列

标签 awk

假设我只想要这两个输入中的一些数据,它们是inputA.txt中的“ A”和inputB.txt中的“ B”

==> 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的数据

==> 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

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

#!/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"
}


让我知道你对此的想法。

预期产量

==> 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

最佳答案

您能不能试一下。

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 - 来自awk中多个输入的输出匹配列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57423495/

相关文章:

Linux命令用另一个字符串替换大文件中的字符串

linux - 如何用shell函数制作 "dictionary"?

linux - 如何使用正则表达式有效地将参数附加到多个不同的 grub 配置文件

python - 过滤出现在两个标记之间的文本

regex - 使用 SED 或 AWK 从文件中删除行

linux - 使用 awk 搜索模式并从日志文件中打印接下来的 15 行

linux - 如何编写 shell 脚本以从 sar 报告中查找 ram 利用率的最小最大值和平均值

shell - 按第一个字符分割文件

bash - 在 Bash 中替换垂直线

regex - 如何 grep/perl/awk 重叠正则表达式