Quantcast
Channel: Active questions tagged bash+awk - Ask Ubuntu
Viewing all articles
Browse latest Browse all 102

How to align the text file after copy and paste?

$
0
0

I copied column 7,8 and 9 from file 1 into columns 7,8 and 9 in file 2 producing a new file 3. the produced file (file 3) is not aligned as the original files, How can I edit it to preserve the alignment ?

I used the command:

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

file 1:

GRM in vacuum

192700

1GRM     C1    1  17.188   0.311  13.994 -0.5971  0.0204 -0.07241GRM     C2    2   0.094   0.383   0.005  0.4831 -0.8709 -0.22041GRM     C3    3   0.091   0.524   0.008 -0.7098  0.3449 -0.39521GRM     C4    4  17.185   0.596   0.007 -0.4066 -0.4079  0.17012GRM     C1    5  17.187   0.738   0.016  0.1844 -0.5441 -0.48932GRM     C2    6   0.090   0.815   0.011  1.1631 -0.2289 -0.83402GRM     C3    7   0.091   0.957   0.014 -0.4473 -0.2423 -0.32232GRM     C4    8  17.184   1.028   0.009 -0.4822  0.0086 -0.47913GRM     C1    9  17.189   1.171   0.004 -0.0345 -0.8207 -0.6602 

file 2:

GRM in vacuum

192760

1GRM     C1    1   0.061   0.071  14.000 1GRM     C2    2   0.184   0.142  14.0001GRM     C3    3   0.184   0.284   0.0001GRM     C4    4   0.061   0.355   0.0002GRM     C1    5   0.061   0.497  14.0002GRM     C2    6   0.184   0.568   0.0002GRM     C3    7   0.184   0.710   0.0002GRM     C4    8   0.061   0.781   0.0003GRM     C1    9   0.061   0.923   0.0003GRM     C2   10   0.184   0.994  14.0003GRM     C3   11   0.184   1.136  14.0003GRM     C4   12   0.061   1.207  14.0004GRM     C1   13   0.061   1.349  14.0004GRM     C2   14   0.184   1.420  14.0004GRM     C3   15   0.184   1.562   0.0004GRM     C4   16   0.061   1.633  14.0005GRM     C1   17   0.061   1.775   0.0005GRM     C2   18   0.184   1.846   0.0025GRM     C3   19   0.184   1.988   0.0015GRM     C4   20   0.061   2.059   0.000

file 3:

GRM in vacuum

192760

1GRM C1 1 0.061 0.071 14.000 -0.5971 0.0204 -0.07241GRM C2 2 0.184 0.142 14.000 0.4831 -0.8709 -0.22041GRM C3 3 0.184 0.284 0.000 -0.7098 0.3449 -0.39521GRM C4 4 0.061 0.355 0.000 -0.4066 -0.4079 0.17012GRM C1 5 0.061 0.497 14.000 0.1844 -0.5441 -0.48932GRM C2 6 0.184 0.568 0.000 1.1631 -0.2289 -0.83402GRM C3 7 0.184 0.710 0.000 -0.4473 -0.2423 -0.32232GRM C4 8 0.061 0.781 0.000 -0.4822 0.0086 -0.47913GRM C1 9 0.061 0.923 0.000 -0.0345 -0.8207 -0.66023GRM C2 10 0.184 0.994 14.000 0.1033 0.4238 -0.89833GRM C3 11 0.184 1.136 14.000 -0.0426 -0.1083 -0.85543GRM C4 12 0.061 1.207 14.000 -0.2814 -0.0363 -0.91464GRM C1 13 0.061 1.349 14.000 0.7722 0.4317 -0.43794GRM C2 14 0.184 1.420 14.000 -0.5701 0.2915 -0.30984GRM C3 15 0.184 1.562 0.000 0.4572 0.1368 0.19214GRM C4 16 0.061 1.633 14.000 -0.0747 -0.5492 -0.12545GRM C1 17 0.061 1.775 0.000 0.3071 0.9864 -0.86795GRM C2 18 0.184 1.846 0.002 -0.7815 -0.8144 -0.00695GRM C3 19 0.184 1.988 0.001 -0.0089 0.1174 0.25485GRM C4 20 0.061 2.059 0.000 0.7953 0.1404 -1.12256GRM C1 21 0.061 2.201 14.000 -0.0370 0.1705 -0.33426GRM C2 22 0.184 2.272 14.000 -0.2619 0.4221 -0.65726GRM C3 23 0.184 2.414 14.000 -0.4540 -0.3948 -0.62046GRM C4 24 0.061 2.485 14.000 -0.1499 -0.2942 -0.5181

