How to make awk produce columns

后端 未结 2 1308
忘掉有多难
忘掉有多难 2021-01-24 06:15

I have a file with two columns. Each line in this file has a different size of words

awk \'$5=$9\' FS="#" OFS="\\t" file1**


Tomato#Vegetable         


        
相关标签:
2条回答
  • 2021-01-24 06:28

    Another awk, that reads the file once, hashes the records to memory (expected that you have enough memory available) and format and outputs in the end:

    $ awk -F\# '{                              # define record separator
        a[NR]=$1                               # store 1st field
        b[NR]=$2                               # store 2nd field
        m=((n=length($1))>m?n:m)               # figure out the max length of 1st field
    }
    END {                                      # in the end
        for(i=1;i<=NR;i++)                     # iterate stored fields
            printf "%-" m+4 "s%s\n",a[i],b[i]  # output
    }' file
    

    Output:

    Tomato          Vegetable
    Orange          Fruit
    Cucumber        Vegetable
    Sweat Potato    Fruit
    
    0 讨论(0)
  • 2021-01-24 06:43

    With Unix / Linux you can use column to produce column output:

    $ column -t -s '#' your_file
    Tomato        Vegetable
    Orange        Fruit
    Cucumber      Vegetable
    Sweat Potato  Fruit
    

    To do it in awk you can read the file twice to get the max field length for each column. Once you do that, you can fix the field width with sprintf with the field width + pad value:

    awk -F'#' -v pad=4 '
            FNR==NR {                    # FNR==NR means first file
                for (i=1;i<=NF;i++)      # find the max width of each field
                    if(max[i]<length($i)) max[i]=length($i)
                next                     # next record of first file
                } 
                {                        # Second file
                    s=""                 # reset the buffer string
                    for (i=1;i<=NF;i++)  # build the buffer field by field
                        s=s sprintf("%-*s%*s", max[i], $i, pad, "")
                    print s              # print the buffer
                } '    your_file your_file
    

    Prints:

    Tomato        Vegetable  
    Orange        Fruit      
    Cucumber      Vegetable  
    Sweat Potato  Fruit   
    
    0 讨论(0)
提交回复
热议问题