LinuxQuestions.org
Review your favorite Linux distribution.
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 11-02-2009, 10:02 AM   #1
t0x
LQ Newbie
 
Registered: Apr 2007
Posts: 2

Rep: Reputation: 0
Another sed thread ...


Hi,

I need help. I now how to use regular expressions, and I thought that I could apply them directly to sed but it doesn't seem to work.

I need to append a string to multiple php files like this :

Quote:
startwebtext( 0, 'top_bulhosa_titulo' );?><?=$main_title;?><?$main_title=stopwebtext(false);
/*startWebText(0,"instock_message");echo("Em stock, enviamos em 24 horas .");
<h3><%startWebText(0,"sinopse_detalhe_produto")%>Sinopse<%stopWebText()%></h3>
<h3><%startWebText(0,"livro_por_dentro")%>O livro por dentro<%stopWebText()%></h3>

Every time I find something with "startwebtext" I want to add a string right after it.

eg.

This :

startwebtext( [number], [content] );

should become this :

startwebtext( [number], [NEW TEXT] . [content] );


this is not working :

sed -e 's/startwebtext\(\s*([0-9]),\s*(.*?)\s*\);/startwebtext\( $1, \$_GLOBALS['test'] . $2 );/ig' file
 
Old 11-02-2009, 11:43 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
It usually helps to post the exact results you get when you say something "isn't working".

Is "\$_GLOBALS['test']" supposed to be a literal string, or a shell variable/array value that gets replaced by the shell? If the latter, you'll have to replace the single quotes around the sed expression with double quotes (or "unquote" the string itself) so that the shell can expand it.

It's better to go with a full regex expression for something like this. The following assumes that you want a case-insensitive match. It works on your example string, but I can't guarantee it will match every possible combination in your file.
Code:
sed -r -e 's/startwebtext?\([ ]?([0-9]),[ ]([^)]*)\)/startWebText(\1, $_GLOBALS['test'] . \2)/Ig'

Last edited by David the H.; 11-02-2009 at 11:44 AM.
 
Old 11-02-2009, 12:29 PM   #3
Dr_P_Ross
Member
 
Registered: Nov 2003
Location: Edinburgh, UK
Distribution: Arch
Posts: 43

Rep: Reputation: 18
tOx,

You wrote:
Quote:
this is not working :
sed -e 's/startwebtext\(\s*([0-9]),\s*(.*?)\s*\);/startwebtext\( $1, \$_GLOBALS['test'] . $2 );/ig' file
There are a couple of things wrong here. In the replacement you have used
$1 and $2, presumably intended to be the first and second parts of the old content of startwebtext(...), but the sed syntax should be \1 and \2 rather than $1 and $2. Also, in the match part of the s/../../, there is only one pair of \(..\), and that pair ends just before the semicolon.

I think you want something like this instead:
Quote:
sed -e 's/startwebtext(\(\s*[0-9]\s*,\)\([^)]*\));/startwebtext(\1 \$_GLOBALS['test']. \2 );/igp' file
In this, the first \(..\) captures the content up to and including the comma, maybe with spaces around the single digit. The second \(..\)
captures everything after the comma that does not include a right parenthesis. And the match expects to see a final right parenthesis and semicolon.

This does of course assume that the text string after the comma does not include a right parenthesis, but if so you can adjust it to suit.

Last edited by Dr_P_Ross; 11-02-2009 at 12:31 PM.
 
Old 11-02-2009, 06:04 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
don't have to use long messy regex for this.
Code:
awk -vFS=","  '/startwebtext/{
   printf "%s,%s" ,$1,"new words"
   for(i=2;i<NF;i++) printf $i FS
   printf $NF
}'  file
output
Code:
$ more file
startwebtext( [number], [content] );
$ ./shell.sh
startwebtext( [number],new words [content] );
 
Old 11-03-2009, 06:05 AM   #5
Dr_P_Ross
Member
 
Registered: Nov 2003
Location: Edinburgh, UK
Distribution: Arch
Posts: 43

Rep: Reputation: 18
ghostdog74,

Although awk is very handy for quick solutions to many tasks, your suggested solution is not general enough. It supposes that startwebtext occurs before the first comma on a line, and at most once per line. And in strings (eg line 2 of tOx's sample input), white space after commas would be munged. For case-insensitive matching, you would also need the gawk-specific IGNORECASE=1 or similar.
 
Old 11-03-2009, 06:47 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Dr_P_Ross View Post
ghostdog74,

Although awk is very handy for quick solutions to many tasks, your suggested solution is not general enough.
who says every suggestion i post have to be general and solve every possible cases? none of the sed solutions posted address the issue of multiple lines either. If a thorough solution is desired, then more data is needed for test cases. But whatever it is, awk is still the better tool to use, period
 
Old 11-03-2009, 07:04 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Hmmmmm---I guess the OP would have to define the general formula: What is "good enough" depends on the specific data set, n'est-ce pas?

Quote:
But whatever it is, awk is still the better tool to use, period
I'm quite sure that I read somewhere that it is a badge of honor to solve problems with SED, if at all possible---preferably with code that is as obfuscated as possible........

Seriously, the tool to use is:
-the one that works
-the one you know how to use

Now where is the OP?
 
Old 11-03-2009, 07:23 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by pixellany View Post
I'm quite sure that I read somewhere that it is a badge of honor to solve problems with SED,
where did you read that?
use sed only for simple subs.

The tool to use is the one that works and at the same time, makes code easy to read and understand.
 
Old 11-03-2009, 07:46 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by ghostdog74 View Post
where did you read that?
use sed only for simple subs.

The tool to use is the one that works and at the same time, makes code easy to read and understand.
understand by who? A professional programmer has an obligation to write code so that his/her stakeholders can understand it. If I am writing code for my own use, then what counts is whether I understand the utility well enough to solve the problem. Thus I often use SED because I understand it better.

I am however in awe of the AWK gurus, and I will learn it someday.

Quote:
Quote:
I'm quite sure that I read somewhere that it is a badge of honor to solve problems with SED
where did you read that?
Feeble attempt at humor.....
 
Old 11-03-2009, 07:57 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by pixellany View Post
understand by who? ...
...
A professional programmer has an obligation to write code so that his/her stakeholders can understand it.
Stakeholder's do not need to know what you do with your programs. All they need to know is whether your company is making money. Even system owners don't have to know code details . They just need to know the business sense of it. The actual person who is going to troubleshoot your code if things happen, is the one that needs to read your code.
 
Old 11-03-2009, 08:00 AM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Sorry, I used "stakeholders" a bit loosely. Your description is correct.
 
Old 11-03-2009, 09:16 AM   #12
Dr_P_Ross
Member
 
Registered: Nov 2003
Location: Edinburgh, UK
Distribution: Arch
Posts: 43

Rep: Reputation: 18
I think we can all agree that awk is more powerful and useful (and bigger) than sed, just as perl is even more powerful (and bigger).
Sed still has its uses. Harking back to the original question,
we don't know in which field, as defined by the FS field separator,
the "startwebtext" appears, so an awk solution might need to use
gawk's gensub function .. in which case we'd still need a slightly non-trivial regexp.

More generally, don't just stick with one tool. The more you learn, the
more common ground you find and the easier it becomes to be versatile.
 
Old 11-03-2009, 02:36 PM   #13
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
I don't think it matters if you use sed or awk, as long as it works and is readable (at least to you).

Usually awk is for tables. If you're dealing with tables, awk or perl. For substitution, deleting, and other quick modifications then you should probably use sed. But, it's your choice, it's just that you may have a harder time using the not as appropriate tool.
 
Old 11-03-2009, 02:50 PM   #14
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I'll add to my list of criteria for the "right" tool: The one that is closest. I once used my hair brush as a hammer. It was there, and getting a hammer would have been an unknown mission (in a hotel).

Applied to scripting, the message is that the best tool is often the one you can find the quickest ("find" includes looking up some specific syntax.) If you use Google to find your tools, then "best" might simply mean "most popular" (which in turn = the one that is closest.)
 
Old 11-03-2009, 02:52 PM   #15
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
... but a hair brush will take more hits to drive a nail, and it can break more easily ...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
python thread safety: printing from thread to redirected stdout - safe? BrianK Programming 2 10-11-2010 11:28 AM
thread creating another thread on 2.4 kernel after long run ashok449 Linux - Kernel 1 02-20-2009 04:26 AM
the return value of getpid() called from main thread and new thread r identical !!! cryincold Programming 3 02-29-2008 01:37 AM
Main thread sending notification to child thread rajesh_b Programming 1 09-22-2004 09:15 AM
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 04:37 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