How to change a number on a specific line with cshell or shell?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to change a number on a specific line with cshell or shell?
# 1  
Old 03-21-2012
Data How to change a number on a specific line with cshell or shell?

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

Code:

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

Code:
 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


Could anyone help please...

Last edited by miriammiriam; 03-21-2012 at 12:03 PM..
# 2  
Old 03-21-2012
try this
egrep "FRR1" sample | tr -s " " " " |cut -d " " -f6 | awk '{if ($1>=24) {if($1<=28) print $1+0.11;};}'
# 3  
Old 03-21-2012
well the output for the command you placed

25.68
27.19
26.17
27.88
26.97
25.9
25.16
26.92
25.86
26.6
25.21

I need to replace the number under the SECON column.. which means that the output format must be the same as the in put file

Thank you
# 4  
Old 03-21-2012
Code:
$ cat addfr.awk
BEGIN { A["FRR1"]=0.11; A["FRR4"]=0.13 }

A[$1] {
        V=substr($0, 24, 5);
        V += A[$1]
        $0=substr($0, 0, 23) sprintf("%5s", V) substr($0, 29);
}

1

$ awk -f addfr.awk data

 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.34                             123    0.6810 11.0 232
 FRR1 SE ES        332 48.97                             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.36                              92    0.4210 26.3 104
 FRR4 SE ES        332 51.76                              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.16                             110    0.3010 15.6 233
 FRR4 SE ES        114  17.6                             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.26                              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.35                             125    0.6910 10.3 142
 FRR4 SE ES       0304  48.2                             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.54                              98    0.7410 29.2 253
 FRR1 SE ES        304 53.24                              98   -0.4210 29.2 253

$

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 03-22-2012
Thanks for the answer Smilie

But when I type the code it gives error:

Code:
sezim@localhost#BEGIN { A["FRR1"]=0.11; A["FRR4"]=0.13 }
BEGIN: No match.
A[FRR4]=0.13: No match.
sezim@localhost#
sezim@localhost#A[$1] {
A[]: Command not found.
sezim@localhost#        V=substr($0, 24, 5);
Badly placed ()'s.
sezim@localhost#        V += A[$1]
V: Command not found.
sezim@localhost#        $0=substr($0, 0, 23) sprintf("%5s", V) substr($0, 29);
Badly placed (.
sezim@localhost#}
}: Command not found.
sezim@localhost#
sezim@localhost#1
1: Command not found.
sezim@localhost#
sezim@localhost# awk -f addfr.awk data
awk: addfr.awk:20640: fatal: cannot open file `data' for reading (No such file or directory)

I did not understand why it did not worked I think there is something wrong with BEGIN or maybe the syntax

I tried a couple of different syntax but it did not change the result...
# 6  
Old 03-22-2012
note the highlighted words

Code:
 
$ cat addfr.awk
BEGIN { A["FRR1"]=0.11; A["FRR4"]=0.13 }

A[$1] {
        V=substr($0, 24, 5);
        V += A[$1]
        $0=substr($0, 0, 23) sprintf("%5s", V) substr($0, 29);
}

1

$ awk -f addfr.awk data

# 7  
Old 03-22-2012
$ cat addfr.awk ( with this line we see the content of the input file?)
and
$ awk -f addfr.awk data (this line writes the changes to data, the output file)

I am wrong? Smilie

---------- Post updated at 03:59 AM ---------- Previous update was at 03:27 AM ----------

Helloo

I added some syntax and it works now:

Code:
awk 'BEGIN { A["OBO3"]=0.13; A["OBO4"]=0.09 }

A[$1] {
        V=substr($0, 24, 5);
        V += A[$1]
        $0=substr($0, 0, 23) sprintf("%5s", V) substr($0, 29);
}

1' addfr.awk > data

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

2. Shell Programming and Scripting

Cut from specific line number to a line number

Hi All, I've a file like this.. Sheet1 a,1 a,2 a,3 a,4 a,5 Sheet2 a,6 a,7 a,8 a,9 a,10 Sheet3 a,11 a,12 a,13 (7 Replies)
Discussion started by: manab86
7 Replies

3. Shell Programming and Scripting

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... (11 Replies)
Discussion started by: miriammiriam
11 Replies

4. Shell Programming and Scripting

new line after specific number character

Hi All, I have input file like this: input1: ( 1083479)=T 158V 1798, T 391V 1896,T 1138V 2273,T 1547V 2477,T 2249V 2917,T 3278V 3234,T 4152V 3495,T 5500V 3631, ( 1083501)=T 181V 1851, T 459V 1954,T 810V 2141,T 1188V 2372,T 1638V 2696,T 2731V 3124,T 4799V 3640,... (5 Replies)
Discussion started by: attila
5 Replies

5. Shell Programming and Scripting

How to read from specific line number in shell script?

I have a text file which is having 30000 lines in it. I have to create a xml file for each 10000 lines until all the lines in the text files are written. Also please help how can i get number of lines in the text file in a shell variable? (19 Replies)
Discussion started by: vel4ever
19 Replies

6. Programming

Cshell help with change colour in print

i want to ask how to change the colour of prompt message from use? and also how to change colour in printing........ i want to change it as blue colour and red colour, but i found many website still don't know how to do. how's the command is wrote? thz really!! (1 Reply)
Discussion started by: wendy1089
1 Replies

7. Shell Programming and Scripting

How would i delete a line at specific line number

Hi guys , I m writing a script to delete a line at particular location. But i m unable to use variable for specifying line number. for example. sed -n '7!p' filename works fine and deletes 7th line from my file but sed -n '$variable!p' filename gives following error. sed: -e... (12 Replies)
Discussion started by: pinga123
12 Replies

8. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

9. UNIX for Dummies Questions & Answers

Change Specific Line of a File

Hi everyone, I am attempting to do something that should be very simple. How do I replace a specific line of a file with different text, and then save that file to its original name? I believe I want to use the sed command with the c option, but I after trying many times, I can't get the right... (10 Replies)
Discussion started by: msb65
10 Replies

10. Shell Programming and Scripting

Adding a columnfrom a specifit line number to a specific line number

Hi, I have a huge file & I want to add a specific text in column. But I want to add this text from a specific line number to a specific line number & another text in to another range of line numbers. To be more specific: lets say my file has 1000 lines & 4 Columns. I want to add text "Hello"... (2 Replies)
Discussion started by: Ezy
2 Replies
Login or Register to Ask a Question