awk line break after each file

前端 未结 3 1448
隐瞒了意图╮
隐瞒了意图╮ 2021-01-28 15:12

With this script every field is printed out according to the longest word of the current file, but needs to have a line break every file. How can this be achieved?



        
3条回答
  •  猫巷女王i
    2021-01-28 15:35

    #!/usr/bin/awk -f
    function beginfile (file) {
        split("", a)
        max = 0
        delim = ""
    }
    
    function endfile (file) {
        for (i = 1; i <= lines; i++) {
            printf "%s%-*s", delim, max, a[i]
            delim = " ,"
        }
        printf "\n"
    }
    
    FILENAME != _oldfilename \
         {
             if (_oldfilename != "")
                 endfile(_oldfilename)
             _oldfilename = FILENAME
             beginfile(FILENAME)
         }
    
         END   { endfile(FILENAME) }
    
    {
        len = length($0)
        if (len > max) {
            max = len
        }
        a[FNR] = $0
        lines = FNR
    }
    

    To run it:

    chmod u+x filename
    ./filename file1 file2
    

    Note that in gawk you can do delete a instead of split("", a). GAWK 4 has builtin BEGINFILE and ENDFILE.

提交回复
热议问题