Search and replace a line in perl


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Search and replace a line in perl
# 1  
Old 04-11-2013
Search and replace a line in perl

Hi All,
i can replace a perticular value in sentence using perl.
perl -pi -e 's/old/new/' sample.txt
but i am not able to replace whole string by perl.

file1 contains "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=10.147.109.211)(PORT=1526))(CONNECT_DAT A=(SID= MWDBD22)))". i want to change this value with following target value.

target value:"jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(FAILOVER=yes)(LOAD_BALANCE=yes)(ADDRESS=(PROTOC OL=TCP)(HOST=10.144.223.236)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=10.144.223.237)(PORT=1521)))(CO NNECT_DATA=(SERVICE_NAME=RICPCF.shareprod.org)))"

i used the following one. bt it didn't change the value

perl -pi -e "s/jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=10.147.109.211)(PORT=1526))(CONNECT_DATA =(SID= MWDBD22)))/jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(FAILOVER=yes)(LOAD_BALANCE=yes)(ADDRESS=(PROTOCOL=TCP) (HOST=10.144.223.236)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=10.144.223.237)(PORT=1521)))(CONNECT_D ATA=(SERVICE_NAME=RICPCF.shareprod.org)))/" file1
# 2  
Old 04-11-2013
I am afraid you need to \ escape each special character:
In the search pattern, ( and ) and . are special in perl RE, and need to be escaped \( and \) and \.
In both the search pattern and the substitution string the @ needs to be \@ escaped because it is always special in Perl.
This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 04-12-2013
Thank u.
now i can replace by using \ for each special character.
# 4  
Old 04-17-2013
Hi,
Can i place those escape characters by script automatically? i mean to say that when it gets one special character,place '/' before this in automatic way.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

2. Shell Programming and Scripting

Search and replace (perl)

How can I achieve this? Perl would be awesome. Input string a_b c //Note there is a blank here Needed Output a_b_c Thanks (4 Replies)
Discussion started by: dragonpoint
4 Replies

3. Shell Programming and Scripting

Perl - search and replace a particular field

Hi, I have a file having around 30 records. Each record has 5 fields delimited by PIPE. Few records in the file having Junk characters in the field2 and field4. I found the junk charcter and I tested it and replace the junk with space with the command below perl -i -p -e "s/\x00/ /g"... (1 Reply)
Discussion started by: ramkrix
1 Replies

4. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

5. Shell Programming and Scripting

Search and Replace in Perl

I am trying to write a simple perl script to run on a FreeBSD machine. There are alot of posts here, and I have read so many, yet can not get this script to run. #!/usr/bin/perl -e 's/\r\n/~/' infile.txt outfile.txt I am trying to take a windows text file, move it into Unix, run a script on... (1 Reply)
Discussion started by: mach1
1 Replies

6. UNIX for Advanced & Expert Users

Search Parameter in first line and replace next line content

Hi, I need help. I have XML file as below <a n="infoLevel"> <v s="true"/> </a> <a n="localAddr"> <v s="server.host.com"/> </a> <a n="ListenPort"> <v s="21111"/> </a> I need to find variable "ListenPort" in line and then replace... (4 Replies)
Discussion started by: rdtrivedi
4 Replies

7. Shell Programming and Scripting

Perl Search and replace entire line

I have a perl function in my script that needs to replace an entire line in a file sub changestate { my $base = (); my @base = (); open(BASE, $file) || die("Could not open file!"); @base=<BASE>; close (BASE); foreach $base(@base) { if($base =~... (1 Reply)
Discussion started by: insania
1 Replies

8. Shell Programming and Scripting

Perl: Global Search and replace

I have a file where the rows correspond to individuals and the columns are about 106 variables. Each variable is coded as either ACGT, and "missing" is coded as blank. This is a tab delimited file. I'm trying to replace all blanks (" ") with 0. The simple script I have is only replacing some of the... (3 Replies)
Discussion started by: epi8
3 Replies

9. Shell Programming and Scripting

Search and replace in Perl

Hello, I have a Perl script that reads in an Excel spread sheet and formats the values into a text file. I am having trouble with one column that can have numbers or letters. Excel left justifies the values that start with a letter and right justifies the values that contain only a... (2 Replies)
Discussion started by: jyoung
2 Replies

10. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies
Login or Register to Ask a Question