Merge two files horizontally and format

I have two files:

file_1

Austin Los Angeles York San Ramon 

file_2

 Texas California New York California 

I want to combine them horizontally as follows:

 Austin Texas Los Angeles California York New York San Ramon California 

I can combine horizontally using the paste command, but formatting goes haywire.

 Austin Texas Los Angeles California York New York San Ramon California 

I understand that paste works as intended, but can someone point me in the right direction to get the correct formatting.

Thanks.

+4
source share
3 answers

The insert uses the tab when the file is "merged", so perhaps you need to post-process the file and remove the tab with spaces:

 paste File_1 File_2 | awk 'BEGIN { FS = "\t" } ; {printf("%-20s%s\n",$1,$2) }' 

result:

 Austin Texas Los Angeles California York New York San Ramon California 
+4
source

First, you need to check the number of characters in the longest string. Than you can use fmt to output a line from the first file to a greater length. Finish it with pasta.

+1
source

If you have an idea of โ€‹โ€‹the width of the field, you can do something like this:

 IFS_BAK="$IFS" IFS=$'\t' paste file_1 file_2 \ | while read city state; do printf "%-15s %-15s\n" "$city" "$state" done IFS="$IFS_BAK" 

Or this shorter version:

 paste file_1 file_2 | while IFS=$'\t' read city state; do printf "%-15s %-15s\n" "$city" "$state" done 

Or use the column tool from bsdmainutils :

 paste file_1 file_2 | column -s $'\t' -t 
+1
source

All Articles