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


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

hi experts ,

I have an input like following.
Code:
R sfst     1000.0000                                                            
$ new time step for mass scaled calculation
R dt2ms    -4.000E-7                                                            
$ friction value for blank
R mue       0.120000                                                            
$ blankholder force
R bhf      2.0000E+5                                                            
$ simulation time
R endtime   0.150000

i want to change the value on the line containing 'mue'
with following I can read it but cant change it.

Code:
awk ' /mue/ { print $3 } ' input.txt

The value is to be taken from another file fric.txt.
fric.txt contains only numbers, one on each line .

Can anybody guide me doing this using sed or awk?
# 2  
Old 03-29-2012
What matches fric.txt values to the value(s) in your input? (i.e. how do you know which fric.txt line to use?)
# 3  
Old 03-29-2012
awk

Hi,

Try this one,

Code:
awk 'BEGIN{while(getline line <"fric.txt"){k++;val[k]=line;}}/mue/{i++;$3=val[i];print;}' file

Input:
file:

Code:
R sfst     1000.0000
$ new time step for mass scaled calculation
R dt2ms    -4.000E-7
$ friction value for blank
R mue       0.120000
$ blankholder force
R mue       0.130000
R bhf      2.0000E+5
R mue       0.140000
$ simulation time
R endtime   0.150000

fric.txt

Code:
1.120000
2.130000
6.140000

Output

Code:
R sfst     1000.0000
$ new time step for mass scaled calculation
R dt2ms    -4.000E-7
$ friction value for blank
R mue 1.120000
$ blankholder force
R mue 2.130000
R bhf      2.0000E+5
R mue 6.140000
$ simulation time
R endtime   0.150000

Hope, i convered your requirement.

Cheers,
RangaSmilie
# 4  
Old 03-29-2012
Quote:
Originally Posted by CarloM
What matches fric.txt values to the value(s) in your input? (i.e. how do you know which fric.txt line to use?)
it will take the first value for one time.
The I will delete that line from fric.txt so that next time it will take the next value and so on..
# 5  
Old 03-29-2012
You could just use rangarasan's solution.

Or modified so it doesn't read all of fric.txt every time:
Code:
awk 'BEGIN{getline line <"fric.txt"} /mue/{$3=line;print;}' file

# 6  
Old 03-29-2012
it doesnt work at all , it just output the line containing mue..

I also want to keep the format same and inplace editing.
# 7  
Old 03-29-2012
Code:
awk 'BEGIN{getline line <"fric.txt"} /mue/{printf ("%s %-8s %s\n", $1, $2, line);} $0 !~ /mue/ {print}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

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

3. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

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

5. Shell Programming and Scripting

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

I have an input like following. *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 ... (5 Replies)
Discussion started by: hamnsan
5 Replies

6. Shell Programming and Scripting

sed: how to replace string by another line

hi experts, i would like to do some html-file replacements and encounter the following problem: after cleaning up the file a bit i'd like to replace a regex with a reference and the content of another line. example: /<title>\(*\) - Wikipedia<\/title>/ should be replaced by \1 (the title... (2 Replies)
Discussion started by: chrisnie
2 Replies

7. Shell Programming and Scripting

How to use SED or AWK to search and replace an exact string

I have a file DS1 DDS DS I want to replace only "DS" to "DSmail.blah.com" in a lot of files. I tried sed 's/DS/DSmail.blah.com' but it changes all the lines . thanks in advance (2 Replies)
Discussion started by: gubbu
2 Replies

8. Shell Programming and Scripting

awk/sed string search and replace

Need help with either sed or awk to acheive the following file1 ----- In the amazon forest The bats eat all the time... mon tue wed they would eat berries In the tropical forest The bats eat all the time... on wed bats eat nuts In the rain forest The bats eat all the time... on... (2 Replies)
Discussion started by: jville
2 Replies

9. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

10. Shell Programming and Scripting

sed conditional string replace for each line

Hi all, I appreciate the enormous amount of knowledge that flows in this forum. I am an average UNIX user. I have many files with lines like the below. I have separated each line with space for ease of reading. I need to replace the first occurance of "/00" with null on those lines that have... (6 Replies)
Discussion started by: Nanu_Manju
6 Replies
Login or Register to Ask a Question