命令行 - 复制和粘贴后,如何对齐文本文件?

分享于 

7分钟阅读

ubuntu

 

问题:

我使用了命令:


 awk '(getline line <"file 1") > -1 {split(line,a); $7 = a[7]; $8 = a[8]; $9= a[9]} 1' file 2 > file 3



文件1:


1GRM C1 1 17.188 0.311 13.994 -0.5971 0.0204 -0.0724


1GRM C2 2 0.094 0.383 0.005 0.4831 -0.8709 -0.2204


1GRM C3 3 0.091 0.524 0.008 -0.7098 0.3449 -0.3952


1GRM C4 4 17.185 0.596 0.007 -0.4066 -0.4079 0.1701


2GRM C1 5 17.187 0.738 0.016 0.1844 -0.5441 -0.4893


2GRM C2 6 0.090 0.815 0.011 1.1631 -0.2289 -0.8340


2GRM C3 7 0.091 0.957 0.014 -0.4473 -0.2423 -0.3223


2GRM C4 8 17.184 1.028 0.009 -0.4822 0.0086 -0.4791


3GRM C1 9 17.189 1.171 0.004 -0.0345 -0.8207 -0.6602 



文件2:


1GRM C1 1 0.061 0.071 14.000 


1GRM C2 2 0.184 0.142 14.000


1GRM C3 3 0.184 0.284 0.000


1GRM C4 4 0.061 0.355 0.000


2GRM C1 5 0.061 0.497 14.000


2GRM C2 6 0.184 0.568 0.000


2GRM C3 7 0.184 0.710 0.000


2GRM C4 8 0.061 0.781 0.000


3GRM C1 9 0.061 0.923 0.000


3GRM C2 10 0.184 0.994 14.000


3GRM C3 11 0.184 1.136 14.000


3GRM C4 12 0.061 1.207 14.000


4GRM C1 13 0.061 1.349 14.000


4GRM C2 14 0.184 1.420 14.000


4GRM C3 15 0.184 1.562 0.000


4GRM C4 16 0.061 1.633 14.000


5GRM C1 17 0.061 1.775 0.000


5GRM C2 18 0.184 1.846 0.002


5GRM C3 19 0.184 1.988 0.001


5GRM C4 20 0.061 2.059 0.000



文件3:


1GRM C1 1 0.061 0.071 14.000 -0.5971 0.0204 -0.0724


1GRM C2 2 0.184 0.142 14.000 0.4831 -0.8709 -0.2204


1GRM C3 3 0.184 0.284 0.000 -0.7098 0.3449 -0.3952


1GRM C4 4 0.061 0.355 0.000 -0.4066 -0.4079 0.1701


2GRM C1 5 0.061 0.497 14.000 0.1844 -0.5441 -0.4893


2GRM C2 6 0.184 0.568 0.000 1.1631 -0.2289 -0.8340


2GRM C3 7 0.184 0.710 0.000 -0.4473 -0.2423 -0.3223


2GRM C4 8 0.061 0.781 0.000 -0.4822 0.0086 -0.4791


3GRM C1 9 0.061 0.923 0.000 -0.0345 -0.8207 -0.6602


3GRM C2 10 0.184 0.994 14.000 0.1033 0.4238 -0.8983


3GRM C3 11 0.184 1.136 14.000 -0.0426 -0.1083 -0.8554


3GRM C4 12 0.061 1.207 14.000 -0.2814 -0.0363 -0.9146


4GRM C1 13 0.061 1.349 14.000 0.7722 0.4317 -0.4379


4GRM C2 14 0.184 1.420 14.000 -0.5701 0.2915 -0.3098


4GRM C3 15 0.184 1.562 0.000 0.4572 0.1368 0.1921


4GRM C4 16 0.061 1.633 14.000 -0.0747 -0.5492 -0.1254


5GRM C1 17 0.061 1.775 0.000 0.3071 0.9864 -0.8679


5GRM C2 18 0.184 1.846 0.002 -0.7815 -0.8144 -0.0069


5GRM C3 19 0.184 1.988 0.001 -0.0089 0.1174 0.2548


5GRM C4 20 0.061 2.059 0.000 0.7953 0.1404 -1.1225


6GRM C1 21 0.061 2.201 14.000 -0.0370 0.1705 -0.3342


6GRM C2 22 0.184 2.272 14.000 -0.2619 0.4221 -0.6572


6GRM C3 23 0.184 2.414 14.000 -0.4540 -0.3948 -0.6204


6GRM C4 24 0.061 2.485 14.000 -0.1499 -0.2942 -0.5181




答案1:

你可以尝试以下技巧:


