如何在awk中添加特殊字符?

时间:2016-05-13 10:53:57

标签: linux shell awk

我有三个不同列和行大小的文件。例如,

ifile1.txt        ifile2.txt        ifile3.txt
  1    2    2       1    6            3     8
  2    5    6       3    8            9     0
  3    8    7       6    8           23     6
  6    7    6      23    6           44     5
  9   87   87      44    7           56     7
 23    6    6      56    8           78    89
 44    5   76      99    0           95    65
 56    6    7                        99    78
 78    7    8                       106     0
 95    6    7                       110     6
 99    6    4                  
106    5   34                  
110    6    4                  

Here ifile1.txt has 3 coulmns and 13 rows, 
     ifile2.txt has 2 columns and 7 rows, 
     ifile3.txt has 2 columns and 10 rows.
     1st column of each ifile is the ID, 
     This ID is sometimes missing in ifile2.txt and ifile3.txt.

我想制作一个包含4列的outfile.txt,其第一列将具有ifile1.txt中的所有ID,而第二列将是来自ifile1.txt的$ 3,第三和第四列将是来自ifile2的$ 2 .txt和ifile3.txt以及ifile2.txt和ifile3.txt中缺少的电台将被指定为特殊字符'?'。

欲望输出:

outfile.txt
  1     2     6     ?
  2     6     ?     ?
  3     7     8     8
  6     6     8     ?
  9    87     ?     0
 23     6     6     6
 44    76     7     5
 56     7     8     7
 78     8     ?    89
 95     7     ?    65
 99     4     0    78
106    34     ?     0
110     4     ?     6

我尝试使用以下算法,但无法编写脚本。

for each i in $1, awk '{printf "%3s %3s %3s %3s\n", $1, $3 (from ifile1.txt), 
check if i is present in $1 (ifile2.txt), then
           write corresponding $2 values from ifile2.txt
      else write ?
similarly check for ifile3.txt

1 个答案:

答案 0 :(得分:1)

您可以使用此脚本使用GNU AWK执行此操作:

<强> script.awk

# read lines from the three files 
ARGIND == 1 { file1[ $1 ] = $3
              # init the other files with ?
              file2[ $1 ] = "?"
              file3[ $1 ] = "?"
              next;
            }

ARGIND == 2 { file2[ $1 ] = $2
              next;
            }

ARGIND == 3 { file3[ $1 ] = $2
              next;
            }

# output the collected information
END         { for( k in file1) { 
                printf("%3s%6s%6s%6s\n", k, file1[ k ], file2[ k ], file3[ k ])
              }
            }

运行如下脚本:awk -f script.awk ifile1.txt ifile2.txt ifile3.txt > outfile.txt