Using sed or perl to replace multiple lines with leading spaces?

Commissario
Joined
16 Oct 2002
Posts
342,208
Location
In the radio shack
Hi folks.

I'm not a programmer but I can manage to do many things simply by looking into the command formats and working it out but this is causing me an issue. I'm trying to replace multiple lines in a .js file which have leading spaces but can't manage it with either sed or perl

This is what the source looks like
hjTSLFx.png

And this is what I want it to be
5vX3Mto.png

I need to change the 000000 to 0000DD, add a new line with lineDash below it and change the width to 2.

I can't just search for 000000 and change it to 0000DD because there are more than one instances of that within the file and the same goes for the width. There is only one instance of var ringStyle so that's the point I have to start with.

I've spent the last couple of hours trying padding the perl command with spaces and using \n for the line separators just to try and get this done but I'm failing badly. I'm sure there's a way to do this but I'm hotspured if I can work it out. I've got the command in a format which doesn't error at me any more but it doesn't actually do anything! Here's what I'm trying (this is a long command with the correct number of spaces). This is just to try and replace the 000000 with 0000DD and then I was going to tack the extra bits on the end.

Code:
sudo perl -i -0 -pe 's/\Q                var ringStyle = new ol.style.Style({\n                        fill: null,\n                        stroke: new ol.style.Stroke({\n                                color: '#000000',\n\E/\Q                var ringStyle = new ol.style.Style({\n                        fill: null,\n                        stroke: new ol.style.Stroke({\n                                color: '#0000DD',\n\E/g' script.js

Can anyone help please? If so, please can you explain the format of the command so I can understand what I'm doing.

Thanks.
 
Commissario
OP
Joined
16 Oct 2002
Posts
342,208
Location
In the radio shack
perl -0777 -i.original -pe "s/'#000000',\n\s*width: 1/'#0000DD',\nlineDash:[4,4]\nwidth: 2/igs" derpy.js
Thanks. This does work, sort of. The trouble is that there are multiple occurrences of the color and width entry within the file and I only want to change the one in the var ringstyle section.

With the example you've given, I thought it would be simple enough to build in the lines above to the command but it's not proving as straightforward as I though. Even just adding the whitespace to the color line isn't working.

One step at a time. First I added the whitespace and the additional text at the start of the color line.

perl -0777 -i.original -pe "s/\s*color: '#000000',\n\s*width: 1/\ncolor: '#0000DD',\nlineDash:[4,4],\nwidth: 2/igs" derpy.js

Works fine.

Then when I get to the next line, I'm introducing characters that are included in regex so I knew I was going to get problems.

perl -0777 -i.original -pe "s/\sstroke: new ol.style.Stroke({\s*color: '#000000',\n\s*width: 1/\nstroke: new ol.style.Stroke({\ncolor: '#0000DD',\nlineDash:[4,4],\nwidth: 2/igs" derpy.js

doesn't work because of the ({ characters.
Unmatched ( in regex; marked by <-- HERE in m/\sstroke: new ol.style.Stroke( <-- HERE {\s*color: '#000000',\n\s*width: 1/ at -e line 1.

I think I need to use \Q and \E as start and end terminators to enclose the ({ but this is where I'm struggling now.

I've been playing with this for an hour this morning and now I have a headache!
 
Commissario
OP
Joined
16 Oct 2002
Posts
342,208
Location
In the radio shack
Thanks, we're so nearly there!

There are other instances of ringstyle within the file so running that command does what is needed on the first instance but destroys the file following the second instance. I've tried removing the g from the /igs but that doesn't seem to stop it. The file goes from 1900 lines long down to a little under 800.

It's also behaving a bit funky. I can see exactly what it's supposed to be doing. As you say it looks for ringstyle and then replaces everything between ringstyle and }) with the following text. Except that it's not quite right.

It does as it's supposed to but then truncates the next line from }); to a single }

It goes from this:
oIDW1wd.png
to this
XMuajYc.png

Which it shouldn't do.

Here's the exact command I'm running

Code:
perl -0777 -i.original -pe  "s/ringStyle.*\}\)/ringStyle = new ol.style.Style\(\{\nfill: null,\nstroke: new ol.style.Stroke\(\{\n color: '#0000DD',\n lineDash:[4,4],\nwidth: 2\n\}\)/igs" derptastic.js

If I change the final 2\n\}\) to be 2\n\}\)feek then the output above will have })feek on the penultimate line and the last line will be }

:confused:
 
Commissario
OP
Joined
16 Oct 2002
Posts
342,208
Location
In the radio shack
Sussed it - Well partly.

The brackets were the wrong way round in the command. I've corrected that and it runs. I now go from:

Code:
                var ringStyle = new ol.style.Style({
                        fill: null,
                        stroke: new ol.style.Stroke({
                                color: '#000000',
                                width: 1
                        })
                });
to
Code:
                var ringStyle = new ol.style.Style({
fill: null,
stroke: new ol.style.Stroke({
 color: '#0000DD',
 lineDash:[4,4],
width: 2
})
});

}

But. See that there's a final } there.

My new file is 787 lines compared to 1907 for the original. That } has not been appended by the command, it's just at the end of a huge chunk of code that's been removed.

Also I think this pattern is too loose and is matching too much and replacing extra stuff.
Yes. We're nearly there, it's just this bit.
 
Commissario
OP
Joined
16 Oct 2002
Posts
342,208
Location
In the radio shack
Thanks. I did have a quick look after you posted that last night but didn't do anything with it as I was full of beer! This morning I've had a good breakfast and am back on the case.

No error.

No change in the file. Which means that it's not finding a match.

To test further, rather than run it over my original source, I've created derpy.js and put this into it:

Code:
             var ringStyle = new ol.style.Style({
                        fill: null,
                        stroke: new ol.style.Stroke({
                                color: '#000000',
                                width: 1
                        })
                });

Run the command

Code:
sudo perl -0777 -i.original -pe  "s/var\s*ringStyle{1}\s*=\s*new\s*ol\.style\.Style{1}\(\{\n\s*fill:\s*null,\n\s*stroke:\s*new\s*ol\.style\.Stroke{1}\(\{\n\s*colour{1}:\s*'#000000',\n\s*width{1}:\s*1\n\s*\}\)\n\s*\}\);{1}/var ringStyle = new ol.style.Style\(\{\nfill: null,\nstroke: new ol.style.Stroke\(\{\n color: '#0000DD',\n lineDash:[4,4],\nwidth: 2\n\}\)\n\}\);\n/igs" derpy.js

No change.

It looks to me as though it should work, the format looks right. I'm stumped.
 
Commissario
OP
Joined
16 Oct 2002
Posts
342,208
Location
In the radio shack
Aww man, I've spent more time examining that than a sixteen year old spent studying the underwear section of the Grattan catalogue in the early 80s looking for a hint of areola :D

I can't find it and the clue didn't help (although I did waste half an hour looking at old adverts).
 
Back
Top Bottom