CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
search and replace

 



cool_cake20
Novice

Jul 30, 2014, 3:44 AM

Post #1 of 6 (752 views)
search and replace Can't Post

Hi ,

I am new to perl.

I need to search for the code line where it ends with ")" and ";"

Code
case1 : $display("i am new to perl ") ; 
case2 : $display("i am new to perl ") ;
case3 : $display("i am new to perl ") ;

replace them with

case1 : $display("i am new to perl ")
case2 : $display("i am new to perl ")
case3 : $display("i am new to perl ")

that means i need search for the combination of ")" and ";" ignoring the white space between them.

how can i do that.



BillKSmith
Veteran

Jul 30, 2014, 4:51 AM

Post #2 of 6 (749 views)
Re: [cool_cake20] search and replace [In reply to] Can't Post

This is a job for a regular expression. Read the perl documentation perlre, perlretut, perlrequick.

You can use a one-liner:


Code
perl -pe"s/)\s*;\s*$/);/" infile >outfile


Note: Replace double quotes with single quotes on almost any system but windows.
Good Luck,
Bill


cool_cake20
Novice

Jul 30, 2014, 7:50 AM

Post #3 of 6 (741 views)
Re: [BillKSmith] search and replace [In reply to] Can't Post

Thanks BillKSmith, it worked.
Sure, i will go thorough the documents that you have mentioned.


Laurent_R
Veteran / Moderator

Jul 30, 2014, 9:22 AM

Post #4 of 6 (733 views)
Re: [BillKSmith] search and replace [In reply to] Can't Post

Hi B ill,

the OP apparently wants to remove the semi-colon and keep the closing parens. So your Perl on e-liner should be modified as follows:


Code
perl -pe"s/)\s*;\/)/" infile >outfile



BillKSmith
Veteran

Jul 30, 2014, 9:35 AM

Post #5 of 6 (731 views)
Re: [Laurent_R] search and replace [In reply to] Can't Post

Neither of seem to have noticed that the text and example in the original post disagree with each other. We each solved one of the problems.
Good Luck,
Bill


cool_cake20
Novice

Jul 30, 2014, 9:22 PM

Post #6 of 6 (714 views)
Re: [BillKSmith] search and replace [In reply to] Can't Post

Hi Laurent_R

I initially tried the code that Bill mentioned ,but getting some errors.
I did little modification to it.
This is my working code.


Code
     $line =~ s/\)\s*;/\)/;


Thanks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives