0

I have the following task: I have to replace several links, but only the links which ends with .do

Important: the files have also other links within, but they should stay untouched.

<li><a href="MeinKonto.do">Einstellungen verwalten</a></li>

to

<li><a href="<s:url action='MeinKonto'/>">Einstellungen verwalten</a></li>

So I have to search for links with .do, take the part before and remember it for example as $a , replace the whole link with

<s:url action=' '/>

and past $a between the quotes.

I thought about sed, but sed as I know does only search a whole string and replace it complete. I also tried bash Parameter Expansions in combination with sed but got severel problems with the quotes and the variables.

cat  ./src/main/webapp/include/stoBox2.jsp | grep -e '<a href=".*\.do">' | while read a;
do
    b=${a#*href=\"};
    c=${b%.do*};
    sed -i 's/href=\"$a.do\"/href=\"<s:url action=\'$a\'/>\"/g' ./src/main/webapp/include/stoBox2.jsp;
done;

any ideas ?

Thanks a lot.

1
  • Are your links always surrounded by double quotes? Commented Jun 10, 2014 at 23:33

2 Answers 2

1
sed -i  sed 's#href="\(.*\)\.do"#href="<s:url action='"'\1'"'/>"#g' ./src/main/webapp/include/stoBox2.jsp

Use patterns with parentheses to get the link without .do, and here single and double quotes separate the sed command with 3 parts (but in fact join with one command) to escape the quotes in your text.

 's#href="\(.*\)\.do"#href="<s:url action='

 "'\1'"

 '/>"#g'

parameters -i is used for modify your file derectly. If you don't want to do this just remove it. and save results to a tmp file with > tmp.

Sign up to request clarification or add additional context in comments.

5 Comments

If you have several links on a same line, and if one of it, other than the first one, is a .do to modify, your regex will delete links (sed is greedy, and your parenthesis catch as much as possible). No offense, but what's wrong with my solution, that made you post nearly the same?
@Qeole Have you check your command? It just missed some double quotes or single quotes. And I think here you have \1 for href=" is a little redundant.
The href=" thing does not not look that redundant too me, but OK. For the quotes, yes I checked my command, and for me with OP's example, it provides desired output. Could you please provide more details? Did you tried your command with input as described in my first comment?
@Qeole yes, you're right in the first comment, I should add a [^\"] to avoid it. And for your command I just met error as Unmatched ".
I didn't meet this error. If you still have the exact command you typed, or if you (or anyone else) ever reproduces it, I'm interested.
1

Try this one:

sed -i "s%\(href=\"\)\([^\"]\+\)\.do%\1<s:url action='\2'/>%g" \
    ./src/main/webapp/include/stoBox2.jsp;

You can capture patterns with parenthesis (\(,\)) and use it in the replacement pattern.
Here I catch a string without any " but preceding .do (\([^\"]\+\)\.do), and insert it without the .do suffix (\2).

There is a / in the second pattern, so I used %s to delimit expressions instead of traditional /.

Comments

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.