It happened again!!!

A 5 star hangout for overworked and underpaid system admins.
Post Reply
abednegoyulo
Posts: 550
Joined: 2007/12/26 06:24:38
Location: 127.0.0.2 44013

It happened again!!!

Post by abednegoyulo » 2010/04/21 01:20:41

But this time, I was able find what I am looking for before I clicked "post" :-D

I had a problem of replacing all instances of

url('/gif/btn.gif', true) ?>"

to

url('/jpg/btn.jpg', true) ?>"


I realized that this is a problem that sed may solve. I finished reading the manual on sed but it did not help that much. Then I tried to search using google using keywords like [i]sed, tutorial, how-to, examples[/i] and alike but the only things that I found is a bunch of very simple examples. After hours of searching, I decided to ask for some help here. After constructing the contents of my question, the thought cross my head that why did not I search for the keywords that I have indicated in the subject. I searched for [i]sed processing only lines containing pattern[/i] and bingo! The first hit contained the example that I was looking for. In the end my solution is

sed "/\/gif\//{s/url('\/gif\//http:\/\/static1.domain.com\//g;s/', true) ?>//g}" test-file

adding the command [i]find[/i]

find . -type f -exec sed -i "/\/gif\//{s/url('\/gif\//http:\/\/static1.domain.com\//g;s/', true) ?>//g}" '{}' \;

will modify all files in the current and sub directories containing the pattern.

I just want to share my experience :-D

User avatar
AlanBartlett
Forum Moderator
Posts: 9345
Joined: 2007/10/22 11:30:09
Location: ~/Earth/UK/England/Suffolk
Contact:

It happened again!!!

Post by AlanBartlett » 2010/04/21 15:35:28

Quite often, I find that setting out my problem in the format of a question will enable me to answer it for myself.

There's just one little mystery to your tale. You said --

[quote]
. . . search for the keywords that I have indicated in the subject.
[/quote]
However I suspect that you changed the subject line to [i]"It happened again!!!"[/i], as an afterthought, forgetting that you had made mention to it in the body of your post. :roll:

As Mr H J Simpson, of Evergreen Terrace, Springfield, USA, would utter: "[b]D'oh![/b]". :lol:

User avatar
jlehtone
Posts: 4532
Joined: 2007/12/11 08:17:33
Location: Finland

Re: It happened again!!!

Post by jlehtone » 2010/04/21 18:49:38

In case of sed the info is much more elaborate than the man ...

User avatar
jlehtone
Posts: 4532
Joined: 2007/12/11 08:17:33
Location: Finland

Re: It happened again!!!

Post by jlehtone » 2010/04/22 11:00:50

And, thinking how I would have had problems with such, consider input line:
[code]<img src="<?= $html->url('/gif/btn.gif', true) ?>"> <img src="<?= $html->url('/jpg/btn.jpg', true) ?>">[/code]
What will your command do?


But as alternative, lets take your:
[code]sed "/\/gif\//{ s/<?= \$html->url('\/gif\//http:\/\/static1.domain.com\//g; s/', true) ?>//g }" test-file[/code]
and write:
[code]sed "sX<?= \$html->url('/gif/\([^.]\+\.gif\)', true) ?>Xhttp://static1.domain.com/\1Xg" test-file[/code]

[i]The syntax of the `s' (as in substitute) command is `s/REGEXP/REPLACEMENT/FLAGS'.[/i]
There is REGEXP, so your "/\/gif\//" address match is not totally necessary.
You could have "s/\.gif', true) ?>/.gif/g", but why do two independent substitutions?

[i]The `/' characters may be uniformly replaced by any other single character within any given `s' command.[/i]
Hence the use of 'X' in my version.

