sed doubt - search and substitute string from variable.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed doubt - search and substitute string from variable.
# 1  
Old 09-22-2010
MySQL sed doubt - search and substitute string from variable.

hi,
trying to learn more abt sed Smilie

i want to substitute a variable(a) with other variable(b) appended.

Read.txt contains:
Code:
home/test2/abc
home/test/root1

input.txt contains:
Code:
make test "home/test1/none"version="1.3"
wt's wrong test "home/test2/abc"version="1.0"
make save "home/test/root1"version="1.2"
make try "home/test3/user1"version="1.4"

code tried:
Code:
a=`cat read.txt`
for i in $a
do
b=_EDITED
sed -e "/$a/s///$a/$b/" input.txt
done

expected output:
Code:
make test "home/test1/none"version="1.3"
wt's wrong test "home/test2/abc_EDITED"version="1.0"
make save "home/test/root1_EDITED"version="1.2"
make try "home/test3/user1"version="1.4"

Thanks in advance Smilie

Last edited by dragon.1431; 09-22-2010 at 06:05 PM.. Reason: removed extra code tags
# 2  
Old 09-22-2010
Code:
#!/usr/bin/env ruby 
a=File.read("read.txt").split("\n")
File.open("input.txt").each do |x|
  a.each{|i| x.gsub!(/#{i}/,"#{i}_EDITED") }
  puts x
end

# 3  
Old 09-23-2010
hi kurumi,
Thanks for ur solution.
I would like to go with sed only, i noticed typo error .sed address pattern shd be $i in my previous post .
still i didnt get the expected output.
# 4  
Old 09-23-2010
try this,

Code:
#!/bin/sh

cp input.txt temp_forum.txt
a=`cat read.txt`

for i in $a
do
if [ -f temp1_forum.txt ]
then
cp temp1_forum.txt temp_forum.txt
fi
b=_EDITED
c=$(echo $i | sed 's/\//\\\//g')
sed  '/'$c'/s//'$c$b'/' temp_forum.txt > temp1_forum.txt
done
cat temp1_forum.txt
rm temp_forum.txt temp1_forum.txt

This User Gave Thanks to pravin27 For This Post:
# 5  
Old 09-23-2010
Or:
Code:
b=_EDITED

while read i
do
  sed "s!$i!$i$b!" input.txt > temp
  mv temp input.txt
done < read.txt

If your sed version supports the -i option:
Code:
b=_EDITED

while read i
do
  sed -i "s!$i!$i$b!" input.txt
done < read.txt

This User Gave Thanks to Franklin52 For This Post:
# 6  
Old 09-23-2010
Code:
awk 'NR==FNR{a[$1];next} {for (i in a) {if ($0~i) sub(i,i "_EDITED")}}1' Read.txt input.txt

This User Gave Thanks to rdcwayx For This Post:
# 7  
Old 09-23-2010
MySQL

hi,
thanks to everyone.
i have tested pravin & franklin solution.It worked . i hope other two solutions also works.

I will be more happy if you could explain how it works ?
Code:
sed -e "/$a/s///$a/$b/"

Am i not saying address & search pattern are same and substitute where $a is found with $a$b.

Thanks a lot in advance for explaining your sed code logic Smilie

Last edited by dragon.1431; 09-23-2010 at 06:54 AM.. Reason: added information about code test
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for string in column using variable: awk

I'm interested to match column pattern through awk using an external variable for data: -9 1:751343:T:A -9 0 T A 0.726 -5.408837e-03 9.576603e-03 7.967536e-01 5.722312e-01 -9 1:751756:T:C -9 0 T C 0.727 -5.360458e-03 9.579447e-03 7.966977e-01 5.757858e-01... (7 Replies)
Discussion started by: genome
7 Replies

2. Shell Programming and Scripting

[sed]: Substitute a string with a multiline value

Dear all, I try to replace a string of characters in a file (MyFile.txt) by a multiline value of the variable "Myvar": $ cat MyFile.txt DESCRIPTION '@TargetTable SCHEMA' ( @InputFlowDef ); $ The content of Myvar: $ echo "$Myvar" col1 , col2 , col3 $ (4 Replies)
Discussion started by: dae
4 Replies

3. Shell Programming and Scripting

Search for 2 string in 2 lines with sed ?

Hi ! I want to search a string in all lines with sed. If that string is there, i want to look for an other string in the next line.If that string is there i want to put an other line under it. eg: aaa bbb ccc ddd cat bla.txt | sed -e '/aaa/a\' -e ' \!!!' in the upper case, i would... (6 Replies)
Discussion started by: fugitivus
6 Replies

4. Shell Programming and Scripting

String search between patterns using sed

Hi, I am trying to find a way to get sed/awk/grep to help me find a string in a log file that exists between two datestamps and then print the preceding datestamp up to the next datestamp. Here is an example of my logfile: +++ 2013/03/28 17:01:37.085 SIGNALING HIGH ACTIVE Failure Response... (5 Replies)
Discussion started by: raytx
5 Replies

5. Shell Programming and Scripting

How to substitute variable in sed for special character?

Hi , I have input file like below Hi this is "vinoth". Hi happy to work with 'unix' USA(united states of America) My script variables are below : Dquote=Ộ Squote=&#$567 Obrac=&^986 Cbrac=&^745 I want to read the variables in my SED command to replace the double quote,single... (9 Replies)
Discussion started by: vinothsekark
9 Replies

6. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

7. Shell Programming and Scripting

Trying to use a variable pulled from a for statement in a search string

Guys I am having trouble trying to pull some data from a file using a value returned from a for statement. Any help would greatly be apprecaited. I can't seem to get my script to pass the variable. I am trying to pull the 2 lines below the line that has the "pool name" so that i can get the... (5 Replies)
Discussion started by: scottzx7rr
5 Replies

8. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

9. Shell Programming and Scripting

search a string in a line and save it in a variable

Hi I want to read a file line by line and search for a particular string in each line(say for example string containing @ )and save that string into a variable. Can someone suggest me the way to implement it.I am using K- shell Thanks Ishita (5 Replies)
Discussion started by: Ishita
5 Replies

10. Shell Programming and Scripting

Search for string in a file and extract another string to a variable

Hi, guys. I have one question: I need to search for a string in a file, and then extract another string from the file and assign it to a variable. For example: the contents of the file (group) is below: ... ftp:x:23: mail:x:34 ... testing:x:2001 sales:x:2002 development:x:2003 ...... (6 Replies)
Discussion started by: daikeyang
6 Replies
Login or Register to Ask a Question