LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Non greedy search in bash (https://www.linuxquestions.org/questions/linux-newbie-8/non-greedy-search-in-bash-763964/)

rajeshksv 10-23-2009 09:03 AM

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

deathalele 10-23-2009 09:10 AM

i'm not entirely sure what you mean.

you could try sed


str='abcdefgcdhd'
echo $str|sed -e 's/defgcd/rgt/'


hope this helps

pixellany 10-23-2009 09:13 AM

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?

ghostdog74 10-23-2009 09:23 AM

non greedy means: for example
Code:

<tag1>sdflsfj<tag2>
you want to match words inside <>. Not from the first < to the last >.

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..

rajeshksv 10-23-2009 10:22 AM

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

ghostdog74 10-23-2009 10:49 AM

Code:

# more file
<table><tr><td>cell1</td><td>cell2</td></tr>
<tr><td>cell3</td><td>cell4</td></tr>
</table>

$ awk 'BEGIN{RS="</tr>"}NR==1{gsub(/<tr>.*/,"");print;next}NR>1{print $0RT}'  file
<table>

<tr><td>cell3</td><td>cell4</td></tr>

</table>

not sure if the output is what you want though. the method is split by "</tr>" as the record separator since this denotes end of one row.

Quote:

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
write this in awk
Code:

/ba/ && !/bala/

lutusp 10-23-2009 02:45 PM

Quote:

Originally Posted by rajeshksv (Post 3729915)
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

This is a multi-line editing task and none of the options you list are suited for it (all of them can be made to do multi-line edits, but only by selling your soul to some mythical creature). It's time to learn how to write your scripts in something other than Bash. Options:

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.

rajeshksv 10-24-2009 02:47 AM

@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 :)

chrism01 10-24-2009 09:27 PM

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:

Although most modern programming languages offer primitive pattern matching tools, usually through an extra library, Perl's patterns are integrated directly into the language core. Perl's patterns boast features not found in pattern matching in other languages, features that encourage a whole different way of looking at data.
.
.

Pattern matching isn't like direct string comparison, even at its simplest. It's more like string searching with mutant wildcards on steroids.
Perl Cookbook: highly recommended.

rajeshksv 10-26-2009 11:08 AM

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

chrism01 10-26-2009 06:37 PM

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 ;)

ghostdog74 10-26-2009 07:04 PM

Quote:

Originally Posted by rajeshksv (Post 3733008)
.Can I use system() and use the above command :)

yes of course, but why do that if you want to use Perl? you make your code non portable.

Quote:

I know that if I do that I am just using bash with perl as a beautiful
wrapper .Am I missing anything here??
yes you are. like what i mentioned above. However, for text parsing like in your case, awk is as good as Perl and sometimes better so if you are not comfortable with Perl yet, stick back to what you know. Many things can still be done easily with the shell.

lutusp 10-27-2009 01:21 AM

Quote:

Originally Posted by rajeshksv (Post 3730650)
But are there tools in perl which are as good as grep/awk ??

Say what? Perl is 100 times better for text processing than grep times akw raised to the third power. No contest. Grep and awk are weak imitations.

Quote:

Originally Posted by rajeshksv (Post 3730650)
Any good links/books for learning perl quickly

Google is your friend.

rajeshksv 10-27-2009 01:32 AM

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 :)

foodown 10-27-2009 01:36 AM

Quote:

Originally Posted by lutusp (Post 3733752)
Say what? Perl is 100 times better for text processing than grep times akw raised to the third power. No contest. Grep and awk are weak imitations.

I must take issue with this characterization.

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.