LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-23-2009, 10:03 AM   #1
rajeshksv
LQ Newbie
 
Registered: Jan 2009
Posts: 10

Rep: Reputation: 0
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
 
Old 10-23-2009, 10:10 AM   #2
deathalele
Member
 
Registered: May 2008
Location: deep dark north wales
Distribution: Zenwalk
Posts: 162

Rep: Reputation: 30
i'm not entirely sure what you mean.

you could try sed


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


hope this helps
 
Old 10-23-2009, 10:13 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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?
 
Old 10-23-2009, 10:23 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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..

Last edited by ghostdog74; 10-23-2009 at 10:24 AM.
 
Old 10-23-2009, 11:22 AM   #5
rajeshksv
LQ Newbie
 
Registered: Jan 2009
Posts: 10

Original Poster
Rep: Reputation: 0
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
 
Old 10-23-2009, 11:49 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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/
 
Old 10-23-2009, 03:45 PM   #7
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by rajeshksv View Post
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.
 
Old 10-24-2009, 03:47 AM   #8
rajeshksv
LQ Newbie
 
Registered: Jan 2009
Posts: 10

Original Poster
Rep: Reputation: 0
@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
 
Old 10-24-2009, 10:27 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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.
 
Old 10-26-2009, 12:08 PM   #10
rajeshksv
LQ Newbie
 
Registered: Jan 2009
Posts: 10

Original Poster
Rep: Reputation: 0
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
 
Old 10-26-2009, 07:37 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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
 
Old 10-26-2009, 08:04 PM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by rajeshksv View Post
.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.
 
Old 10-27-2009, 02:21 AM   #13
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by rajeshksv View Post
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 View Post
Any good links/books for learning perl quickly
Google is your friend.
 
Old 10-27-2009, 02:32 AM   #14
rajeshksv
LQ Newbie
 
Registered: Jan 2009
Posts: 10

Original Poster
Rep: Reputation: 0
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
 
Old 10-27-2009, 02:36 AM   #15
foodown
Member
 
Registered: Jun 2009
Location: Texas
Distribution: Slackware
Posts: 611

Rep: Reputation: 219Reputation: 219Reputation: 219
Quote:
Originally Posted by lutusp View Post
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.

Last edited by foodown; 10-27-2009 at 02:40 AM.
 
  


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
Bash text search query basildon Linux - Newbie 2 09-17-2008 08:41 PM
Search and Replace from bash hammertime1983 Linux - Newbie 4 11-02-2007 04:18 PM
Non greedy Regex in C crazyjimbo Programming 5 11-15-2006 12:19 PM
Something is greedy (do you know what) command line xpression Linux - General 2 09-22-2005 05:44 PM
case of the greedy gui wlfdgcrkz Linux - Software 12 04-29-2003 07:30 PM


All times are GMT -5. The time now is 02:18 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration