Home > database >  align lines when pasting files in bash
align lines when pasting files in bash

Time:12-07

How to align the rows when pasting files into columns?

Example:

$ paste <(echo -e 'bike\ncar\nlane\nroad\nwheel') <(echo -e 'car\nwheel') <(echo -e 'bike\nlane\nwheel') | column -s $'\t' -t
bike   car    bike
car    wheel  lane
lane          wheel
road
wheel

and the desired output is:

bike          bike
car    car    
lane          lane
road
wheel  wheel  wheel

CodePudding user response:

To align the rows

$ paste file1 file2 | column -t

If you want to make the output easier to read, you can also specify the delimiter to use when pasting. Like a comma or tab.

$ paste -d '\t' file1 file2 | column -t

CodePudding user response:

Is that what your looking for?

awk -v OFS='\t' '
    FNR == 1 {   FILENUM }
    { arr[$0] = arr[$0] FS FILENUM }
    END {
        for (ln in arr)
            for (i = 1; i <= FILENUM; i  )
                printf( "%s%s", \
                    (match(arr[ln], "(^| )"i"( |$)") ? ln : "-" ), \
                    (i < FILENUM ? OFS : ORS) \
                )
    }
' <(echo -e 'bike\ncar\nlane\nroad\nwheel') \
  <(echo -e 'car\nwheel') \
  <(echo -e 'bike\nlane\nwheel')
lane    -   lane
bike    -   bike
wheel   wheel   wheel
car car -
road    -   -
  • Related