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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to change a number on a specific lines in a file with shell?
# 1  
Old 03-09-2012
Data 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,

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

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

Last edited by Corona688; 03-09-2012 at 11:46 AM.. Reason: Code tags for code please.
# 2  
Old 03-09-2012
Hi miriammiriam,

One way using awk:
Code:
$ 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

# 3  
Old 03-09-2012
Hello birei,

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

when I type the command you gave:

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

I got the lines shifted.

Code:
 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 Smilie Do have any idea why this happens?

Thank you very much!!!!!

Best..

Last edited by Corona688; 03-09-2012 at 11:35 AM..
# 4  
Old 03-09-2012
[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.
# 5  
Old 03-09-2012
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:

Code:
 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.
# 6  
Old 03-09-2012
Quote:
Originally Posted by Corona688
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:

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

Last edited by miriammiriam; 03-09-2012 at 05:50 PM..
# 7  
Old 03-10-2012
Try:
Code:
awk '{sub(" FRO *" $11," FRO " $11+1.3)}1' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Quick UNIX command to display specific lines in the middle of a file from/to specific word

This could be a really dummy question. I have a log text file. What unix command to extract line from specific string to another specific string. Is it something similar to?: more +/"string" file_name Thanks (4 Replies)
Discussion started by: aku
4 Replies

2. Shell Programming and Scripting

To change Specific Lines in An XML file

hi Guys, this is my requirement, there is a huge xml file of this i have to change 3 lines with out opening the file /users/oracle > cat lnxdb-pts-454.xml|egrep "s_virtual|s_cluster|s_dlsnstatus" <cluster_port oa_var="s_clusterServicePort">9998</cluster_port> <host... (2 Replies)
Discussion started by: smarlaku
2 Replies

3. Shell Programming and Scripting

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

4. Shell Programming and Scripting

Change default shell of a specific user with awk

I would like to replicate the functionality of chsh (or passwd -e) by awk. This is what I got so far, but I think there should be an easier way to search and replace field $7 only for lines beginning with user_name: awk -v user_name="$user_name" -v new_shell="$new_shell" -F: '$1 == user_name {... (2 Replies)
Discussion started by: nomad84
2 Replies

5. UNIX for Dummies Questions & Answers

How to use sed to copy specific lines from a file using shell variables?

hello! I am trying to use sed to copy specific set of lines from a file for which the starting and ending line numbers of the lines to be copied are stored in shell variables. How can i copy those lines? if the input_file is something like this and if the following is the script a=2 b=4... (4 Replies)
Discussion started by: a_ba
4 Replies

6. Shell Programming and Scripting

KSH SHELL: problem calculation number of lines inside compressed file

Hi Gurus, I am working with a korn shell script to simplify some operations of calculation number of lines inside compressed file. The called function (inside a cycle) is the following: ######################################### # F.ne: CheckCount #########################################... (3 Replies)
Discussion started by: GERMANICO
3 Replies

7. Shell Programming and Scripting

Delete all lines that start with a bigger number of a specific one.

E.g. the file is like this: I want to delete all lines that begin with a number larger than 2, ignoring the lines that doesn't begin with a number! PS:'2' is actually a variable that can have a lot of values:b:i bet you got it (10 Replies)
Discussion started by: hakermania
10 Replies

8. SCO

Why? I can not change the number of lines to print

hi My problem now is that if shipping options as -o length = 88 it says the following: # lp -o length=88 -dhp4015 /etc/hosts UX:lp: ERROR: The following options can't be handled: -o length= TO FIX: The printer(s) that otherwise qualify for printing your request can't handle one or more of... (2 Replies)
Discussion started by: Edgar Guevara
2 Replies

9. Shell Programming and Scripting

extract the lines between specific line number from a text file

Hi I want to extract certain text between two line numbers like 23234234324 and 54446655567567 How do I do this with a simple sed or awk command? Thank you. ---------- Post updated at 06:16 PM ---------- Previous update was at 05:55 PM ---------- found it: sed -n '#1,#2p'... (1 Reply)
Discussion started by: return_user
1 Replies

10. Shell Programming and Scripting

Shell Script to read specific lines in a file

I have a file with contents as follows Record 1: Rejected - Error on table "DWO"."P2G_CUST_EVENTS". ORA-00001: unique constraint (DWO.CUST_EVENTS_PK) violated Record 5: Rejected - Error on table "DWO"."P2G_CUST_EVENTS". ORA-00001: unique constraint (DWO.CUST_EVENTS_PK) violated Record 6:... (5 Replies)
Discussion started by: varshanswamy
5 Replies
Login or Register to Ask a Question