Newbie: replacing strings containing special caracters


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Newbie: replacing strings containing special caracters
# 1  
Old 03-31-2006
Newbie: replacing strings containing special caracters

Hello,

I am a newbie here and I am just learning using Unix.
I have a dozen scripts that start like that:

#!/bin/ksh
if test $# -eq 0
then
list=short
else
list=$1
fi
DIR=.
DIRTOOLS=../tools

What I want to do is change modify 2 lines in each of these scripts without having to edit the files one by one.

I need to change:

DIR=.
DIRTOOLS=../tools

into:

DIR=../marc2006_qa
DIRTOOLS=/d2/marcdev/tools

There are other occurences of the string "DIR=." and "DIRTOOLS=" elsewhere in each script but only the first ones need to be changed.

Can someone tell me what command I need to type to perform this action?

Thank you so much for your help!
# 2  
Old 04-03-2006
Your Fix, I think

perl -e 'while(<>){s/DIR=\.\/DIR=\.\.\/marc2006_qa/;print $_;}' /yourfile > NEWFILE

You can call this in a shell script or run it right on the commandline. I didn't test it but this should work.

The command reads like this:

Run PERL from the command line: "perl -e"
Read each line one at a time: "while (<>)"
Substitute where you find DIR=. and replace it with DIR=../marc2006_qa
, (using g for all or NO g for the first occurance) to replace. print each line: "{s/:/ /; print $_;}'
The file your using as input: "/yourfile"
The change is not made to the input file, it must be printed out to a new file: " > NEWFILE "
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Any tip to replacing the special characters in a file

Hi, Please find attached a file that has special characters on it. It is a copy and paste from a Micro$oft file. I don't want to use strings as it remove all the 'indentations' / 'formatting' so I am replacing them with space instead. I am using the sed command below sed "s/$(printf... (1 Reply)
Discussion started by: newbie_01
1 Replies

2. UNIX for Dummies Questions & Answers

Replacing special character with sed

Hi All, I have a text file that contains I1SP2 *=*=Y=M=D001D My requirement is to replace all occurrence of =* to =Z expected o/p is I1SP2 *=Z=Y=M=D001D I have tried with sed 's/=*/=Z/g' file sed 's!\=*!\=Z/g' file sed 's!\=*!\=Z!g' file sed 's!\=\*!\=Z!g' file but its not... (3 Replies)
Discussion started by: gotamp
3 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

Need help in replacing special characters

I am writing a ksh script. I need to replace a set of characters in an xml file. FROM="ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÛÚÜÝßàáâãäåçèéêëìíîïðñòóôõö¿¶ø®"; TO="AAAAAAACEEEEIIIIDNOOOOOOUUUUYSaaaaaaceeeeiiiionooooo N R" I have used the code- sed 's/$FROM/$TO/g'<abc.xml But its not working. Can anyone tell me the code to do this? (3 Replies)
Discussion started by: saga20
3 Replies

5. Shell Programming and Scripting

Replacing string with special characters in shell

Hi, I am trying to replace a string in shell but it is not working correctly. @xcom.file@ needs to be replaced with tb137 Plz help.Thx. Please use and tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks. (4 Replies)
Discussion started by: manish72
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

help on sed replacing special characters

Hello, I have a file with many lines with below format: \abc\\1234 jkl\\567 def\\345 \pqr\\567 \xyz\\234 Here, i need to do 2 things. 1. replace \\ with \ 2. remove starting \ so output to be as below: (11 Replies)
Discussion started by: prvnrk
11 Replies

8. Shell Programming and Scripting

Replacing French special characters

Hi, I have tonnes of .txt files that are written in French. I need to replace the French special characters, however, with English equivalents (e.g. é -> e and ç -> c). I have tried this --- #!/bin/bash # Convert French characters to normal characters # Treat each of the files exec... (4 Replies)
Discussion started by: BlueberryPickle
4 Replies

9. Shell Programming and Scripting

Silly newbie question on special characters!

Hello again Gurus, Can someone please direct me to an online source that specifically explains what characters like mean within if statements? or scripts in general, I have found information about the different letter options you can specify for an if statment, but I get really confused with the... (3 Replies)
Discussion started by: charliemp3
3 Replies

10. Shell Programming and Scripting

replacing string with special character ???

the problem is while replacing the old string with new one with the help of SED i am unable to replace the special characters with new strings. how can i do that? i dont want the user to be given the trouble to write '\' before every special characters like * , . , \ , $ , &. sed... (4 Replies)
Discussion started by: imppayel
4 Replies
Login or Register to Ask a Question