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?
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!
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 -n
is Debian specific.
This works fine
paste "$FILE1" "$FILE2" | awk -F'\t' '{printf("%-16s%s\n", $1, $2)}'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With