Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have searched online for 10 hours now and tried various codes, but still need help. I am attempting to merge three files using 'paste' and 'awk'. However, the columns are not adjusting to the longest string of characters. All files are formatted in the same manner as below.

  • F gge0001x
  • D 12-30-2006
  • T 14:15:20
  • S a69
  • B 15.8
  • M gge06001
  • P 30.1

Below is my faulty code.

$ paste <(awk '{print $1}' lineid) <
(awk '{printf("%-13.10s\n", $1)}' gge0001x) <
(awk '{printf("%-13.10s\n", $1)}' gge0001y) <
(awk '{printf("%-13.10s\n", $1)}' gge0001z)

This code results in misaligned columns as pictured below. Misaligned Columns

I would greatly appreciate your help.


Input File 1

F 
D 
T 
S 
B 
M 
P  
Q  
R  
U  
X 
A    
G    
H  
O  
C  
K  
W  
L  

Input File 2

gge0006x
12-30-2006
14:05:23
a69
15.4
gge06001
30.8 
19.2 
1006.2 
1012.7 
36.2
38.994   
107.71   
8.411 
37.084 
7.537 
28.198 
212.52 
68.1

Input File 3

gge0006y
12-30-2006 
14:05:55
a69
15.3
gge06001
30.6 
21.1 
1006.6 
1014.6 
36.1
38.994   
107.71   
8.433 
36.705 
7.621 
27.623 
210.51 
68 

Input File 4

gge0006z
12-30-2006
14:06:28
a69
15.7
gge06001
30.3 
23.5 
1008 
1014.1 
36.6
38.994   
107.71   
8.434 
36.508 
7.546 
27.574 
208.08 
67.6 

Results for paste file1 file2 file3 file4 | column -t enter image description here

share|improve this question
1  
Can you please edit your post to include the input files you are using? This will greatly aid testing. –  dhag Apr 21 at 16:10
    
Sorry, I will do so right now. –  M.Rubie Apr 21 at 16:12
1  
If you're just trying to format the output nicely have you considered using the column command instead of messing with the fields individually using awk? e.g. paste file1 file2 file3 file4 | column -t –  steeldriver Apr 21 at 16:37
    
Thank you, steeldriver. No luck either with that code. I edited my post with a screenshot using the suggested code. –  M.Rubie Apr 21 at 16:44
1  
I'm sorry. I didn't know. –  M.Rubie Apr 21 at 18:17

2 Answers 2

up vote 2 down vote accepted

Your input files have DOS \r\n line endings. Remove the carriage returns with the dos2unix command or with sed -i 's/\r$//'

share|improve this answer
    
THANK YOU! I haven't slept all night. I would have never figured this out on my own. –  M.Rubie Apr 21 at 17:40

The command paste separate entries with tabs, which are then interpreted as a variable number of spaces upon display. If your input is already padded with whitespace, you may try deleting tabs from the output of paste, with | tr -d '\t', or turning each tab into a single space,with| tr '\t' ' ' (on my system, the first can be achieved using paste -d '', but I don't know whether this is portable, and the second can be achieved with paste -d ' '; telling paste to use spaces as delimiters).

share|improve this answer
    
Thank you, but I'm having no luck with either code you suggested. –  M.Rubie Apr 21 at 16:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.