1

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


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

6
  • 1
    Can you please edit your post to include the input files you are using? This will greatly aid testing. Commented Apr 21, 2015 at 16:10
  • Sorry, I will do so right now. Commented Apr 21, 2015 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 Commented Apr 21, 2015 at 16:37
  • Thank you, steeldriver. No luck either with that code. I edited my post with a screenshot using the suggested code. Commented Apr 21, 2015 at 16:44
  • 1
    I'm sorry. I didn't know. Commented Apr 21, 2015 at 18:17

2 Answers 2

2

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

1
  • THANK YOU! I haven't slept all night. I would have never figured this out on my own. Commented Apr 21, 2015 at 17:40
0

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).

1
  • Thank you, but I'm having no luck with either code you suggested. Commented Apr 21, 2015 at 16:07

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.