awk '(getline line <"file_1") > -1 {split(line,a); $7 = a[7]; $8 = a[8]; $9= a[9]} 1' file_2 | rev | column -t | rev > file_3



输出:


1GRM C1 1 0.061 0.071 14.000 -0.5971 0.0204 -0.0724


1GRM C2 2 0.184 0.142 14.000 0.4831 -0.8709 -0.2204


1GRM C3 3 0.184 0.284 0.000 -0.7098 0.3449 -0.3952


1GRM C4 4 0.061 0.355 0.000 -0.4066 -0.4079 0.1701


2GRM C1 5 0.061 0.497 14.000 0.1844 -0.5441 -0.4893


2GRM C2 6 0.184 0.568 0.000 1.1631 -0.2289 -0.8340


2GRM C3 7 0.184 0.710 0.000 -0.4473 -0.2423 -0.3223


2GRM C4 8 0.061 0.781 0.000 -0.4822 0.0086 -0.4791


3GRM C1 9 0.061 0.923 0.000 -0.0345 -0.8207 -0.6602


3GRM C2 10 0.184 0.994 14.000 -0.0345 -0.8207 -0.6602


3GRM C3 11 0.184 1.136 14.000 -0.0345 -0.8207 -0.6602


3GRM C4 12 0.061 1.207 14.000 -0.0345 -0.8207 -0.6602


4GRM C1 13 0.061 1.349 14.000 -0.0345 -0.8207 -0.6602


4GRM C2 14 0.184 1.420 14.000 -0.0345 -0.8207 -0.6602


4GRM C3 15 0.184 1.562 0.000 -0.0345 -0.8207 -0.6602


4GRM C4 16 0.061 1.633 14.000 -0.0345 -0.8207 -0.6602


5GRM C1 17 0.061 1.775 0.000 -0.0345 -0.8207 -0.6602


5GRM C2 18 0.184 1.846 0.002 -0.0345 -0.8207 -0.6602


5GRM C3 19 0.184 1.988 0.001 -0.0345 -0.8207 -0.6602


5GRM C4 20 0.061 2.059 0.000 -0.0345 -0.8207 -0.6602




答案2:

如果需要严格对齐,可以考虑使用显式格式化的printfsprintf语句。


$ awk '


 BEGIN{fmt="%8s%7s%5d%8.3f%8.3f%8.3f%8.4f%8.4f%8.4f"} 


 (getline line <"file 1") > -1 {n = split(line,a)} 


 n > 6 {$0 = sprintf(fmt, $1,$2,$3,$4,$5,$6,a[7],a[8],a[9])}


 1


 '"file 2"


GRM in vacuum



192760



 1GRM C1 1 0.061 0.071 14.000 -0.5971 0.0204 -0.0724


 1GRM C2 2 0.184 0.142 14.000 0.4831 -0.8709 -0.2204


 1GRM C3 3 0.184 0.284 0.000 -0.7098 0.3449 -0.3952


 1GRM C4 4 0.061 0.355 0.000 -0.4066 -0.4079 0.1701


 2GRM C1 5 0.061 0.497 14.000 0.1844 -0.5441 -0.4893


 2GRM C2 6 0.184 0.568 0.000 1.1631 -0.2289 -0.8340


 2GRM C3 7 0.184 0.710 0.000 -0.4473 -0.2423 -0.3223


 2GRM C4 8 0.061 0.781 0.000 -0.4822 0.0086 -0.4791


 3GRM C1 9 0.061 0.923 0.000 -0.0345 -0.8207 -0.6602


 3GRM C2 10 0.184 0.994 14.000 -0.0345 -0.8207 -0.6602


 3GRM C3 11 0.184 1.136 14.000 -0.0345 -0.8207 -0.6602


 3GRM C4 12 0.061 1.207 14.000 -0.0345 -0.8207 -0.6602


 4GRM C1 13 0.061 1.349 14.000 -0.0345 -0.8207 -0.6602


 4GRM C2 14 0.184 1.420 14.000 -0.0345 -0.8207 -0.6602


 4GRM C3 15 0.184 1.562 0.000 -0.0345 -0.8207 -0.6602


 4GRM C4 16 0.061 1.633 14.000 -0.0345 -0.8207 -0.6602


 5GRM C1 17 0.061 1.775 0.000 -0.0345 -0.8207 -0.6602


 5GRM C2 18 0.184 1.846 0.002 -0.0345 -0.8207 -0.6602


 5GRM C3 19 0.184 1.988 0.001 -0.0345 -0.8207 -0.6602


 5GRM C4 20 0.061 2.059 0.000 -0.0345 -0.8207 -0.6602





COM  文件  COMM  Comma  命令行  cop  
相关文章