Palace Chan
Palace Chan

Reputation: 9183

Perl regexp replace?

I'm checking to see if I can replace an include like this:

#include <pathFoo/whatever.H>

with:

#include "whatever.H"

to do so I would use the -i switch, but to check I am doing it correctly I am simply using the -p switch without -i. I have the following command:

perl -p -e 's/<pathFoo\/\(.*\)>/"$1"' thefile

but this isn't quite working and i'm not exactly sure which part is off?

Upvotes: 1

Views: 118

Answers (4)

TLP
TLP

Reputation: 67900

Since TIMTOWTDI, here's a double substitution. Or rather a substitution and a transliteration:

perl -pe 's|^#include\s+<\KpathFoo/|| && tr/<>/""/'

So, just remove the pathFoo/ part first, and if that succeeds, then transliterate the <> characters to quotes.

Upvotes: 1

ikegami
ikegami

Reputation: 385565

perl -i~ -pe's!<pathFoo/(.*)>!"$1"!' file

The following is safer:

perl -i~ -pe's!#include\s+\K<pathFoo/(.*)>!"$1"!' file

Upvotes: 1

ddoxey
ddoxey

Reputation: 2063

I think this will get you there:

perl -pi -e 's{#include[ ]+<.+?/([^/]+)>}{#include "$1"}g' file

Upvotes: 0

ErikR
ErikR

Reputation: 52029

You don't want to escape the parens, i.e.:

perl -p -e 's/<pathFoo\/(.*)>/"$1"/' thefile

should work for you.

Also note the ending /.

Upvotes: 4

Related Questions