ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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
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
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
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.
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.
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.
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.)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.