LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-09-2011, 06:33 AM   #1
akif05
LQ Newbie
 
Registered: Dec 2008
Location: Bulgaria - Varna
Distribution: slackware
Posts: 8

Rep: Reputation: 0
Question help for sed


Please help for solving problem with sed

Thank you in advance

#by executing bash file
#!/bin/sh

sed -i 's/if (IEexec || domExec) document.write('<iframe id="IdMyIframe" '+IframePropriedades+'></iframe>');//g' *.php

output is:

line 4: syntax error near unexpected token `<'
line 4: `sed -i 's/if (IEexec || domExec) document.write('<iframe id="IdMyIframe" '+IframePropriedades+'></iframe>');//g' *.php'

Last edited by akif05; 03-09-2011 at 06:35 AM. Reason: sed
 
Old 03-09-2011, 06:44 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
You probably need to escape quotes and slashes (i.e. the slash in </iframe>).
 
Old 03-09-2011, 06:53 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The problem is that the pattern contains single quotes, so that they are open and closed alternatively, leaving part of the string to the shell which tries to interpret it with its own rules. Basically you can keep the quotes around the whole sed command and escape the inner ones. Moreover you have to use another separator in the sed command (other than slash) to avoid confusion with the literal slashes in the pattern. Something like this should work:
Code:
sed 's:if (IEexec || domExec) document.write('\''<iframe id="IdMyIframe" '\''+IframePropriedades+'\''></iframe>'\'');::g' file.php
In this example the opening quote is closed, then followed by an escaped quote (in red) and then re-opened. In alternative you can use double quotes to embed the sed command and escape the double quotes inside the pattern:
Code:
sed "s:if (IEexec || domExec) document.write('<iframe id=\"IdMyIframe\" '+IframePropriedades+'></iframe>');::g" file.php
Please test these solution without the -i option, until you're sure of the results.
 
1 members found this post helpful.
Old 03-09-2011, 06:56 AM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
Moreover you have to use another separator in the sed command (other than slash) to avoid confusion with the literal slashes in the pattern.
Cool, I didn't realise you could do that.
 
Old 03-09-2011, 07:06 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
I usually use the hash sign, as the separator, when editing xml or html files in sed, or vim. Many people use the bar sign| instead. These stand out more, but any character will do that isn't in the strings or has another use.

Using double quotes is necessary if you want to embed the value of a variable in the sed command. So remember to escape the $ sign if it is in the sed command normally.
 
Old 03-09-2011, 07:07 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Nylex View Post
Cool, I didn't realise you could do that.
One of the many tricks of sed. You can also use a different delimiter for regular expressions in sed addresses. Suppose you have a slash inside the regexp: you can't do:
Code:
sed -n '/some/text/p' file
but you can put a backslash as first character of the sed command followed by a custom delimiter:
Code:
sed -n '\:some/text:p' file
 
1 members found this post helpful.
Old 03-09-2011, 08:29 AM   #7
akif05
LQ Newbie
 
Registered: Dec 2008
Location: Bulgaria - Varna
Distribution: slackware
Posts: 8

Original Poster
Rep: Reputation: 0
sed "s:if (IEexec || domExec) document.write('<iframe id=\"IdMyIframe\" '+IframePropriedades+'></iframe>');::g" file.php

that's the solution.
Thanks a lot
 
  


Reply



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
[SOLVED] sed 's/Tb05.5K5.100/Tb229/' alone but doesn't work in sed file w/ other expressions Radha.jg Programming 6 03-03-2011 07:59 AM
sed: how to change MAC address string with sed cold Linux - Software 5 08-02-2010 07:43 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:32 AM.

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