LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-03-2022, 02:06 PM   #1
tercel
Member
 
Registered: Aug 2022
Posts: 54
Blog Entries: 23

Rep: Reputation: 0
Question Why did not "sed" replace as expected?


I created a simple bash shell script to change multiple html files of my website at the same time :

Code:
#!/bin/bash
# $1 extension of the file to be changed
# $2 path where the files to ve changed found - all files should be in the that directory

files=$(find "$2" -name "*.${1}")

for file in $files
do
 echo "$file" is changing...
 sed -i 's/<h3\><\?php/<h3 class="DateHeader"\><\?php/g' "$file"
done
I checked my code several times in https://www.shellcheck.net/ to see the errors, I corrected all of them. Final code is at above. I expect to change this line :

Quote:
<h3><\?php echo "Date : "."Today"; ?>
to this

Quote:
<h3 class="DateHeader"><\?php echo "Date : "."Today"; ?>
(I had to put '\' before all '?'s, including the shell script otherwise site blocked my post since it treat my code as php script So in my original script there is no backslash before ? )

But my code did not worked as I expected. Can anybody say that where is my error? Thanks.

Edited :

I found my mistake :

the backslashes before ">" in sed command prevents change; they do not needed ! Below code works:

Code:
sed -i 's/<h3><\?php/<h3 class="DateHeader"><\?php/g' "$file"
(without backslahes in front of ?)

Last edited by tercel; 09-03-2022 at 02:54 PM. Reason: solution update
 
Old 09-03-2022, 03:38 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
It would be helpful if you would explain how it didn't work, not just that it didn't work.

I see you have resolved it while I was trying to get my own reply past the CloudFlake police, so I will add that it would be helpful to future visitors if you wold also post your solution.

First, the escaped '?' in the replacement text is also unnecessary.

You have the use of the escaped '?' backwards - it needs to be escaped when using extended regular expressions (sed -r), not when using basic regular expressions as you are doing. From the GNU sed manual:

Code:
The only difference between basic and extended regular expressions
is in the behavior of a few characters: ‘?’, ‘+’, parentheses, and braces
(‘{}’). While basic regular expressions require these to be escaped if 
you want them to behave as special characters, when using extended 
regular expressions you must escape them if you want them to match a
literal character.
So, in your example, this should work:

Code:
sed -i 's/<h3><?php/<h3 class="DateHeader"><?php/g'

Otherwise this should work with extended regular expressions:

Code:
sed -ri 's/<h3><\?php/<h3 class="DateHeader"><?php/g'
Or simply put the '?' inside square brackets:

Code:
sed -ri 's/<h3><[?]php/<h3 class="DateHeader"><?php/g'
...which should work in either case.

Last edited by astrogeek; 09-03-2022 at 06:35 PM. Reason: Take that CloudFlake! Testing...
 
1 members found this post helpful.
Old 09-04-2022, 08:41 AM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,817

Rep: Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211
The \? is special in GNU sed; a literal ? is without \
(unless in sed -r or sed -E where ? becomes special and must be escaped)
Apparently was used here to escape the quick Wiki editor - you better go to the advanced editor. That also has got a preview button...

The \> is special in many sed versions; a literal > is without \
 
1 members found this post helpful.
Old 09-04-2022, 10:10 AM   #4
tercel
Member
 
Registered: Aug 2022
Posts: 54

Original Poster
Blog Entries: 23

Rep: Reputation: 0
Question

hi astrogeek and MageInGermany, thanks for detail explanations that are always valuable for me, I hope the same for all.

Actually I tried to explained in my OP that I "escaped" '?' because LQ blocked my post otherwise as MadeInGermany explained.

astrogeek, how could you use php start tag without a problem?
As MadeInGermany said,now I am using an advanced editor of LQ, and I copied the sed code you used in your post to here between CODE tags, when I used preview LQ again blocked my post! (Editor vs. tercel)
 
Old 09-04-2022, 11:41 AM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
Quote:
Originally Posted by tercel View Post
hi astrogeek and MageInGermany, thanks for detail explanations that are always valuable for me, I hope the same for all.
...
astrogeek, how could you use php start tag without a problem?
You are welcome!

What I figured out was it is the complete <?php string of characters which upsets the man-in-the-middle sitting in the cloud, so you need to break that up with something which does not produce visible characters in the browser. There are several choices in the markup allowed here, but simple empty [i][/i] or [b][/b] is about as easy as it gets!

Code:
<h3><[i][/i]?php echo "Date : "."Today"; ?> 

     ...produces...

<h3><?php echo "Date : "."Today"; ?>
There may be other ways. Surprisingly, I just ran into this and figured this out for the first time when replying to your post, so others may know an even simpler way.

Last edited by astrogeek; 09-04-2022 at 11:45 AM. Reason: tpoys, tyops, ptoys
 
1 members found this post helpful.
Old 09-04-2022, 03:32 PM   #6
tercel
Member
 
Registered: Aug 2022
Posts: 54

Original Poster
Blog Entries: 23

Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by astrogeek View Post
Surprisingly, I just ran into this and figured this out for the first time when replying to your post, so others may know an even simpler way.
That is a great solution! I am taking my hat off to you astrogeek!
 
  


Reply

Tags
sed



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
unable to replace "ö" to "p" in shell scripting with "sed" meninmech Programming 5 06-22-2012 02:58 PM
[SOLVED] Replace a string @CURRANGE("***","***") to @CURRANGE("xxx","xxx") in a file mavadikarmayur Linux - Newbie 3 03-26-2012 08:32 AM
[SOLVED] Sed replace Help replace for (<a> guessity Linux - Newbie 1 11-18-2011 09:05 AM
sed - use sed to replace multiple instances from "x" to "y" in a line mrodmac Linux - General 4 02-02-2010 11:37 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration