how to change a number in a file with sed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to change a number in a file with sed?
# 1  
Old 07-12-2008
how to change a number in a file with sed?

Hello,

I have a file which contains some line as follows,

9.9 TEMP
9.9 MUCOEFF
0.0 EPSILON

And I want to increase this MUCOEFF by 0.2 as

9.9 TEMP
10.1 MUCOEFF
0.0 EPSILON

I browsed this forum for a bit and find some tips using sed, awk,..
But still do not know to change this number infront of MUCOEFF.
Note that space between 9.9 and MUCOEFF changes from file to file,
but this 9.9 is always comes first in line.

Any tips will be great help !
Specially complete method using sed will be really nice for me.

Last edited by lorenzz; 07-12-2008 at 02:53 AM..
# 2  
Old 07-12-2008
Try awk:
Code:
awk '/MUCOEFF/{$1+=.2}1' file

# 3  
Old 07-13-2008
Thanks danmero, it worked in command line.
But it do not work in makefile.
I stored this in makefile for frequent use, but $1 do not work.
Is there any solution?
# 4  
Old 07-13-2008
To understand your situation you have to post your script.
# 5  
Old 07-13-2008
Quote:
Originally Posted by lorenzz
Thanks danmero, it worked in command line.
But it do not work in makefile.
I stored this in makefile for frequent use, but $1 do not work.
Is there any solution?
Did you mean passing env vars that contain values to be incremented ?

Code:
awk -v var=0.2 '/MUCOEFF/ { $1+=var}1' b

# 6  
Old 07-13-2008
I made makefile as follows,

test:
awk '/MUCOEFF/{$1+=0.6}1' filename > filename

and when I type "make test", I get

awk: /MUCOEFF/{+=0.6}1
awk: ^ syntax error
make: *** [test] Error 1

It seems that "$1" do not recognized.

Last edited by lorenzz; 07-13-2008 at 09:42 AM..
# 7  
Old 07-13-2008
The simpliest way is to put your awk script into a seperate file and call this script using the awk -f option i.e.
Code:
file1:
        @echo "Calling awk -f awkfile ...."
        awk -f awkfile file > file1

where awkfile contains
Code:
/MUCOEFF/ {$1+=0.6}1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using sed to change file into an awk script

Hi I want to use sed to change a text files input into an awk script. For example if the input says " chord -- english " I want to change this using sed 's/pattern 1 /pattern 2 /'g filename but I don't understand how to use part of the pattern 1 to input that into pattern 2 . Like after... (10 Replies)
Discussion started by: enforcer
10 Replies

2. Shell Programming and Scripting

How to change comma delimeter in the file to number?

I have a file H,20180624200732,VPAS,TRANS_HDR,20180724, VPAS.TRANS_HDR.20180724.01.txt, ,93, T,1, I have to change and instead first comma put ",1" like below H,20180624200732,VPAS,TRANS_HDR,20180724, VPAS.TRANS_HDR.20180724.01.txt,1,93, T,1, I made sed "2s/, /,1/"... (8 Replies)
Discussion started by: digioleg54
8 Replies

3. UNIX for Beginners Questions & Answers

File name change with sed

I have a bunch of text files like this: Sample_S1_L001_R1.txt Sample_S10_L001_R1.txt Sample_S11_L001_R1.txt I am using the following script to add a 0 to those files with a single digit after the S: ls *.txt | sed 's/\(.*_S\)\(_.*\)/mv & \10\2/' | sh And then the following script to... (4 Replies)
Discussion started by: Xterra
4 Replies

4. UNIX for Dummies Questions & Answers

How to take every number in in file by sed?

Hi all.., I have problem when i want to take every number in my file. this is my file look like.. errorpath.csv error=29 error=34 error=2 error=3 error=6 error=5 error=36 error=36 error=36 error=36 error=36 error=36 (9 Replies)
Discussion started by: weslyarfan
9 Replies

5. Shell Programming and Scripting

Trying to take file numbers from a file, pass them to sed to change strings in corresponding lines

I have a bunch of file numbers in the file 'test': I'm trying the above command to change all the instances of "H" to "Na+" in the file testsds.pdb at the line numbers indicated in the file 'test'. I've tried the following and various similar alternatives but nothing is working: cat test |... (3 Replies)
Discussion started by: crunchgargoyle
3 Replies

6. 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

7. UNIX for Dummies Questions & Answers

sed question - change 2d to last line in file

The file is repeating blocks of text separated by a blank line. Some of the lines are unique. e.g.: This is unique line This is line 2 This is line 3 This is line 4 This is unique line This is line 2 This is line 3 This is line 4 ... The goal is to change/replace line 4... (2 Replies)
Discussion started by: uiop44
2 Replies

8. Shell Programming and Scripting

Change the File name using pattern and number

Hi, i have the below files in the wrk folder. File names are AAB0001.R BBA0002.R CCDC0003.R AAB0002.R AAB0034.R BBA0081.R DDDE0008.R i need to change the file name USING pattern and number. output like below :wall: AAB0001.R AAB0002.R AAB0003.R BBA0001.R BBA0002.R CCDC0001.R... (3 Replies)
Discussion started by: krbala1985
3 Replies

9. Shell Programming and Scripting

sed to change a configuration file

Hi I want to create a configuration file from a template one. The only thing to change for now is the port number that I need to take from another file that has this format: server:port So I need to take the 2nd field. server is passed in the script argument. how to do that? I... (1 Reply)
Discussion started by: melanie_pfefer
1 Replies

10. Shell Programming and Scripting

How to change this file with SED?

I have a file like below: I want to delete the rows which begining with "BC" "CD" and "TY" . then change every fields into one row like this at last delete the head words : USing awk to change it is not hard. I want to know how to do this work using SED ? Thank you! ... (4 Replies)
Discussion started by: bejgirl
4 Replies
Login or Register to Ask a Question