重新考虑列间距

重新考虑列间距

我有一个具有以下格式的文件

1      1      1 0.02484183   57.2400000   24.6000000  
2      1      2 -0.16091000   56.5200000   24.5600000 
3      1      1 0.02484183   55.5700000   24.4900000  
4      1      1 0.02484183   56.4400000   25.5100000  
5      1      2 0.08638400   56.8900000   23.3300000  
6      1      1 0.01376475   57.8900000   23.5200000  
7      1      1 0.01376475   57.0600000   22.5200000  
8      1      2 0.17612200   55.8800000   22.9700000  
9      1      1 0.02042900   56.1600000   22.0400000  
10      1      2 -0.34768900   55.9300000   24.0700000
11      1      1 0.07439383   55.5100000   25.0200000 
12      1      1 0.07439383   55.4600000   23.7100000 
13      1      1 0.07439383   57.0000000   24.2400000 
14      1      2 0.02151800   54.4400000   22.8700000 
15      1      1 0.08793975   54.5000000   22.4000000 

我想生成一个具有以下格式的文件

 1      1      1  0.02484183   57.2400000   24.6000000  
 2      1      2 -0.16091000   56.5200000   24.5600000 
 3      1      1  0.02484183   55.5700000   24.4900000  
 4      1      1  0.02484183   56.4400000   25.5100000  
 5      1      2  0.08638400   56.8900000   23.3300000  
 6      1      1  0.01376475   57.8900000   23.5200000  
 7      1      1  0.01376475   57.0600000   22.5200000  
 8      1      2  0.17612200   55.8800000   22.9700000  
 9      1      1  0.02042900   56.1600000   22.0400000  
10      1      2 -0.34768900   55.9300000   24.0700000
11      1      1  0.07439383   55.5100000   25.0200000 
12      1      1  0.07439383   55.4600000   23.7100000 
13      1      1  0.07439383   57.0000000   24.2400000 
14      1      2  0.02151800   54.4400000   22.8700000 
15      1      1  0.08793975   54.5000000   22.4000000 

答案1

awk 使用 printf+%f

尝试这个。通过使用修改了宽度的 printf(例如%2d,根据需要填充该字段)。同样,% 2.8f这里格式化一个浮点数。

$ awk '{printf "%2d%7d%7d % 2.8f  % 2.7f  % 2.7f\n",$1,$2,$3,$4,$5,$6}' file
 1      1      1  0.02484183   57.2400000   24.6000000
 2      1      2 -0.16091000   56.5200000   24.5600000
 3      1      1  0.02484183   55.5700000   24.4900000
 4      1      1  0.02484183   56.4400000   25.5100000
 5      1      2  0.08638400   56.8900000   23.3300000
 6      1      1  0.01376475   57.8900000   23.5200000
 7      1      1  0.01376475   57.0600000   22.5200000
 8      1      2  0.17612200   55.8800000   22.9700000
 9      1      1  0.02042900   56.1600000   22.0400000
10      1      2 -0.34768900   55.9300000   24.0700000
11      1      1  0.07439383   55.5100000   25.0200000
12      1      1  0.07439383   55.4600000   23.7100000
13      1      1  0.07439383   57.0000000   24.2400000
14      1      2  0.02151800   54.4400000   22.8700000
15      1      1  0.08793975   54.5000000   22.4000000
$

在线尝试一下!

awk 使用 printf+tabs

还建议使用制表符 (\t) 方法使代码更加清晰,但它并不能完全按照您的要求实现对齐。

$ awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}' file
1       1       1       0.02484183      57.2400000      24.6000000
2       1       2       -0.16091000     56.5200000      24.5600000
3       1       1       0.02484183      55.5700000      24.4900000
4       1       1       0.02484183      56.4400000      25.5100000
5       1       2       0.08638400      56.8900000      23.3300000
6       1       1       0.01376475      57.8900000      23.5200000
7       1       1       0.01376475      57.0600000      22.5200000
8       1       2       0.17612200      55.8800000      22.9700000
9       1       1       0.02042900      56.1600000      22.0400000
10      1       2       -0.34768900     55.9300000      24.0700000

无代码版本,专栏

同样,不完全是您要求的输出,但使用column.

$ column -t file
1   1  1  0.02484183   57.2400000  24.6000000
2   1  2  -0.16091000  56.5200000  24.5600000
3   1  1  0.02484183   55.5700000  24.4900000
4   1  1  0.02484183   56.4400000  25.5100000
5   1  2  0.08638400   56.8900000  23.3300000
6   1  1  0.01376475   57.8900000  23.5200000
7   1  1  0.01376475   57.0600000  22.5200000
8   1  2  0.17612200   55.8800000  22.9700000
9   1  1  0.02042900   56.1600000  22.0400000
10  1  2  -0.34768900  55.9300000  24.0700000
11  1  1  0.07439383   55.5100000  25.0200000
12  1  1  0.07439383   55.4600000  23.7100000
13  1  1  0.07439383   57.0000000  24.2400000
14  1  2  0.02151800   54.4400000  22.8700000
15  1  1  0.08793975   54.5000000  22.4000000
$

答案2

使用 读取文件awk并使用 输出值printf

awk '{ printf ("%2d\t%s\t%s\t%11s\t%s\t%s\n", $1, $2, $3, $4, $5, $6); }' file

根据需要调整 printf 格式字符串。

相关内容