Single/Multiple Line with Special characters - Find & Replace in Unix Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Single/Multiple Line with Special characters - Find & Replace in Unix Script
# 1  
Old 06-02-2010
Java Single/Multiple Line with Special characters - Find & Replace in Unix Script

Hi,

I am creating a script to do a find and replace single/multiple lines in a file with any number of lines.

I have written a logic in a script that reads a reference file say "findrep" and populates two variables $FIND and $REPLACE

print $FIND gives
Hi How r $u
Rahul()

Note: $FIND can contain one or multiple lines along with special characters

print $REPLACE gives
#Hi How r $u
#Rahul()
i am $fine
thanks()

Note: $REPLACE can contain one or multiple lines along with special characters

Now, I want to replace $FIND with $REPLACE in "introduction.sh". Can anyone help me in achieving this? i have gone through lot of threads and forums but no success.

I have tried below command but it did not work
Code:
sed "s/'$FIND'/'$REPLACE'/g" introduction.sh > introduction.sh.a
mv introduction.sh.a introduction.sh

Also tried below command
Code:
perl -pi -e "s:$FIND:$REPLACE:g" introduction.sh

Also, tried awk
Code:
awk '{gsub(a,b);print}' a="$FIND" b="$REPLACE" introduction.sh > introduction.sh.a
mv introduction.sh.a introduction.sh

But this gave an error as below
awk: syntax error near line 1
awk: illegal statement near line 1

Last edited by r_sarnayak; 06-02-2010 at 05:56 AM.. Reason: adding code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find multiple strings and replace single string

Hi, following Perl code i used for finding multiple strings and replace with single string. code: #!/usr/bin/perl my @files = <*.txt>; foreach $fileName (@files) { print "$fileName\n"; my $searchStr = ',rdata\)' | ',,rdata\)' | ', ,rdata\)'; my $replaceStr =... (2 Replies)
Discussion started by: chettyravi
2 Replies

2. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

3. Shell Programming and Scripting

Split a single record to multiple records & add folder name to each line

Hi Gurus, I need to cut single record in the file(asdf) to multile records based on the number of bytes..(44 characters). So every record will have 44 characters. All the records should be in the same file..to each of these lines I need to add the folder(<date>) name. I have a dir. in which... (20 Replies)
Discussion started by: ram2581
20 Replies

4. Shell Programming and Scripting

Replace new line with <br /> & escape special characters

Hi, I wish to replace a new line with br (html) but it doesn't seem to work message=$(echo ${FORM_message} | tr '\r' '<br \/>' ) what it gives me seems to be ... b...? I am also having problem escaping hash sign in cut command: list=$(echo "$line" | cut -d'\#;\#' -f1) ; my intention is... (2 Replies)
Discussion started by: ted_chou12
2 Replies

5. Shell Programming and Scripting

Script to multiple find and replace in a single file

Dear all I need a script for multiple find and replace in a single file. For example input file is - qwe wer ert rty tyu asd sdf dgf dfg fgh qwe wer det rtyyui jhkj ert asd asd dfgd now qwe should be replace with aaaaaa asd should be replace with bbbbbbbb rty should be replace... (6 Replies)
Discussion started by: wildhorse
6 Replies

6. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

7. Shell Programming and Scripting

Replace special characters in multiple files - perl

I have 100 files, where i want to search a set of strings and make the replacement by other strings In the first case I want to include a parameter in the name of a file LOG_DCT = $ LOG_DIR/DCT_GERAL_"$DATAINI".log replace to : LOG_DCT = $ LOG_DIR / DCT_GERAL_ $ 1_ "$ DATAINI". log I did... (1 Reply)
Discussion started by: RMSoares
1 Replies

8. UNIX for Dummies Questions & Answers

Find and replace special characters in a file

HI All I need a shell script ehich removes all special characters from file and converts the file to UTF-* format Specail characters to be removed must be configurable. strIllegal = @"?/><,:;""'{|\\+=-)(*&^%$#@!~`"; Please help me in getting this script as my scripting skilla are... (2 Replies)
Discussion started by: sujithchandra
2 Replies

9. UNIX for Dummies Questions & Answers

Help with find and replace w/string containing special characters

Can I get some help on this please, I have looked at the many post with similar questions and have tried the solutions and they are not working for my scenario which is: I have a text file (myfile) that contains b_log=$g_log/FILENAME.log echo "Begin processing file FILENAME " >> $b_log ... (4 Replies)
Discussion started by: CAGIRL
4 Replies

10. UNIX for Advanced & Expert Users

Special characters getting replaced by &Pound in Unix Environment

Hi, Please find the Question Summary below- In our email template document(.txt) bullets and Apostrophe are getting replaced by the string "&pound" in our Live environment.We are using sun solaris 8 in live. Can anybody let me know why this happens and how to prevent this . Thanks... (0 Replies)
Discussion started by: kaushik05
0 Replies
Login or Register to Ask a Question