How to change a number on a specific lines in a file with shell?

Hello

My problem is that I want to change some specific numbers in a file. It is like,

 2009 10 3 2349 21.3 L  40.719  27.388 10.8  FRO  7 0.8 1.1LFRO 2.6CFRO 1.1LMAM1
 GAP=157        1.69       5.7     5.9  5.8  0.5405E+01  0.4455E+00  0.1653E+02E
 STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO AIN AR TRES W  DIS CAZ7
 FRR1 SZ IP       2349 25.57                             153    0.7210 3.59  56
 FRR1 SE ES       2349 27.08                             153   -0.3510 3.59  56
 YNKM SZ IPG  0   2349 26.44   364.6e+01                 116    0.0510 11.9   4
 YNKM SZ ESG  1   2349 28.25     1.2e+02                 116   -1.85 7 11.9   4

 2009 10 4 1009  6.8 L  40.758  27.692 12.1  FRO 10 0.5 1.3LFRO 2.6CFRO 1.5LMAM1
 GAP=141        1.10       4.1     2.5  3.0  0.2367E+01  0.6141E+00 -0.3938E+01E
 STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO AIN AR TRES W  DIS CAZ7
 FRR4 SZ IP       1009 11.85                             122    0.5610 11.7 100
 FRR4 SN ES       1009 13.99                             122   -0.5810 11.7 100
 MADM SZ IPG  0   1009 12.08   266.3e+01                 122    0.3110 11.8 192
 MADM SZ ESG  1   1009 14.83     8.4e+01                 122   -0.57 7 11.8 192
 FRR2 SZ IP       1009 12.65                             109    0.5810 18.0 295
 FRR2 SE ES       1009 15.26                             109   -0.6510 18.0 295
 FRR1 SZ IP       1009 14.15                              61    0.7710 22.9 264


 2009 10 5 0705 27.2 L  40.477  29.166  5.5  FRO  7 0.7 1.2LFRO 2.4CFRO 1.3LMAM1
 GAP=124        1.57       4.4     5.9  6.6  0.4955E+01  0.1054E+02 -0.1047E+02E
 STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO AIN AR TRES W  DIS CAZ7
 ESKM SZ IP   0   0705 33.54     2.2e+01                  40   -0.0810 23.6 308
 ESKM SZ ES   1   0705 36.80     3.0e+01                  40   -1.48 7 23.6 308
 IGDM SZ IP   0   0705 33.88   201.4e+01                  40    0.1210 23.8 173

The number I want to edit is the number after FBO which is the 11th column in the header string.
(Ex: 2009 10 5 0705 27.2 L 40.477 29.166 5.5 FRO 7 0.7 1.2LFRO 2.4CFRO 1.3LMAM1 )

All I need is to add 1.3 to that number. The line number between the blank lines vary.

I create two file one with "FRO number" and one with "FRO number+1.3" And tried to replace them with sed command.

sed 's/12.8  FRO/14.1  FRO/g' file1 > file2

but since the values repeated several time it went all the way wrong so that idea of mine became useless.

Any help would be appreciated...

Hi miriammiriam,

One way using awk:

$ cat infile                                                                                                                                                                                                                                 
2009 10 3 2349 21.3 L 40.719 27.388 10.8 FRO 7 0.8 1.1LFRO 2.6CFRO 1.1LMAM1                                                                                                                                                                  
GAP=157 1.69 5.7 5.9 5.8 0.5405E+01 0.4455E+00 0.1653E+02E                                                                                                                                                                                   
STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO AIN AR TRES W DIS CAZ7                                                                                                                                                               
FRR1 SZ IP 2349 25.57 153 0.7210 3.59 56                                                                                                                                                                                                     
FRR1 SE ES 2349 27.08 153 -0.3510 3.59 56                                                                                                                                                                                                    
YNKM SZ IPG 0 2349 26.44 364.6e+01 116 0.0510 11.9 4                                                                                                                                                                                         
YNKM SZ ESG 1 2349 28.25 1.2e+02 116 -1.85 7 11.9 4                                                                                                                                                                                          
                                                                                                                                                                                                                                             
2009 10 4 1009 6.8 L 40.758 27.692 12.1 FRO 10 0.5 1.3LFRO 2.6CFRO 1.5LMAM1                                                                                                                                                                  
GAP=141 1.10 4.1 2.5 3.0 0.2367E+01 0.6141E+00 -0.3938E+01E                                                                                                                                                                                  
STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO AIN AR TRES W DIS CAZ7                                                                                                                                                               
FRR4 SZ IP 1009 11.85 122 0.5610 11.7 100
FRR4 SN ES 1009 13.99 122 -0.5810 11.7 100
MADM SZ IPG 0 1009 12.08 266.3e+01 122 0.3110 11.8 192
MADM SZ ESG 1 1009 14.83 8.4e+01 122 -0.57 7 11.8 192
FRR2 SZ IP 1009 12.65 109 0.5810 18.0 295
FRR2 SE ES 1009 15.26 109 -0.6510 18.0 295
FRR1 SZ IP 1009 14.15 61 0.7710 22.9 264


