1

I have a string test.cgi@action=<action-name>

<action-name> can be any string

I would like to replace the above string with <action-name>.html

Can any one please let me know how I can proceed with this using a Perl script?

5
  • can you clarify what you would like to replace with .html ? the entire string or just .cgi ? Commented Jul 1, 2011 at 10:04
  • Given the data you've shown us, it's hard to see why: $string .= '.html'; isn't the answer :-) Commented Jul 1, 2011 at 12:28
  • Example...test.cgi@action=redirect...then the replaced string should be redirect.html Commented Jul 2, 2011 at 22:54
  • @Flimzy has the right answer then Commented Jul 3, 2011 at 13:38
  • How can i extend this to search and replace all the occurences of above string within a file?. Commented Jul 11, 2011 at 3:54

1 Answer 1

7
$string =~ s/test\.cgi@action=(.*?)/$1.html/g;
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your suggestion. I will try this out...if I extend this to all the files within a directory...how ca I implement this?...i.e replace all the files with names test.cgi@action=<action-name> as <action-name>.html??
So you're trying to rename a bunch of files? That's really not related to the question you asked originally, but you can do that with the rename command. For example: rename 's/^test\.cgi\@action=(.*)$/$1.html/' test.cgi*
Yes, I'm looking for both issues. Btw, rename command seems not to work. I have a file test.cgi@action=manager and i'm expecting to get renamed to manager.html, but it doesn't work. Can you please let me know how to resolve this?. or how i can debug this.
I tried with following code, but this doesn't replace the string : my $string = 'test.cgi@action=xxxxxxxxxxxxxxxx test.cgi@action=aaaaaayyyyyyyyyyytest.cgi@action='; $string =~ s/test\.cgi@action=(.*)/$1.html/; print $string;
What result are you expecting when your string contians multiples of these address-like substrings?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.