Awk:将输出附加到现有文件中的新字段

时间:2017-07-12 08:51:32

标签: awk printing append field

有没有办法每次都将awk脚本的输出作为新字段打印到现有文件中?

嗨! 我是awk的新手(因此我的术语可能不正确,对不起!)我正在尝试打印一个脚本的输出,该脚本将在几百个文件上运行到同一个文件,在不同的字段中。

例如,我的数据文件具有以下结构:

#File1
1
Values, 2, Hanna
20
15
Values, 2, Josh
30 
56
Values, 2, Anna
50
70

#File2
2
Values, 2, Hanna
45
60
Values, 2, Josh
98 
63
Values, 2, Anna
10
56

我有几个这样的文件,它们按编号的月份划分,具有相同的名称,但值不同。我想要按人名来命名的文件,以及按月分类的字段,如下所示:

#Hanna
20 45
15 60

#Josh
30 98
56 63

#Anna
50 10
70 56

在我的脚本中,我搜索“值”这个词,并确定要打印的记录(基于“值”之后的数字)。这很好用。然后我想打印这些值。它适用于一个文件,命令为:

Print $0 > name #the varible name已保存为= $ 3正确的行

这会创建三个正确命名为“Hanna”,“Josh”和“Anna”的文件及其值。但是,我想为我的所有数据文件运行脚本,并在新字段中将它们仅附加到一个“Hanna”文件等。

所以我正在寻找的是类似print $0 > $month name的东西,读出来就像“将记录打印到对应于月份的字段”

我试图找到一个解决方案,但大多数解决方案只是将临时文件粘贴在一起或者将值附加到现有文件之后(以便它们都在字段1中)。我想避免使用临时文件并将它们放在不同的字段中(这样我就得到了一种矩阵结构)。

提前谢谢!

1 个答案:

答案 0 :(得分:0)

尝试关注,但我没有检查所有排列和组合,只考虑了你的帖子。你的输出Josh列也不一致(或者如果有更多条件也请告诉我们)。让我知道它是怎么回事。

awk 'FNR==NR{if($0 ~ /^Values/){Q=$NF;B[$NF]=$NF;i="";next};A[Q,++i]=$0;next} /^Values/{V=$NF;print "#"B[V];i="";next} B[V]{print A[V,++i],$0}' file1 file2

编辑:也添加非单一形式的解决方案。

awk 'FNR==NR{
                if($0 ~ /^Values/){
                                        Q=$NF;
                                        B[$NF]=$NF;
                                        i="";
                                        next
                                  };
                A[Q,++i]=$0;
                next
            }
     /^Values/{
                V=$NF;
                print "#"B[V];
                i="";
                next
              }
     B[V]{
                print A[V,++i],$0
         }
    ' file1 file2

EDIT2:现在也为此添加说明。

awk 'FNR==NR{                                      ###Checking condition FNR==NR where this condition will be TRUE only when first file named file1 is being read. FNR and NR both indicate number of lines in a Input_file, only difference between them is FNR value will be RESET whenever there is next Input_file is being read and NR value will be keep on incresing till all the Input_files are read.
                if($0 ~ /^Values/){                ###Checking here if any line starts from string Values if yes then perform following operations.
                                        Q=$NF;     ###Creating a variable named Q whose value is the last field of the line.
                                        B[$NF]=$NF;###Creating an array named B whose index is $NF(last field of the line) and value is same too.
                                        i="";      ###Making variable i value to NULL now.
                                        next       ###using next here, it is built-in keyword for awk and it will skip all further statements now.
                                  };
                A[Q,++i]=$0;                       ###Creating an array named A whose index is Q and variable i with increasing value with 1 to it, each time it comes on this statement.
                next                               ###Using next will skip all further statements now.
            }
     /^Values/{                                    ###All statements from here will be executed when second file named file2 is being read. So I am checking here if a line starts from string Values then do following.
                V=$NF;                             ###create variable V whose value is $NF of current line.
                print "#"B[V];                     ###printing the string # then value of array B whose index is variable V.
                i="";                              ###Nullifying the variable i value here.
                next                               ###next will sip all the further statements now.
              }
     B[V]{                                         ###Checking here if array B with index V is having a value in it, then perform following on it too.
                print A[V,++i],$0                  ###printing the value of array A whose index is variable V and variable i increasing value with 1 and current line.
         }
    ' file1 file2                                  ###Mentioning the Input_files here named file1 and file2.