2

In my file contains the following text

http://mydomain.com/test.phtml
http://mydomain.com/classes/main.class.phtml
http://mydomain.com/scripts/filemanager/nl.phtml

i need this (i think i need use sed?)

http://mydomain.com/mydirectory/test.php
http://mydomain.com/classes/mydirectory/main.class.php
http://mydomain.com/scripts/filemanager/mydirectory/nl.php

2 Answers 2

6
sed 's/\([^/]*\)\.phtml$/mydirectory\/\1.php/' <filename>

Will do that, if that’s what you need. (optionally with the -i flag to do an in-place replace.)


To break it up, first we have

s/<regexp>/<replacement>/

Which will replace what <regexp> matches with <replacement>. Next let’s look at the regexp we had:

\([^/]*\)\.phtml$

First, at the end we have \.phtml$ which will look for the string .phtml at the end of the line. $ anchors the regexp to the end of the line, and there’s a backslash in front of the dot to escape it, because ordinarily a dot matches anything.

After that what we have left is:

\([^/]*\)

Looking in the middle we have [^/] which will match one character ([] will match one of the characters inside the square brackets), which can be anything that isn’t a slash, because ^ will do a negated match, so ^/ inside square brackets “anything other than a slash”. After the closing square bracket there is an asterisks, *, which means that it will match one or more of the characters inside the square brackets.

Then wrapped around the above we have \( and \) which will capture what is matched inside the parenthesis and let you use them in the <replacement> section of s/<regexp>/<replacement>/ so we can use them.

And then in the <replacement> section we have:

mydirectory\/\1.php/

Which will replace the matched rexecp with first mydirectory/, the slash needing to be replaced since / is used as the separator for the sed replace, and then \1 is used to put in the text that was captured in the first capture group, and then we add the .php extension at the end.


All of this taken together means that we will capture everything from the last / to .phtml, add mydirectory/ after the last slash, write back the text we captured, and then add the .php extension.

0
moi@laptop:/home/moi/tmp >cat in.txt
http://mydomain.com/test.phtml
http://mydomain.com/classes/main.class.phtml
http://mydomain.com/scripts/filemanager/nl.phtml
moi@laptop:/home/moi/tmp >sed -i 's/\/\([^\/]\+\)\.phtml/\/mydirectory\/\1.php/g' in.txt 
moi@laptop:/home/moi/tmp >cat in.txt
http://mydomain.com/mydirectory/test.php
http://mydomain.com/classes/mydirectory/main.class.php
http://mydomain.com/scripts/filemanager/mydirectory/nl.php
moi@laptop:/home/moi/tmp >
1
  • 3
    Your answer contains 10 lines. 9 of them are of no interest to the reader. Maybe it is better to remove them and add some explanation that helps to understand the regular expression. Your prompt is not chosen very well for a code sample: the blank between the path and the trailing '>' makes me believe that the '>' is part of the command. Also the information moi@laptop:/home/moi/tmp is not really valuable.
    – miracle173
    Dec 1, 2014 at 1:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .