Tasneem
Tasneem

Reputation: 101

How to make awk produce columns

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
Orange#Fruit
Cucumber#Vegetable
Sweat Potato#Fruit

This line of code makes it like this:

Tomato   Vegetable
Orange   Fruit
Cucumber   Vegetable
Sweat Potato    Fruit

I am trying to make this file be displayed like this:

Tomato          Vegetable
Orange          Fruit
Cucumber        Vegetable
Sweat Potato    Fruit

What am I doing wrong that does not give me this result?

Upvotes: 1

Views: 93

Answers (2)

dawg
dawg

Reputation: 103744

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   

Upvotes: 2

James Brown
James Brown

Reputation: 37394

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

Upvotes: 2

Related Questions