2009 10 5 0705 27.2 L 40.477 29.166 5.5 FRO 7 0.7 1.2LFRO 2.4CFRO 1.3LMAM1
GAP=124 1.57 4.4 5.9 6.6 0.4955E+01 0.1054E+02 -0.1047E+02E
STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO AIN AR TRES W DIS CAZ7
ESKM SZ IP 0 0705 33.54 2.2e+01 40 -0.0810 23.6 308
ESKM SZ ES 1 0705 36.80 3.0e+01 40 -1.48 7 23.6 308
IGDM SZ IP 0 0705 33.88 201.4e+01 40 0.1210 23.8 173
$ awk '$10 == "FRO" { $11 += 1.3 } { print }' infile
2009 10 3 2349 21.3 L 40.719 27.388 10.8 FRO 8.3 0.8 1.1LFRO 2.6CFRO 1.1LMAM1
GAP=157 1.69 5.7 5.9 5.8 0.5405E+01 0.4455E+00 0.1653E+02E
STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO AIN AR TRES W DIS CAZ7
FRR1 SZ IP 2349 25.57 153 0.7210 3.59 56
FRR1 SE ES 2349 27.08 153 -0.3510 3.59 56
YNKM SZ IPG 0 2349 26.44 364.6e+01 116 0.0510 11.9 4
YNKM SZ ESG 1 2349 28.25 1.2e+02 116 -1.85 7 11.9 4

2009 10 4 1009 6.8 L 40.758 27.692 12.1 FRO 11.3 0.5 1.3LFRO 2.6CFRO 1.5LMAM1
GAP=141 1.10 4.1 2.5 3.0 0.2367E+01 0.6141E+00 -0.3938E+01E
STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO AIN AR TRES W DIS CAZ7
FRR4 SZ IP 1009 11.85 122 0.5610 11.7 100
FRR4 SN ES 1009 13.99 122 -0.5810 11.7 100
MADM SZ IPG 0 1009 12.08 266.3e+01 122 0.3110 11.8 192
MADM SZ ESG 1 1009 14.83 8.4e+01 122 -0.57 7 11.8 192
FRR2 SZ IP 1009 12.65 109 0.5810 18.0 295
FRR2 SE ES 1009 15.26 109 -0.6510 18.0 295
FRR1 SZ IP 1009 14.15 61 0.7710 22.9 264


2009 10 5 0705 27.2 L 40.477 29.166 5.5 FRO 8.3 0.7 1.2LFRO 2.4CFRO 1.3LMAM1
GAP=124 1.57 4.4 5.9 6.6 0.4955E+01 0.1054E+02 -0.1047E+02E
STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO AIN AR TRES W DIS CAZ7
ESKM SZ IP 0 0705 33.54 2.2e+01 40 -0.0810 23.6 308
ESKM SZ ES 1 0705 36.80 3.0e+01 40 -1.48 7 23.6 308
IGDM SZ IP 0 0705 33.88 201.4e+01 40 0.1210 23.8 173

Hello birei,

Thank you for your quick kindly answer, but here is another problem:

when I type the command you gave:

$ awk '$10 == "FRO" { $11 += 1.3 } { print }' infile

I got the lines shifted.

 2009 10 1 2048 30.3 L   40.378   28.578   6.7   FRO  12 0.6 1.3LFRO 2.2CFRO   
 2009 10 1 2048 30.3 L 40.378 28.578 6.7 FRO 13.3 0.6 1.3LFRO 2.2CFRO

It could not be displayed here but the spaces which are two, decrease to one space.

So, my file does not work since the format is changed :frowning: Do have any idea why this happens?

Thank you very much!!!!!

Best..

[edit] You can display it. That's what code tags are for.

Awk considers the spaces as separators, not fixed columns. If you need fixed columns, you must put them in fixed columns. Working on something.

I've tried preserving the formatting, but you are trying to put a 3-long string (8.3) in a column that only has room for two characters:

 2009 10 3 2349 21.3 L  40.719  27.388 10.8  FRO  7 0.8 1.1LFRO 2.6CFRO 1.1LMAM1
 2009 10 3 2349 21.3 L  40.719  27.388 10.8  FRO 8.3 0.8 1.1LFRO 2.6CFRO 1.1LMAM1

What you want isn't possible unless you want to enlarge that column.

It is possible to eliminate the spaces after FBO so that the column number does not change.

I will try to handle this problem with that awk statement.

Thank you for your kindly anwers...

Try:

