From: Ed Morton on 8 Jun 2010 14:55 On Jun 8, 11:40 am, gaze...(a)shell.xmission.com (Kenny McCormack) wrote: > In article <d8cbd965-3be1-4c65-b97f-b1a170722...(a)y11g2000yqm.googlegroups..com>, > Ed Morton <mortons...(a)gmail.com> wrote: > ... > > >I wouldn't bother with fgrep or egrep. If you have to do anything more > >complicated than that you may as well learn sed or awk as, with the > >exception of a couple of cute GNU grep extensions, their syntax is as > >simple as greps for what grep does but they're extensible to do things > >grep can't do. The most extensible with the simplest syntax for > >anything even moderately complex is awk but sed is also very useful > >for simple searching/subsitutions on a single line. > > I agree with your general sentiment, and also your recommendation of AWK > (as the best balance between high level and low level). I disagree with > the (tepid) recommendation of sed - I find that once one knows AWK, sed > drops off the radar. YMMV but I use sed for the trivial throw-away things like: $ echo "foo" | sed 's/o$/x/' fox $ echo "foo" | awk '{sub(/o$/,"x")}1' fox so I still find it useful to know the sed syntax for simple substitutions on a single line as it just saves a little bit of typing compared to awk in that specific case. Ed.
From: Ed Morton on 8 Jun 2010 15:08 On Jun 8, 12:24 pm, Janis Papanagnou <janis_papanag...(a)hotmail.com> wrote: > kaushal wrote: > > Hi, > > > can someone explain me about the usage of grep , fgrep and egrep with > > examples. > > In what contexts i should use this. > > grep - if you're using simple regexps > fgrep - if you use literal strings > egrep - if you use extended regexps > > Some samples to illustrate... > > echo 'xyz' | grep x.z > echo 'xyz' | fgrep x.z > > echo 'hello' | grep 'hello|world' > echo 'hello' | egrep 'hello|world' > > And of course, as already proposed, man grep is your friend. Right, and for the OP just be aware of this: $ echo 'hello world' | egrep 'hello|world' hello world $ echo 'hello world' | awk '/hello/||/world/' hello world $ echo 'hello world' | egrep 'hello&world' <- note no output $ echo 'hello world' | awk '/hello/&&/world/' hello world To simply check for BOTH "hello" and "world" appearing in any order of the same line, with *grep you need to specify the combination of words in every possible order: $ echo 'hello world' | egrep 'hello.*world|world.*hello' hello world or create a pipline of separate grep commands: $ echo 'hello world' | grep 'hello' | grep 'world' hello world whereas with awk you can just AND ("&&") the REs: $ echo 'hello world' | gawk '/hello/&&/world/' hello world just like if you wanted to check for either word you could just OR ("||") the REs: $ echo 'hello world' | gawk '/hello/||/world/' hello world Regards, Ed.
From: Janis Papanagnou on 8 Jun 2010 20:34 Ed Morton wrote: > On Jun 8, 12:24 pm, Janis Papanagnou <janis_papanag...(a)hotmail.com> > wrote: >> kaushal wrote: >>> Hi, >>> can someone explain me about the usage of grep , fgrep and egrep with >>> examples. >>> In what contexts i should use this. >> grep - if you're using simple regexps >> fgrep - if you use literal strings >> egrep - if you use extended regexps >> >> Some samples to illustrate... >> >> echo 'xyz' | grep x.z >> echo 'xyz' | fgrep x.z >> >> echo 'hello' | grep 'hello|world' >> echo 'hello' | egrep 'hello|world' >> >> And of course, as already proposed, man grep is your friend. > > Right, and for the OP just be aware of this: > > $ echo 'hello world' | egrep 'hello|world' > hello world > $ echo 'hello world' | awk '/hello/||/world/' > hello world > $ echo 'hello world' | egrep 'hello&world' <- note no output > $ echo 'hello world' | awk '/hello/&&/world/' > hello world See also Kornshell's if [[ $var == @(*hello*&*world*) ]] ; then echo "both any order" ; fi if [[ $var == @(*hello*|*world*) ]] ; then echo "one or more" ; fi Janis > > To simply check for BOTH "hello" and "world" appearing in any order of > the same line, with *grep you need to specify the combination of words > in every possible order: > > $ echo 'hello world' | egrep 'hello.*world|world.*hello' > hello world > > or create a pipline of separate grep commands: > > $ echo 'hello world' | grep 'hello' | grep 'world' > hello world > > whereas with awk you can just AND ("&&") the REs: > > $ echo 'hello world' | gawk '/hello/&&/world/' > hello world > > just like if you wanted to check for either word you could just OR > ("||") the REs: > > $ echo 'hello world' | gawk '/hello/||/world/' > hello world > > Regards, > > Ed.
From: Chris F.A. Johnson on 9 Jun 2010 03:33 On 2010-06-08, kaushal wrote: > Hi, > > can someone explain me about the usage of grep , fgrep and egrep with > examples. > In what contexts i should use this. fgrep and egrep are replaced in the standard by grep -F and grep -E respectively. fgrep uses the search patterns as strings; egrep as exetended regular expressions. -- Chris F.A. Johnson, author <http://shell.cfajohnson.com/> =================================================================== Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress) ===== My code in this post, if any, assumes the POSIX locale ===== ===== and is released under the GNU General Public Licence =====
First
|
Prev
|
Pages: 1 2 Prev: wget -r (recursive) failing to back up a site Next: how to optionally silence lines in script |