sed problem replacing long strings


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sed problem replacing long strings
# 1  
Old 11-27-2008
sed problem replacing long strings

Hi all,

I have a script which uses sed to replace one string with another. The problem is, the string to be matched, and its replacement are coming in as two command line arguments $1 and $2

$1 and $2 can be absolutely anything, but both should be treated purely as strings. My sed command below doesn't do any escaping which is what I need.

sed "s/$1/$2/g" /tmp/input.txt > /tmp/output.txt

I spent a couple of days making some code which could escape these variables before use in my sed command and it seemed to allow me to enter almost anything but today, when trying to replace one string, with another string that contains an ampersand, some fancy sed backtracking came into play.

function escape {
new=`echo "$1" | sed 's/\\\\/\\\\\\\\/g' | sed 's/\//\\\\\//g' | sed 's/\*/\\\\\*/g' | sed 's/"/\"/g' | sed 's/\"/\\\\\"/g'`
echo $new
}

What I think I really need is a premade escape function for use in sed, or maybe some arguments to sed itself to ignore all special chars and treat them as strings.
# 2  
Old 11-28-2008
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Replacing a string in a long list of files

I have a script that needs to read a file with a long list of /path/filenames - replace the name of the server in each file - and write the file to the same path with a date extension. This is the script that I have so far #!/bin/ksh umask 022 LIST=`scripts.list` for i in $LIST do ... (2 Replies)
Discussion started by: bjdamon
2 Replies

2. Shell Programming and Scripting

Replacing strings

The code below gives the string "test1.txt" even though "tessdsdt" does not match "test1.txt". I would like to return "" if there is no match and return some kind of error that I can capture and decide what to do. echo test1.txt | awk -v src="tessdsdt" -v dst="test" '{sub(src,dst); print}' (16 Replies)
Discussion started by: kristinu
16 Replies

3. UNIX for Dummies Questions & Answers

best method of replacing multiple strings in multiple files - sed or awk? most simple preferred :)

Hi guys, say I have a few files in a directory (58 text files or somthing) each one contains mulitple strings that I wish to replace with other strings so in these 58 files I'm looking for say the following strings: JAM (replace with BUTTER) BREAD (replace with CRACKER) SCOOP (replace... (19 Replies)
Discussion started by: rich@ardz
19 Replies

4. Shell Programming and Scripting

Replacing or removing a long list of pattern by using awk or sed

Input: >abc|123456|def|EXIT| >abc|203456|def|EXIT2| >abc|234056|def|EXIT3| >abc|340056|def|EXIT4| >abc|456000|def|EXIT5| . . . Output: def|EXIT| def|EXIT2| def|EXIT3| def|EXIT4| def|EXIT5| . . My try code: (9 Replies)
Discussion started by: patrick87
9 Replies

5. Web Development

Apache log with long strings of Xs in GET request

Hi everybody, I was looking at my apache2 log and I found GET requests as such: Some ip - - "GET... (4 Replies)
Discussion started by: z1dane
4 Replies

6. Shell Programming and Scripting

Replacing strings

I am trying to take the two line version of this: mv myFile.txt myFile.txt.bak sed 's/foo/bar/g' myFile.txt.bak > myFile.txt and make it into a shell script with three parameters. First two parameters are the string and string replacement and the third is file. So far this is what I have... (5 Replies)
Discussion started by: gordonheimer
5 Replies

7. Shell Programming and Scripting

problem in replacing asterisk in sed

Hi all, Sed is the one which always trobules me :( here is my input : *** it industry need to be evolved *** in the world and hope so *** to be dream the output i am expecting is : *** it industry need to be evolved *** in the world and hope so *** to be dream ... (4 Replies)
Discussion started by: panyam
4 Replies

8. Shell Programming and Scripting

problem with sed while replacing a string with another

Hi, I have a line something like this sys,systematic,system I want to replace only the word system with HI I used sed for this as below echo sys,systematic,system | sed 's/system/HI/' but I got output as sys,HIatic,system I wanted output as sys,systematic,HI Please tell me... (9 Replies)
Discussion started by: friendyboy
9 Replies

9. Shell Programming and Scripting

replacing strings with newlines : sed

Hi everyone, Since the previous time I received help from unix.com I have been encouraged to learn more. going through 1 of the articles(View Article) on sed I found, it pointed an interesting situation. Suppose the text is : Romeo and Ethel the Dancer Moves Audience to Tears. I... (3 Replies)
Discussion started by: hkansal
3 Replies

10. Shell Programming and Scripting

SED: Replacing $1 with $2 escape problem

Hi all, I have a script which uses sed to replace one string with another. The problem is, the string to be matched, and its replacement are coming in as two command line arguments $1 and $2 $1 and $2 can be absolutely anything, but both should be treated purely as strings. My sed command... (7 Replies)
Discussion started by: mark007
7 Replies
Login or Register to Ask a Question