Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH: Display two files side by side simultaneously

Tags:

text

bash

awk

I'm working on a bash script, and I want to print two files side by side. One file is filled with IPv4 addresses and the other one is filled with IPv6 addresses. I tried,

pr -mtw $WIDTH $FILE1 $FILE2

but it cut the output.

┌────┤IPV4├─────┬─────────────────┤IPV6├────────────────┐
 224.0.0.1       2001:0db8:0000:
 192.0.2.128     ff02::1
 192.0.2.128     2001:0db8:0000:
 192.0.2.128     ::
                 2001:0db8:0000:
                 2001:db8:0:0:0:
                 2001:db8::ff00:
                 0000:0000:0000:
                 ::1
                 fe80::
                 ::ffff:192.0.2.
                 ::192.0.2.128

I also tried,

paste $FILE1 $FILE2  | awk '$1=$1' OFS='\t '

and the output was,

┌────┤IPV4├─────┬─────────────────┤IPV6├────────────────┐
224.0.0.1    2001:0db8:0000:0042:0000:8a2e:0370:7334
192.0.2.128  ff02::1
192.0.2.128  2001:0db8:0000:0000:0000:ff00:0042:8329
192.0.2.128  ::
2001:0db8:0000:0000:0000:ff00:0042:8329
2001:db8:0:0:0:ff00:42:8329
2001:db8::ff00:42:8329
0000:0000:0000:0000:0000:0000:0000:0001
::1
fe80::
::ffff:192.0.2.128
::192.0.2.128

I want the output to be something like,

┌────┤IPV4├─────┬─────────────────┤IPV6├────────────────┐
 224.0.0.1       2001:0db8:0000:0042:0000:8a2e:0370:7334
 192.0.2.128     ff02::1
 192.0.2.128     2001:0db8:0000:0000:0000:ff00:0042:8329
 192.0.2.128     ::
                 2001:0db8:0000:0000:0000:ff00:0042:8329
                 2001:db8:0:0:0:ff00:42:8329
                 2001:db8::ff00:42:8329
                 0000:0000:0000:0000:0000:0000:0000:0001
                 ::1
                 fe80::
                 ::ffff:192.0.2.128
                 ::192.0.2.128

FILE1:

 224.0.0.1
 192.0.2.128
 192.0.2.128
 192.0.2.128

FILE2:

 2001:0db8:0000:0042:0000:8a2e:0370:7334
 ff02::1
 2001:0db8:0000:0000:0000:ff00:0042:8329
 ::
 2001:0db8:0000:0000:0000:ff00:0042:8329
 2001:db8:0:0:0:ff00:42:8329
 2001:db8::ff00:42:8329
 0000:0000:0000:0000:0000:0000:0000:0001
 ::1
 fe80::
 ::ffff:192.0.2.128
 ::192.0.2.128

Note that there is a space at the beginning of each line. Any ideas?

like image 710
xtonousou Avatar asked Oct 26 '16 10:10

xtonousou


3 Answers

Using awk, tr and GNU paste command:-

$ paste file1 file2 | awk -v FS='\t' '{printf("%-15s %s\n",$1,$2)}' | \
            awk '{sub(/^/, " ", $0)}1'

 224.0.0.1       2001:0db8:0000:0042:0000:8a2e:0370:7334
 192.0.2.128     ff02::1
 192.0.2.128     2001:0db8:0000:0000:0000:ff00:0042:8329
 192.0.2.128     ::
                 2001:0db8:0000:0000:0000:ff00:0042:8329
                 2001:db8:0:0:0:ff00:42:8329
                 2001:db8::ff00:42:8329
                 0000:0000:0000:0000:0000:0000:0000:0001
                 ::1
                 fe80::
                 ::ffff:192.0.2.128
                 ::192.0.2.128

You can optimize the last piped awk with 2nd one, was not sure exactly how to do it. Otherwise, this works!

like image 96
Inian Avatar answered Oct 13 '22 11:10

Inian


You can use the command column:

paste -d, file1 file2 | column  -s',' -n -t

The paste command will join both file line by line with the separator ,.

The column command will replace the , by the necessary spaces to have it indented correctly (with option -t). The -n is saying to column to fill empty column. Note the option -nis Debian specific.

like image 27
oliv Avatar answered Oct 13 '22 11:10

oliv


This works fine

paste "$FILE1" "$FILE2" | awk -F'\t' '{printf("%-16s%s\n", $1, $2)}'
like image 44
xtonousou Avatar answered Oct 13 '22 11:10

xtonousou