How to make awk produce columns

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

    Prints:

    Tomato        Vegetable  
    Orange        Fruit      
    Cucumber      Vegetable  
    Sweat Potato  Fruit   
    

提交回复
热议问题