代码之家  ›  专栏  ›  技术社区  ›  Johanna Blomqvist

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

  •  2
  • Johanna Blomqvist  · 技术社区  · 7 年前

    我对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
    

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

    Print $0 > name

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

    print $0 > $month name ,读取类似“将记录打印到与月份对应的字段”

    提前谢谢!

    1 回复  |  直到 7 年前
        1
  •  0
  •   RavinderSingh13 Nikita Bakshi    7 年前

    尽管我并没有检查所有的排列和组合,只考虑了你们的帖子,但请尝试下面的内容。此外,您的输出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
    

    编辑2: 现在也为同样的问题添加了解释。

    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.