Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-12-2012
Registered User
 
Join Date: Feb 2012
Posts: 13
Thanks: 5
Thanked 0 Times in 0 Posts
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.



Code:
2684717    -194.7050476      64.2345581     150.6500092       0       0
2684718    -213.1575623      62.7032242     150.6500092       0       0
*INCLUDE
$# filename
./meshes/exportneu/147.k
*END

mesh.k is as follows


Code:
100

or any other 3 digit number

I want to replace the 147.k of this input.k to another number from
another file mesh.k which is 100 in this case..



Required output is as follows



Code:
2684717    -194.7050476      64.2345581     150.6500092       0       0
2684718    -213.1575623      62.7032242     150.6500092       0       0
*INCLUDE
$# filename
../meshes/exportneu/100.k
*END

I used


Code:
sed '/\<meshes\>/!d;=;s/.* ([^ ]\+).*/\1/;R mesh.k' input.k | 
sed 'N;N;s|\n|s/|;s|\n|/|;s|$|/|;q'  >temp.sed       
sed -i -f temp.sed input.k

The point is that I want to replace this
Quote:
147.k to 100.k where 100
is written in another file mesh.k , like in the other file only 100 is present or it could be 3 digit anyother number.
i know it can work with searching the line with word meshes for example and the dividing with last / and piping the data from other file but am not able to formulate the sed or awk.
I hope now you people understand what I mean. regards

Last edited by hamnsan; 06-12-2012 at 07:44 AM..
Sponsored Links
    #2  
Old 06-12-2012
clx clx is offline Forum Advisor  
Registered User
 
Join Date: Jun 2007
Location: Mumbai,India
Posts: 1,417
Thanks: 101
Thanked 133 Times in 129 Posts
Please post the required data from "mesh.k" and "input.k".
Also, please edit your post and use code tags. (hurry up, our mods here, are too fast )
The Following 2 Users Say Thank You to clx For This Useful Post:
hamnsan (06-12-2012), Scrutinizer (06-12-2012)
Sponsored Links
    #3  
Old 06-12-2012
Registered User
 
Join Date: Feb 2012
Posts: 13
Thanks: 5
Thanked 0 Times in 0 Posts
hi experts,

I have updated the question.. any suggestions or comments.

Last edited by hamnsan; 06-12-2012 at 10:42 AM..
    #4  
Old 06-13-2012
elixir_sinari's Avatar
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,372
Thanks: 87
Thanked 478 Times in 458 Posts
Assuming that mesh.k contains only 1 number:


Code:
awk 'NR==FNR{num=$1;next} /[0-9]{1,}\.k$/ {sub("[0-9]{1,}\.k$",num".k")} {print}' mesh.k input.k

The Following User Says Thank You to elixir_sinari For This Useful Post:
hamnsan (06-13-2012)
Sponsored Links
Closed Thread

Tags
awk, linux, replace, sed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replace line in file with line in another file based on matching string luckycharm Shell Programming and Scripting 5 01-09-2012 11:16 PM
replace (sed?) a single line/string in file with multiple lines (string) from another file?? tuathan Shell Programming and Scripting 5 04-20-2011 12:16 AM
To trim Certain field in a line of a file and replace the new string in that position rpadhi Shell Programming and Scripting 4 08-15-2009 11:03 AM
Replace string in a file within a range of line tosattam Shell Programming and Scripting 3 08-15-2008 01:12 AM
how can search a String in one text file and replace the whole line in another file kkraja UNIX for Dummies Questions & Answers 6 08-06-2008 07:23 AM



All times are GMT -4. The time now is 03:25 PM.