Aligning Text using Sed and Awk

Dear All,

I have been working on aligning contents of a text file but I am having problems. I have the following text file called "tempThermody.txt" and I need to align all the last values from row 3 to appear as the 80th column. The values that will appear as the 80th columns are 1,2,3 and 4. I have achieved this as shown in the file "thermody.txt" below. The problem is that some of the first row values do not start from column 1 and the application I am running requires that they begin from column 1. Apart from that, the values in between columns 1 and 80 need to start from certain specific columns which I have also achieved. Please how can I adjust the file tempThermody.txt:

THERMO
300.0 1500.0 5000.0
Ar                     Ar    1           g   300.00  5000.00  1000.00       1
2.5000                 0.0000 0.0000      0.0000   0.0000                   2
-745.3750                 4.3660 2.5000      0.0000   0.0000                3
0.0000                 0.0000 -745.3750      4.3660                   4
N2                      N    2           g   300.00  5000.00  1000.00       1
3.2987                 0.0014 -0.0000      0.0000  -0.0000                   2
-1020.8999                 3.9504 2.9266      0.0015  -0.0000                3
0.0000                -0.0000 -922.7977      5.9805                   4
H                      H    1           g   200.00  3500.00  1000.00       1
2.5000                 0.0000 -0.0000      0.0000  -0.0000                   2
25473.6599                -0.4467 2.5000     -0.0000   0.0000                3
-0.0000                 0.0000 25473.6599     -0.4467                   4
H2                      H    2           g   200.00  3500.00  1000.00       1
2.3443                 0.0080 -0.0000      0.0000  -0.0000                   2
-917.9352                 0.6830 3.3373     -0.0000   0.0000                3
-0.0000                 0.0000 -950.1589     -3.2050                   4
O                      O    1           g   200.00  3500.00  1000.00       1
3.1683                -0.0033 0.0000     -0.0000   0.0000                   2
29122.2592                 2.0519 2.5694     -0.0001   0.0000                3
-0.0000                 0.0000 29217.5791      4.7843                   4
O2                      O    2           g   200.00  3500.00  1000.00       1
3.7825                -0.0030 0.0000     -0.0000   0.0000                   2
-1063.9436                 3.6577 3.2825      0.0015  -0.0000                3
0.0000                -0.0000 -1088.4577      5.4532                   4
OH                      H    1           g   200.00  3500.00  1000.00       1
3.9920                -0.0024 0.0000     -0.0000   0.0000                   2
3615.0806                -0.1039 3.0929      0.0005   0.0000                3
-0.0000                 0.0000 3858.6570      4.4767                   4
OH                      O    1           g   200.00  3500.00  1000.00       1
3.9920                -0.0024 0.0000     -0.0000   0.0000                   2
3615.0806                -0.1039 3.0929      0.0005   0.0000                3
-0.0000                 0.0000 3858.6570      4.4767                   4
H2O                      H    2           g   200.00  3500.00  1000.00       1
4.1986                -0.0020 0.0000     -0.0000   0.0000                   2
-30293.7267                -0.8490 3.0340      0.0022  -0.0000                3
-0.0000                 0.0000 -30004.2971      4.9668                   4
H2O                      O    1           g   200.00  3500.00  1000.00       1
4.1986                -0.0020 0.0000     -0.0000   0.0000                   2
-30293.7267                -0.8490 3.0340      0.0022  -0.0000                3
-0.0000                 0.0000 -30004.2971      4.9668                   4
HO2                      H    1           g   200.00  3500.00  1000.00       1
4.3018                -0.0047 0.0000     -0.0000   0.0000                   2
294.8080                 3.7167 4.0172      0.0022  -0.0000                3
0.0000                -0.0000 111.8567      3.7851                   4
HO2                      O    2           g   200.00  3500.00  1000.00       1
4.3018                -0.0047 0.0000     -0.0000   0.0000                   2
294.8080                 3.7167 4.0172      0.0022  -0.0000                3
0.0000                -0.0000 111.8567      3.7851                   4
H2O2                      H    2           g   200.00  3500.00  1000.00       1
4.2761                -0.0005 0.0000     -0.0000   0.0000                   2
-17702.5821                 3.4351 4.1650      0.0049  -0.0000                3
0.0000                -0.0000 -17861.7877      2.9162                   4
H2O2                      O    2           g   200.00  3500.00  1000.00       1
4.2761                -0.0005 0.0000     -0.0000   0.0000                   2
-17702.5821                 3.4351 4.1650      0.0049  -0.0000                3
0.0000                -0.0000 -17861.7877      2.9162                   4
END


I used this code:
sed -e :a -e 's/^.\{30,79\}$/ &/;ta'  tempThermody.txt > thermody.txt



To produce thermody.txt as:
THERMO
300.0 1500.0 5000.0
Ar                           1Ar            g   300.00  5000.00  1000.00       1
   2.5000                 0.0000 0.0000      0.0000   0.0000                   2
   -745.3750                 4.3660 2.5000      0.0000   0.0000                3
         0.0000                 0.0000 -745.3750      4.3660                   4
