You are not logged in.

#1 2008-10-27 23:45:19

Stythys
Member
From: SF Bay Area
Registered: 2008-05-18
Posts: 878
Website

Sed Special Characters

Can someone help me with a sed string?

Basically what I want to run is: sed -i 's/<?/<?php/g' *.php

but it has trouble with the '<' characters and I've tried googling but can't for the life of me figure out how to parse this correctly hmm


[home page] -- [code / configs]

"Once you go Arch, you must remain there for life or else Allan will track you down and break you."
-- Bregol

Offline

#2 2008-10-27 23:49:56

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,498
Website

Re: Sed Special Characters

My guess is that it is the ? character that is causing the problem.  Haven't tested but "s#<\?#<\?php#" should get you close...

Offline

#3 2008-10-27 23:54:45

Stythys
Member
From: SF Bay Area
Registered: 2008-05-18
Posts: 878
Website

Re: Sed Special Characters

well, it ran successfully...

put '<?php' at the beginning of every single line tho haha


[home page] -- [code / configs]

"Once you go Arch, you must remain there for life or else Allan will track you down and break you."
-- Bregol

Offline

#4 2008-10-28 00:07:58

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Sed Special Characters

\? is a special regex character. It matches 0-1 instances.

Definitely use ?

It changed (only) the beginning of every line because it didn't have the g option.

Same goes for \<. It is used to match words. But just < should work.

So I don't see anything wrong with your initial command. What was the error message?

Offline

#5 2008-10-28 00:09:31

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,498
Website

Re: Sed Special Characters

oops....  tongue

it appears my sed-foo is weak

Offline

#6 2008-10-28 00:11:52

Stythys
Member
From: SF Bay Area
Registered: 2008-05-18
Posts: 878
Website

Re: Sed Special Characters

adding a 'g' onto the end replaces every single character with '<?php' hmm


[home page] -- [code / configs]

"Once you go Arch, you must remain there for life or else Allan will track you down and break you."
-- Bregol

Offline

#7 2008-10-28 01:37:04

Stythys
Member
From: SF Bay Area
Registered: 2008-05-18
Posts: 878
Website

Re: Sed Special Characters

anyone? hmm


[home page] -- [code / configs]

"Once you go Arch, you must remain there for life or else Allan will track you down and break you."
-- Bregol

Offline

#8 2008-10-28 01:48:45

creslin
Member
Registered: 2008-10-04
Posts: 241

Re: Sed Special Characters

Stythys wrote:

adding a 'g' onto the end replaces every single character with '<?php' hmm

Are you saying "every single character" literally?  'a' gets replaced with <?php, 'b' gets replaced with <?php, etc?  Can you post an example of 1 file before and after?  I just did some quick tests and sed  -i  's/<?/<?php/g'  *.php works fine for me to replace every occurrence of <? with <?php in a directory's .php files.


ARCH|awesome3.0 powered by Pentium M 750 | 512MB DDR2-533 | Radeon X300 M
The journey is the reward.

Offline

#9 2008-10-28 01:57:09

Stythys
Member
From: SF Bay Area
Registered: 2008-05-18
Posts: 878
Website

Re: Sed Special Characters

when I run the command you listed I get output like this

Last edited by Stythys (2008-10-28 01:59:40)


[home page] -- [code / configs]

"Once you go Arch, you must remain there for life or else Allan will track you down and break you."
-- Bregol

Offline

#10 2008-10-28 02:03:09

Stythys
Member
From: SF Bay Area
Registered: 2008-05-18
Posts: 878
Website

Re: Sed Special Characters

wait...sorry creslin, I was running your command on my files after I already screwed them up, forgot to revert tongue

and yeah your command worked, thanks. I'm almost certain that's what I was originally running....ah well


[home page] -- [code / configs]

"Once you go Arch, you must remain there for life or else Allan will track you down and break you."
-- Bregol

Offline

#11 2008-10-28 02:07:34

creslin
Member
Registered: 2008-10-04
Posts: 241

Re: Sed Special Characters

yeah... the sed in your original looked fine.. /shrug


ARCH|awesome3.0 powered by Pentium M 750 | 512MB DDR2-533 | Radeon X300 M
The journey is the reward.

Offline

Board footer

Powered by FluxBB