the Format I wanted to have is the same format as file 1 and 2 (spaces between columns)

I tried the below commands as advised by schrodigerscatcuriosity and steeldriver, However I still have a problem from line 10002 to the end, please check below the commands I tried and the output:

    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    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"> file 3

both of them gave me the below:

 2499GRM     C2 9994  15.433   7.810   0.001  0.2740 -0.1307  0.2597 2499GRM     C3 9995  15.433   7.952   0.000 -0.0604 -0.1317  0.2130 2499GRM     C4 9996  15.310   8.023   0.001 -0.0362 -0.0640 -0.3092 2500GRM     C1 9997  15.310   8.165   0.001 -0.0183 -0.0765 -0.0458 2500GRM     C2 9998  15.433   8.236   0.000 -0.2414  0.3310  0.3241 2500GRM     C3 9999  15.433   8.378   0.000 -0.2669  0.3179  0.8367 2500GRMC410000   15   8.449   0.000   0.000  0.5860  0.8426  0.0000 #(This line is 10002) 2501GRMC110001   15   8.591   0.000   0.000  0.5939 -0.1623  0.0000 2501GRMC210002   15   8.662   0.000   0.000  0.4561 -0.1660  0.0000

I tried to increase the spaces using

 awk 'BEGIN{fmt="%10s%9s%7d%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"> file 3

as advised by , and I got the below output:

   2499GRM       C3   9995  15.433   7.952   0.000 -0.0604 -0.1317  0.2130   2499GRM       C4   9996  15.310   8.023   0.001 -0.0362 -0.0640 -0.3092   2500GRM       C1   9997  15.310   8.165   0.001 -0.0183 -0.0765 -0.0458   2500GRM       C2   9998  15.433   8.236   0.000 -0.2414  0.3310  0.3241   2500GRM       C3   9999  15.433   8.378   0.000 -0.2669  0.3179  0.8367   2500GRM  C410000     15   8.449   0.000   0.000  0.5860  0.8426  0.0000 # (This line is 10002)   2501GRM  C110001     15   8.591   0.000   0.000  0.5939 -0.1623  0.0000   2501GRM  C210002     15   8.662   0.000   0.000  0.4561 -0.1660  0.0000   2501GRM  C310003     15   8.804   0.000   0.000  0.4524 -0.1486  0.0000   2501GRM  C410004     15   8.875   0.000   0.000 -0.2143  0.4926  0.0000   2502GRM  C110005     15   9.017   0.000   0.000 -0.2203  0.2975  0.0000   2502GRM  C210006     15   9.088   0.000   0.000 -0.2375 -0.0458  0.0000   2502GRM  C310007     15   9.230  14.000   0.000 -0.2338 -0.4862  0.0000   2502GRM  C410008     15   9.301  14.000   0.000 -0.1438 -0.0725  0.0000

The original file looks as below at the same part (at line 10002):

file 1:

 2499GRM     C3 9995  15.167   8.231  13.994 -0.0604 -0.1317  0.2130 2499GRM     C4 9996  15.045   8.304  13.996 -0.0362 -0.0640 -0.3092 2500GRM     C1 9997  15.046   8.446   0.001 -0.0183 -0.0765 -0.0458 2500GRM     C2 9998  15.170   8.516  13.999 -0.2414  0.3310  0.3241 2500GRM     C3 9999  15.172   8.658   0.003 -0.2669  0.3179  0.8367 2500GRM     C410000  15.049   8.727   0.006 -0.1138  0.5860  0.8426 #(line 10002) 2501GRM     C110001  15.048   8.869   0.006 -0.0660  0.5939 -0.1623 2501GRM     C210002  15.171   8.940   0.006  0.0179  0.4561 -0.1660 2501GRM     C310003  15.173   9.082   0.005  0.3025  0.4524 -0.1486

file 2:

 2499GRM     C3 9995  15.433   7.952   0.000 2499GRM     C4 9996  15.310   8.023   0.001 2500GRM     C1 9997  15.310   8.165   0.001 2500GRM     C2 9998  15.433   8.236   0.000 2500GRM     C3 9999  15.433   8.378   0.000 2500GRM     C410000  15.310   8.449   0.000     #(This line is 10002) 2501GRM     C110001  15.310   8.591   0.000 2501GRM     C210002  15.433   8.662   0.000 2501GRM     C310003  15.433   8.804   0.000 2501GRM     C410004  15.310   8.875   0.000

It is worth mentioning that the problem happens at line 10002


Viewing all articles
Browse latest Browse all 102

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>