Non greedy search in bash
Is there a way to search non greedily as we do in perl??
Say I have a string str="Abcdefgcdhd"; Now I want to replace "defgcd" with "rgt" In perl I can use str =~ s/d.*?d/rgt But with bash how can I acheive this non greedy search.Is there any .*? in bash?? Thanks Rajesh KSV |
i'm not entirely sure what you mean.
you could try sed str='abcdefgcdhd' echo $str|sed -e 's/defgcd/rgt/' hope this helps |
Hop over to http://tldp.org and get the Bash Guide for Beginners. Later, get the Advanced Bash Scripting Guide.
At some point, Regexes are all the same---but they are used differently in different utilities. And of course there are "standard" and "extended" flavors What is the precise meaning of "*?" in PERL? |
non greedy means: for example
Code:
<tag1>sdflsfj<tag2> bash doesn't yet support non greediness, so is sed /awk (correct me if i am wrong). the other way is to do pure string manipulation. @OP, show examples of input and output you want.. |
Thanks to all for your quick replies.
I have an html page which contains a table <table><tr><td>cell1</td><td>cell2</td></tr> <tr><td>cell3</td><td>cell4</td></tr> </table> I want to delete the first row.I dont know what will be contents of cells. sed 's|<table><tr>.*</tr>|<table>|' doesnt work.What should I do?? ----I.e,how to achieve Non greedy search And one more thing I want to search for words which contain "ba" but not followed by "la" .i.e., I want to search for a specific pattern which shouldnt be followed by another specific pattern.How can I write a regex for that in awk/sed Thanks in Advance |
Code:
# more file Quote:
Code:
/ba/ && !/bala/ |
Quote:
Ruby Python Perl Wait a minute. If you already know how to write in Perl, why are you trying to perform this obvious Perlish task in Bash? This is what Perl was intended to do -- its purpose in life. The same could be said of Python and Ruby, with the difference that the latter languages are easier to read. |
@lutsup ; Thanks for your suggestion.But are there tools in perl which are as good as grep/awk ??
Any good links/books for learning perl quickly :) |
Perl is at least as good, if not better. In fact, many tools use the PCRE - Perl compatible regex engine.
http://perldoc.perl.org/ http://www.perlmonks.org/?node=Tutorials Quote:
|
Good links.That perl cookbook is really good.But still I find difficulty in using perl.How can I grep through gz files in perl (without compressing them)
In bash I can say count=$(zgrep "Error" /logs/2009/10/12/*/*gz | wc -l) But in perl its looks like a big job.First I have to read each file using Archive::Zip and then use grep on each file.I searched on net but was not able to find a better solution.Can I use system() and use the above command :) I know that if I do that I am just using bash with perl as a beautiful wrapper .Am I missing anything here?? Thanks in Advance |
Try any of
http://search.cpan.org/~pmqs/IO-Comp.../AnyInflate.pm http://search.cpan.org/~softdia/Tie-...ib/Tie/Gzip.pm http://search.cpan.org/~nwclark/Perl...p-0.17/gzip.pm I think prob 1 or 3, but you could just go to search.cpan.org and enter 'gzip' in the search box. Anyway, that's what I did ;) |
Quote:
Quote:
|
Quote:
Quote:
|
Yup I can go to awk but I find things in my project which I cant do with awk easily as I can do with perl.. I was actually searching for "search.cpan.org" sort of site only.Thanks crism :)
|
Quote:
First off, perl is certainly great. It brings a lot of overhead, though . . . for writing simple shell scripts or constructing effective one-time commands, grep, sed, and awk are fan-friggin-tastic. They are, however, certainly not to be wasted on the profane. When used in concert, grep, sed, awk can do almost anything with text. They also all three pre-date perl, which shows up for the first time in 1987, and so it is impossible for them to be imitations, weak or otherwise. |
All times are GMT -5. The time now is 12:26 AM. |