Replacing keyword using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing keyword using sed
# 1  
Old 07-09-2013
Replacing keyword using sed

Hi All,

I am workingon below script, Where requirement is first copy the file with differnt name and than remove the specific keyword in the file after reading from the input file.

So I have to copy of file equal to the number of lines present in the input file and replace the specfic keyword.

Code:
 
#!/bin/bash
 
#Loop through the file
HSFILE=/home/Temp/Components.txt
for line in $HSFILE ;do
tmp=`ls *.cpp | awk -F"." '{ print $1 }'`
cp $tmp.cpp $tmp$(date +%F-%T).cpp
sed -i 's/URL/{print $1}/g' $tmp.cpp $tmp$(date +%F-%T).cpp
done

I am facing issue with the sed command as it is note taking the variable replacement in the file. It is replacing it as string in the file. I want to replace the value strored in $1 variable in the file.
# 2  
Old 07-09-2013
try using double quotes in sed sed -i "<<code>>"
# 3  
Old 07-09-2013
I have tried it but same issue..It is not replacing it.

Code:
 
sed -i "s/URL/$1/g" Monitor_Template2013-07-09-00:54:34.cpp

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

Hi,

I have strored the value in variabl;e and using double quotes to replace it in SED. But still it is not replacing it with value. It is just deleted the value in the file.

Code:
 
Url= awk -F":" '{print $1}' $HSFILE
echo $Url
sed -i "s/Url/$Url/g" <File>

# 4  
Old 07-09-2013
It should work just fine. Could you run the following and paste the results here?

echo $Url ; echo "test: Url" | sed "s/Url/$Url/"
# 5  
Old 07-09-2013
Above code snippet printing
Code:
 
 
test:

# 6  
Old 07-09-2013
Quote:
Originally Posted by sharsour
Above code snippet printing
Code:
 
 
test:

It seems like the $Url variable is empty.

This won't work:
Code:
Url= awk -F":" '{print $1}' $HSFILE

Try this instead:
Code:
Url=$(awk -F":" '{print $1}' $HSFILE)

This User Gave Thanks to Subbeh For This Post:
# 7  
Old 07-09-2013
Quote:
Originally Posted by sharsour
Hi All, . . .
Code:
 
#!/bin/bash
 
#Loop through the file
HSFILE=/home/Temp/Components.txt
for line in $HSFILE ;do
tmp=`ls *.cpp | awk -F"." '{ print $1 }'`
cp $tmp.cpp $tmp$(date +%F-%T).cpp
sed -i 's/URL/{print $1}/g' $tmp.cpp $tmp$(date +%F-%T).cpp
done

I am facing issue with the sed command as it is note taking the variable replacement in the file. It is replacing it as string in the file. I want to replace the value strored in $1 variable in the file.
1) You are not looping through any file. HSFILE will assume the string given, and that's it. Your for-loop-variable line also will assume exactly that value, plus it is not used anywhere.
2) Your tmp variable will assume many filenames given there's more than one .cpp file in your directory, which will make your cp cmd fail.
3) Why do you copy the file and then use the "in place" option of sed? Run the sed cmd on the original file, capturing it's output in the respective dated target file
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Not replacing using sed

Hi all, I am trying to change the below word. but the changes is not reflecting in the new file sed -n 's/apple/orange/' filename ---------- Post updated at 12:51 AM ---------- Previous update was at 12:41 AM ---------- I tried this it works perl -pi.bak -e... (9 Replies)
Discussion started by: ramkumar15
9 Replies

2. Shell Programming and Scripting

Search for a Keyword in file and replace another keyword or add at the end of line

Hi I want to implement something like this: if( keyword1 exists) then check if(keyword2 exists in the same line) then replace keyword 2 with New_Keyword else Add New_Keyword at the end of line end if eg: Check for Keyword JUNGLE and add/replace... (7 Replies)
Discussion started by: dashing201
7 Replies

3. Shell Programming and Scripting

sed not replacing

Data not replacing using sed,please check below. Replace_value=$$dbconn_target Search_value=$$dbcon_source   sed -e s/\${Search_value}/\${Replace_value}/g intrepid_sps_val.parm (2 Replies)
Discussion started by: katakamvivek
2 Replies

4. Shell Programming and Scripting

Replacing using sed

hi Guys, I have a rar file which consists of 10 files. each file has a space in its file name. how can i replace all spaces with _ i can replace them using sed but the thing is i need to replace using a script and not command. can anyone help me out??:confused: (2 Replies)
Discussion started by: rajeshb6
2 Replies

5. Shell Programming and Scripting

replacing by sed

hi my input file has got >,,,, or >, or >,,,,,, there are independent number of commas after >.... i want the o/p as > only that is just to remove "," after">" another is: i want to replace the last line of the file and to replace it by "hello"...how to do?... any nice script plz help (2 Replies)
Discussion started by: Indra2011
2 Replies

6. Shell Programming and Scripting

sed inside sed for replacing string

My need is : Want to change docBase="/something/something/something" to docBase="/only/this/path/for/all/files" I have some (about 250 files)xml files. In FileOne it contains <Context path="/PPP" displayName="PPP" docBase="/home/me/documents" reloadable="true" crossContext="true">... (1 Reply)
Discussion started by: linuxadmin
1 Replies

7. Shell Programming and Scripting

How can i use sed to replace a keyword in an xml file?

Hello Unix gurus! I'm a unix newbie. Can I use sed to replace a keyword in an xml file and convert this keyword with an output of a unix cat command? for example: <test>keyword</test> and temp.txt = hello! I would like to replace "keyword" with the output of "cat temp.txt". I think... (6 Replies)
Discussion started by: 4dirk1
6 Replies

8. Shell Programming and Scripting

Removing Multiple lines below a keyword using SED?

I have 9,000 + html files. I am using the following to remove the content from a certain line up for i in `ls` do sed '1,569d' $i > $i.bak done This will remove the unwanted formatting keeping the content I need which changes in each HTML file. the problem I have now is that the... (2 Replies)
Discussion started by: deaconf19
2 Replies

9. Shell Programming and Scripting

Using sed to delete a line having a particular keyword

Hi Geeks :b:, I need to delete a line from file that contains a particular keyword. I had read in some forum of unix.com that below code could be used sed "/$titlesearch/d" movielist >tmp mv tmp movielist But my file contains lines which contain slashes (/) FOr eg: /etc/movie/title/... (5 Replies)
Discussion started by: ajincoep
5 Replies

10. Shell Programming and Scripting

replacing using sed

its again sed question. i have line - sed "s/$old/$new/g" "$f" > $TFILE && mv $TFILE "$f" working well if old="myoldfile" new="mynewfile" but if i want old="/home/shailesh/1test/" new="/home/shailesh/workspace/" it gives error like sed: -e expression #1, char 9: unknown option to... (2 Replies)
Discussion started by: shailesh_arya
2 Replies
Login or Register to Ask a Question