BASH: Display two files simultaneously

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

pr -mtw $WIDTH $FILE1 $FILE2 

but he turned off 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 there was a way out

 โ”Œโ”€โ”€โ”€โ”€โ”ค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 result 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?

+5
source share
3 answers

It works great

 paste "$FILE1" "$FILE2" | awk -F'\t' '{printf("%-16s%s\n", $1, $2)}' 
+1
source

Using the awk , tr and GNU paste commands: -

 $ 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 awk pipe with the second, you donโ€™t know exactly how to do this. Otherwise it works!

+2
source

You can use the column command:

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

The paste command paste both files line by line with the delimiter,.

The column command will replace , with the necessary spaces, to properly defer it (with the -t option). -n tells column fill in an empty column. Note that the -n option is specific to Debian.

+1
source

Source: https://habr.com/ru/post/1258816/


All Articles