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.
# 8  
Old 09-23-2010
Quote:
Originally Posted by dragon.1431
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:
b=_EDITED

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

The sed code is straightforward...
Code:
sed "s!$i!$i$b!"

replaces the variable $i with $i$b.
# 9  
Old 09-24-2010
hi pravin ,franklin &rdcwayx,

i noticed one error when i tried the solution for the following input:
read.txt contains:
Code:
home/test/root1
home/test/root1_try
home/test/root1_ATB

Actual output:
Code:
make save "home/test/root1_EDITED"version="1.2"
test repo "home/test/root1_EDITED_try"version="1.2.1"
make prod "home/test/root1_EDITED_ATB"version="1.2.2"

Expected output:
Code:
make save "home/test/root1_EDITED"version="1.2"
test repo "home/test/root1_try_EDITED"version="1.2.1"
make prod "home/test/root1_ATB_EDITED"version="1.2.2"

with rdcwayx solution,actual ouput i got as follows:
Code:
make save "home/test/root1_EDITED"version="1.2"
test repo "home/test/root1_EDITED_try_EDITED"version="1.2.1"
make prod "home/test/root1_EDITED_ATB_EDITED"version="1.2.2"

i understood pravin27 logic. i didn't get the logic of using negation!Is it not something similar to grep -v?

---------- Post updated at 07:03 AM ---------- Previous update was at 06:11 AM ----------

i think i fixed it as follows:
Code:
sed  '/'$c\"'/s//'$c$b\"'/' temp_forum.txt > temp1_forum.txt


Last edited by dragon.1431; 09-24-2010 at 09:04 AM.. Reason: added the new solution
# 10  
Old 09-24-2010
MySQL

Code:
# ./justdoit file
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"

Code:
# cat justdoit
#!/bin/bash
sed "" read.txt | while read -r l;
 do
newl=$(echo "$l"| sed 's@/@\\/@g')
sed "s/\(.*\)\($newl\)\(.*\)/\1\2_EDITED\3/" $1 > filetmp
mv filetmp $1
 done
more $1


Last edited by ygemici; 09-24-2010 at 09:34 AM..
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