sed or awk to replace a value in a certain line.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed or awk to replace a value in a certain line.
# 1  
Old 02-29-2012
sed or awk to replace a value in a certain line.

I have an input like following.
Code:
  *DEFINE_CURVE_TITLE 
Force for tool binder 
$#    lcid       sidr       sfa        sfo              offa                   offo      dattyp 
          3              0     1 .000000   125.00000       0.000     0.000         0 
$#                a1                  o1
 0.000               0.000 
       0.5000000          -1.0000000 
     100.0000000          -1.0000000

I want to modify the 3rd line after *DEFINE_CURVE_TITLE like
3 0 1.000000 107.00000 0.000 0.000 0

so that it becomes
Code:
  *DEFINE_CURVE_TITLE 
Force for tool binder 
$#    lcid       sidr       sfa        sfo              offa                   offo      dattyp 
          3              0     1 .000000   107.00000       0.000     0.000         0 
$#                a1                  o1
 0.000               0.000 
       0.5000000          -1.0000000 
     100.0000000          -1.0000000

The question is that I can search for the required line as follows
Code:
  sed -i -e '/^\**DEFINE_CURVE_TITLE/{n;n;n;R $4 newBHF.txt' -e 'd}' input.txt

This way I am on the line to be edit i.e. three lines below the search string.
Now I have to change the values of $4 from 125.00000 to someother value that could be taken from newBHF.txt. That newBHF.txt has the required replacement i.e only one value 107.00000 . If possible please also tell me how can I set $4 , if I have the value in another variable in the same program.
This will help to do alot of things myself. Smilie

best regards


Moderator's Comments:
Mod Comment Please use next time
code tags for your code and data

Last edited by vbe; 02-29-2012 at 12:57 PM..
# 2  
Old 02-29-2012
Code:
awk -v p="$(cat newBHF.txt)" '/^\*DEFINE_CURVE_TITLE/{x=NR+3}NR==x{$4=p}1' input

Or if you need to update $5 instead :

Code:
awk -v p="$(cat newBHF.txt)" '/^\*DEFINE_CURVE_TITLE/{x=NR+3}NR==x{$5=p}1' input

---------- Post updated at 06:02 PM ---------- Previous update was at 05:57 PM ----------

In your first example : 1 .000000 contains a space : is it a typo error or is it the format of your file that need to be fixed ?
# 3  
Old 03-01-2012
I am sorry that was a typo , i.e it should be 1.00000 .

so in that case , I need to replace field 4.

thanks.

---------- Post updated at 03:03 AM ---------- Previous update was at 02:58 AM ----------

worked perfect. thanks man.

---------- Post updated at 03:13 AM ---------- Previous update was at 03:03 AM ----------

the problem is that , it has changed the format, I mean the software only recognize the changed file when it is in the same format, so please help me also to keep the format, like number of spaces on left and also in between .

original
Code:
         3         0  1.000000 125.00000     0.000     0.000         0

changed
Quote:
3 0 1.000000 107.00000 0.000 0.000 0
regards.

Last edited by hamnsan; 03-01-2012 at 04:27 AM.. Reason: to show the change in format upon replacement of required field.
# 4  
Old 03-01-2012
Isn't your file <tab> separated instead ?

if so

Code:
awk -v p="$(cat newBHF.txt)" '/^\*DEFINE_CURVE_TITLE/{x=NR+3}NR==x{$4=p}1' OFS="\t" input

# 5  
Old 03-02-2012
hi ,

its not tab seperated , infact , the same spacing I have shown in 'original'.

this I can do this with printf but I wanted to know if it is possible using a sed or awk instead.
# 6  
Old 03-02-2012
Code:
sed '/*DEFINE_CURVE_TITLE/{n;n;n;s/[^ \t][^ \t]*/107.00000/4;}' infile

Code:
sed "/*DEFINE_CURVE_TITLE/{n;n;n;s/[^ \t][^ \t]*/$(cat newBHF.txt)/4;}" infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace values in script reading line by line using sed

Hi all, Let's say I have a script calling for the two variables PA_VALUE and PB_VALUE. for pa in PA_VALUE blah blah do for pb in PB_VALUE blah blah do I have a text file with two columns of values for PA and PB. 14.5 16.7 7.8 9.5 5.6 3.6 etc etc I would like to read this... (7 Replies)
Discussion started by: crimsonengineer
7 Replies

2. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

3. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

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

5. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

6. Shell Programming and Scripting

sed or awk to replace a value in a certain line from another file containing a string

Hi experts, In my text file I have the following alot of lines like below. input.k is as follows. 2684717 -194.7050476 64.2345581 150.6500092 0 0 2684718 -213.1575623 62.7032242 150.6500092 0 0 *INCLUDE $# filename... (3 Replies)
Discussion started by: hamnsan
3 Replies

7. Shell Programming and Scripting

sed or awk to replace a value in a certain line containing a string

hi experts , I have an input like following. R sfst 1000.0000 $ new time step for mass scaled calculation R dt2ms -4.000E-7 $ friction value for blank R mue ... (10 Replies)
Discussion started by: hamnsan
10 Replies

8. Shell Programming and Scripting

Replace line and field using SED and/or AWK or some other suggestion

QUESTION 1: How do you replace a specific line (i.e. line 4) with a new user defined line (i.e. the contents of SAMS’s name, history, math and English grades have been set already). I have been attempting to use SED (FYI: I don’t have GNU SED) or AWK, but haven’t had any luck. FYI: I am using... (1 Reply)
Discussion started by: thibodc
1 Replies

9. Shell Programming and Scripting

Replace specific field on specific line sed or awk

I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case. After asking for new input and doing some... (14 Replies)
Discussion started by: crownedzero
14 Replies

10. Shell Programming and Scripting

Replace part of a line with sed/awk

Hello I have a document and in this document I have several occurrence of "VAR == xxxxxxx" and xxxxx can be anything. I don't know what it is. I want to replace the 'xxxxx's with something I know. What I know however, is the line numbers of the VAR =='s in the file. How can I replace... (1 Reply)
Discussion started by: alirezan
1 Replies
Login or Register to Ask a Question