[i]The REPLACEMENT can contain `\N' (N being a number from 1 to 9, inclusive) references, which refer to the portion of the match which is contained between the Nth `\(' and its matching `\)'.[/i]
Hence the use of "\([^.]\+\.gif\)". That catches the name of the file without being greedy.

But even this expression fill fail in particular case. Can you already see, when?
[Edit]: Make it two cases.

abednegoyulo
Posts: 550
Joined: 2007/12/26 06:24:38
Location: 127.0.0.2 44013

Re: It happened again!!!

Post by abednegoyulo » 2010/04/23 05:47:31

@ Alan

You are right :-D

@ jlehtone

Although the pattern

[code]
<img src="<?= $html->url('/gif/btn.gif', true) ?>"> <img src="<?= $html->url('/jpg/btn.jpg', true) ?>">
[/code]

does not appear, in my test it only modified the first instance which is what I am expecting for. Honestly speaking I was not aware what the output will be so I did the test :-D

The problems that I have later on discovered is on the

[code]
<img src="<?= $html->url('/gif
[/code]

vs

[code]
<img src="<?=$html->url('/gif
[/code]

and

[code]
.gif', true) ?>"
[/code]

vs

[code]
.gif', true)?>"
[/code]

I was a little bit surprised that such differences exists. I can't blame the developers because it only existed in 4 out of 54 files or 8 lines out of 498 lines :-o

What really caught my curiosity right now is your usage of X instead of /. In my understanding from your example, you used it to locate a regex instead of a string. Please correct me if my understanding is incorrect. Currently I am searching right now for its usage.

About my task, good thing we are running trac with svn so it was easy reviewing the changes that I have made and also found two lines that were modified that shouldn't be. This is one of them


[code]
<img src="<?= $html->url('/gif/touch/btn_' . $name . '.gif', true) ?>"
[/code]

turned to

[code]
<img src="http://static1.domain.com/touch/btn_' . $name . '.gif" alt="<?= $name; ?>"
[/code]

which is should have been

[code]
<img src="http://static1.domain.com/touch/btn_<?=$name?>.gif" alt="<?= $name; ?>"
[/code]

The lesson learned: Even though that you have coordinated with the developers very closely, be prepared for glitches when using sed on somebody else's code. :-D

User avatar
jlehtone
Posts: 4532
Joined: 2007/12/11 08:17:33
Location: Finland

Re: It happened again!!!

Post by jlehtone » 2010/04/23 09:15:31

[quote]abednegoyulo wrote:
Although the pattern
[code]
<img src="<?= $html->url('/gif/btn.gif', true) ?>"> <img src="<?= $html->url('/jpg/btn.jpg', true) ?>">
[/code]
does not appear, in my test it only modified the first instance which is what I am expecting for. Honestly speaking I was not aware what the output will be so I did the test :-D[/quote]
Really? Your
[code]s/', true) ?>//g[/code]
should remove every occurrence of "', true) ?>" from the gif-having line, including from those jpg tags.

[quote]abednegoyulo wrote:
The problems that I have later on discovered is on the[/quote]
That is easy.
[code]s/', true) ?>//g
s/', true) *?>//g[/code]
The second version does have a '*' after ' ', which means that there can be 0 or more ' ' between ')' and '?'

[quote]abednegoyulo wrote:
What really caught my curiosity right now is your usage of X instead of /.[/quote]
That is a convenience thing. If you do have '/' in the REGEXP or REPLACEMENT strings for "s", you have to quote them '\/', as you did. What I prefer to do, is to use any character, which is not used in those strings, in place of '/'. Thus, these mean the same:
[code]s/Hello/World/
sXHelloXWorldX
ssHellosWorlds
s Hello World [/code]


[quote]abednegoyulo wrote:
[code]
<img src="<?= $html->url('/gif/touch/btn_' . $name . '.gif', true) ?>"
[/code]
which is should have been
[code]
<img src="http://static1.domain.com/touch/btn_<?=$name?>.gif" alt="<?= $name; ?>"
[/code][/quote]
You could substitute the "btn_' . $name . '.gif" to "btn_.gif" (or something similar) [i]before[/i] the "main" substitute command.


You can easily generalize your conclusion by noting that you will encounter glitches with everybody's source data, including your own. Even the self-coordination fails. :-)

abednegoyulo
Posts: 550
Joined: 2007/12/26 06:24:38
Location: 127.0.0.2 44013

Re: It happened again!!!

Post by abednegoyulo » 2010/04/24 01:20:01

Having second look, it does confirm that it will replace all instances of "', true) ?>"

[code]
<img src="http://static1.domain.com/btn.gif"> <img src="<?= $html->url('/jpg/btn.jpg">
[/code]

I knew I should have worn my glasses. :lol:

Thanks for the tip about X! This will really come in handy.

[code]
Even the self-coordination fails.
[/code]

I totally agree!!! :-D

Post Reply