N2                         N    2           g   300.00  5000.00  1000.00       1
  3.2987                 0.0014 -0.0000      0.0000  -0.0000                   2
  -1020.8999                 3.9504 2.9266      0.0015  -0.0000                3
         0.0000                -0.0000 -922.7977      5.9805                   4
H                          H    1           g   200.00  3500.00  1000.00       1
  2.5000                 0.0000 -0.0000      0.0000  -0.0000                   2
  25473.6599                -0.4467 2.5000     -0.0000   0.0000                3
       -0.0000                 0.0000 25473.6599     -0.4467                   4
H2                         H    2           g   200.00  3500.00  1000.00       1
  2.3443                 0.0080 -0.0000      0.0000  -0.0000                   2
   -917.9352                 0.6830 3.3373     -0.0000   0.0000                3
        -0.0000                 0.0000 -950.1589     -3.2050                   4
O                          O    1           g   200.00  3500.00  1000.00       1
   3.1683                -0.0033 0.0000     -0.0000   0.0000                   2
  29122.2592                 2.0519 2.5694     -0.0001   0.0000                3
       -0.0000                 0.0000 29217.5791      4.7843                   4
O2                         O    2           g   200.00  3500.00  1000.00       1
   3.7825                -0.0030 0.0000     -0.0000   0.0000                   2
  -1063.9436                 3.6577 3.2825      0.0015  -0.0000                3
        0.0000                -0.0000 -1088.4577      5.4532                   4
OH                         H    1           g   200.00  3500.00  1000.00       1
   3.9920                -0.0024 0.0000     -0.0000   0.0000                   2
   3615.0806                -0.1039 3.0929      0.0005   0.0000                3
        -0.0000                 0.0000 3858.6570      4.4767                   4
OH                         O    1           g   200.00  3500.00  1000.00       1
   3.9920                -0.0024 0.0000     -0.0000   0.0000                   2
   3615.0806                -0.1039 3.0929      0.0005   0.0000                3
        -0.0000                 0.0000 3858.6570      4.4767                   4
H2O                        H    2           g   200.00  3500.00  1000.00       1
   4.1986                -0.0020 0.0000     -0.0000   0.0000                   2
 -30293.7267                -0.8490 3.0340      0.0022  -0.0000                3
      -0.0000                 0.0000 -30004.2971      4.9668                   4
H2O                        O    1           g   200.00  3500.00  1000.00       1
   4.1986                -0.0020 0.0000     -0.0000   0.0000                   2
 -30293.7267                -0.8490 3.0340      0.0022  -0.0000                3
      -0.0000                 0.0000 -30004.2971      4.9668                   4
HO2                        H    1           g   200.00  3500.00  1000.00       1
   4.3018                -0.0047 0.0000     -0.0000   0.0000                   2
    294.8080                 3.7167 4.0172      0.0022  -0.0000                3
          0.0000                -0.0000 111.8567      3.7851                   4
HO2                        O    2           g   200.00  3500.00  1000.00       1
   4.3018                -0.0047 0.0000     -0.0000   0.0000                   2
    294.8080                 3.7167 4.0172      0.0022  -0.0000                3
          0.0000                -0.0000 111.8567      3.7851                   4
H2O2                       H    2           g   200.00  3500.00  1000.00       1
   4.2761                -0.0005 0.0000     -0.0000   0.0000                   2
 -17702.5821                 3.4351 4.1650      0.0049  -0.0000                3
       0.0000                -0.0000 -17861.7877      2.9162                   4
H2O2                       O    2           g   200.00  3500.00  1000.00       1
   4.2761                -0.0005 0.0000     -0.0000   0.0000                   2
 -17702.5821                 3.4351 4.1650      0.0049  -0.0000                3
       0.0000                -0.0000 -17861.7877      2.9162                   4
END

The thermody.txt has maximum of 80 columns as required except that some of the first row values do not start from column 1. I want all first strings to start from column 1 and the last numeric value at column 80 for all lines starting from the third row (
Ar                           1Ar            g   300.00  5000.00  1000.00       1
) to the second to the last row (
0.0000                -0.0000 -17861.7877      2.9162                   4
)

Please find the two files (tempThermody.txt and thermody.txt) as attachments.

Any help will be appreciated.
Kind regards

Comments

  • don't use sed for this. use awk.

    awk's printf function provides formatted print output like the ForTan, c, c++ function of the same name.

    As you probably know, awk will automatically read one line from your text file per iteration. Fields are normally separated by spaces but you can change the ENV "FS" variable to use comma delimited list, etc. Fields are assigned $1,$2,$3... $0 will return the entire line.

    I would do something like this:
    /.*/ { printf("%4.1f    %4.f      %4.1f        %4.1f",$1,$2,$3,$4) }
    

    Play with this a little and you should get the idea on how to generate a table with controlled formatting.

    Best of Luck on your project
Sign In or Register to comment.