How to make awk produce columns

后端 未结 2 1307
忘掉有多难
忘掉有多难 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
    

提交回复
热议问题