awk '{sub(" FRO *" $11," FRO " $11+1.3)}1' infile

Hey it works perfectly well thank you very much!!!!:b:

Hello everybody again

Well I have made a mistake the number which will be changed is the $9 not $11 so I tried as below

awk '{sub($9 " FRO ",$9-1.3 " FRO ")}1' gupguzelhyp.out > uphyp.out

But it did not work :frowning:

Try:

awk '{sub($9 " *FRO ",$9-1.3 " FRO ")}1' infile
1 Like

Yess thank you very much!!! One little star fixed the whole file :b::o

Hello all,

I need to change a number in a file by adding some residuals respectively

To make it clear,

I need to add 0.11 to the number between 24-28 (which is below the SECON) for all the lines starting with FRR1
or I need to add 0.13 to the number between 24-28 (which is below the SECON) for all lines starting with FRR4

The values for the codes (FRR1, FRR4...) are constant.

since the columns are vary from line to line I try to set the characters between 24-28 so that the code would change only the second...

I tried this code but it is ofcourse useless

set SEC=`\cut -c 24-28 inp` 
awk '{sub(" FRR2 * " $SEC," FRR2 * " $SEC+0.11)}1' inp > out

and here is the example of my file

 2009 1024 0332 41.7 L  40.797  27.526 12.7  FRO 14 0.5 1.5LFRO 2.5CFRO 1.6LMAM1
 GAP= 86        0.97       2.6     2.4  4.1 -0.3933E+00 -0.3282E+01  0.2834E+01E
 STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO AIN AR TRES W  DIS CAZ7
 FRR2 SZ IP        332 44.09                             155   -0.4510 3.95 323
 FRR1 SZ IP        332 46.23                             123    0.6810 11.0 232
 FRR1 SE ES        332 48.86                             123    0.5310 11.0 232
 YNKM SZ IPG  0   0332 45.82   169.7e+01                 123    0.0110 11.4 286
 YNKM SZ ESG  1   0332 48.03     1.6e+02                 123   -0.74 7 11.4 286
 MADM SZ IPG  0   0332 47.05   124.0e+01                  99    0.0810 19.8 144
 MADM SZ ESG  1   0332 49.98     6.6e+01                  99   -0.80 7 19.8 144
 FRR4 SZ IP        332 48.23                              92    0.4210 26.3 104
 FRR4 SE ES        332 51.63                              92   -0.6110 26.3 104
 SART SZ IP   0   0332 48.78   426.2e+00                  91   -0.29 9 31.6 248
 SART SZ ES   2   0332 53.66     1.6e+01                  91   -0.75 5 31.6 248

 2009 1025 0114 10.4 L  40.824  27.977 11.7  FRO 12 0.5 1.4LFRO 2.6CFRO 1.6LMAM1
 GAP=103        1.23       3.6     3.0  5.2 -0.2501E+01 -0.3318E+01  0.5687E+01E
 STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO AIN AR TRES W  DIS CAZ7
 FRR4 SZ IP        114 15.03                             110    0.3010 15.6 233
 FRR4 SE ES        114 17.47                             110   -0.4010 15.6 233
 MARM SZ IPG  0   0114 15.18   183.9e+01                 110    0.1910 15.9 355
 MARM SZ ESG  1   0114 18.13     1.5e+02                 110   -0.18 7 15.9 355
 FRR2 SZ IP        114 18.57                              72   -0.11 9 40.4 270
 FRR2 SE ES        114 24.13                              72   -0.57 9 40.4 270
 FRR1 SZ IP        114 20.15                              72    0.04 9 47.7 258

 2009 1025 0304 42.0 L  40.814  27.754 11.7  FRO 14 0.6 1.6LFRO 2.7CFRO 1.8LMAM1
 GAP=126        1.36       3.7     2.8  4.8  0.3033E+00 -0.2901E+01 -0.5029E+01E
 STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU VELO AIN AR TRES W  DIS CAZ7
 FRR4 SZ IP       0304 46.22                             125    0.6910 10.3 142
 FRR4 SE ES       0304 48.07                             125   -0.0110 10.3 142
 MADM SZ IPG  0   0304 47.08   245.1e+01                 106   -0.0510 19.3 203
 MADM SZ ESG  1   0304 50.01     7.9e+01                 106   -0.85 7 19.3 203
 FRR2 SZ IP        304 47.29                             102    0.1110 21.6 274
 FRR2 SE ES        304 50.20                             102   -0.7410 21.6 274
 MARM SZ IP   0   0304 48.11   241.0e+02                 100    0.1310 24.4  46
 MARM SZ ES   3   0304 52.06     2.5e+02                 100   -0.26 2 24.4  46
 FRR1 SN IP        304 49.43                              98    0.7410 29.2 253
 FRR1 SE ES        304 53.13                              98   -0.4210 29.2 253