FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » simpletest vs phpunit vs ...
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
simpletest vs phpunit vs ... [message #180459] Tue, 19 February 2013 08:05 Go to next message
Simon is currently offline  Simon
Messages: 29
Registered: February 2011
Karma: 0
Junior Member
Hi,

I currently use Simpletest to unit test the back-end of my site, (I
still aim to cover the front-end at some later stage).
I have about 2000 tests and try to write more every day...

But am now wondering if, long term, Simpletest is the right tool for
unit testing my app.

If for not other reason than it has not been updated for some time now,
and, maybe more importantly, there hardly seems to be any support for it
in general.
PHPUnit on the other hand, seems to have a much larger support base,
regular updates/discussions.

Most comparisons I Googled seem to be 2 or 3 years old, nothing really
'current', (for example,
http://stackoverflow.com/questions/34312/simple-test-vs-phpunit).

Shall I bite the bullet now and move to phpunit or will simpletest still
be good enough for a long, long time?

Any comments/suggestions?

Thanks

Simon
Re: simpletest vs phpunit vs ... [message #180472 is a reply to message #180459] Tue, 19 February 2013 13:22 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/19/2013 3:05 AM, Simon wrote:
> Hi,
>
> I currently use Simpletest to unit test the back-end of my site, (I
> still aim to cover the front-end at some later stage).
> I have about 2000 tests and try to write more every day...
>
> But am now wondering if, long term, Simpletest is the right tool for
> unit testing my app.
>
> If for not other reason than it has not been updated for some time now,
> and, maybe more importantly, there hardly seems to be any support for it
> in general.
> PHPUnit on the other hand, seems to have a much larger support base,
> regular updates/discussions.
>
> Most comparisons I Googled seem to be 2 or 3 years old, nothing really
> 'current', (for example,
> http://stackoverflow.com/questions/34312/simple-test-vs-phpunit).
>
> Shall I bite the bullet now and move to phpunit or will simpletest still
> be good enough for a long, long time?
>
> Any comments/suggestions?
>
> Thanks
>
> Simon

I don't use any packages. I write my own tests, based on the design
criteria. It isn't all that hard and can do a more thorough job of testing.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: simpletest vs phpunit vs ... [message #180478 is a reply to message #180472] Thu, 21 February 2013 10:09 Go to previous messageGo to next message
Goran is currently offline  Goran
Messages: 38
Registered: January 2011
Karma: 0
Member
On 19.2.2013 14:22, Jerry Stuckle wrote:
> I don't use any packages. I write my own tests, based on the design
> criteria. It isn't all that hard and can do a more thorough job of
> testing.

Sounds like fun
Re: simpletest vs phpunit vs ... [message #180479 is a reply to message #180478] Thu, 21 February 2013 13:40 Go to previous messageGo to next message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
On 2/21/2013 2:09 AM, Goran wrote:
> On 19.2.2013 14:22, Jerry Stuckle wrote:
>> I don't use any packages. I write my own tests, based on the design
>> criteria. It isn't all that hard and can do a more thorough job of
>> testing.
>
> Sounds like fun
>

If you want fun you go Skiing, if you want something done right, for the
most part, do it yourself. :)
Re: simpletest vs phpunit vs ... [message #180486 is a reply to message #180459] Thu, 21 February 2013 19:27 Go to previous messageGo to next message
legalize+jeeves is currently offline  legalize+jeeves
Messages: 21
Registered: September 2010
Karma: 0
Junior Member
[Please do not mail me a copy of your followup]

Simon <bad(at)example(dot)com> spake the secret code
<aogq26FaaoiU1(at)mid(dot)individual(dot)net> thusly:

> Shall I bite the bullet now and move to phpunit or will simpletest still
> be good enough for a long, long time?

I've been using phpunit. What I find lacking is a good mocking
library, so I end up writing my own state-based fake objects that
derive from my interface. I'd really like to find something that is
as expressive as jMock is for java mock objects, but so far I haven't
found anything that seems to work that way.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
Re: simpletest vs phpunit vs ... [message #180487 is a reply to message #180486] Thu, 21 February 2013 20:08 Go to previous messageGo to next message
Chris Riesbeck is currently offline  Chris Riesbeck
Messages: 3
Registered: September 2011
Karma: 0
Junior Member
On 2/21/2013 1:27 PM, Richard wrote:
> [Please do not mail me a copy of your followup]
>
> Simon <bad(at)example(dot)com> spake the secret code
> <aogq26FaaoiU1(at)mid(dot)individual(dot)net> thusly:
>
>> Shall I bite the bullet now and move to phpunit or will simpletest still
>> be good enough for a long, long time?
>
> I've been using phpunit. What I find lacking is a good mocking
> library, so I end up writing my own state-based fake objects that
> derive from my interface. I'd really like to find something that is
> as expressive as jMock is for java mock objects, but so far I haven't
> found anything that seems to work that way.
>

I'm curious which specific aspects of jMock you most wanted that weren't
in PHPunit's Mock objects

http://www.phpunit.de/manual/3.0/en/mock-objects.html
Re: simpletest vs phpunit vs ... [message #180488 is a reply to message #180487] Thu, 21 February 2013 21:53 Go to previous messageGo to next message
legalize+jeeves is currently offline  legalize+jeeves
Messages: 21
Registered: September 2010
Karma: 0
Junior Member
[Please do not mail me a copy of your followup]

Chris Riesbeck <Chris(dot)Riesbeck(at)gmail(dot)com> spake the secret code
<aond6kFphqiU1(at)mid(dot)individual(dot)net> thusly:

> I'm curious which specific aspects of jMock you most wanted that weren't
> in PHPunit's Mock objects
>
> http://www.phpunit.de/manual/3.0/en/mock-objects.html

When I looked at it, it felt like COBOL. SOOOOOO much typing.

I think this comparison makes my point in more detail:
< http://blog.sznapka.pl/why-mockery-is-better-than-phpunit-mock-builder-and- how-to-integrate-it-with-symfony2/>
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
Re: simpletest vs phpunit vs ... [message #180489 is a reply to message #180488] Thu, 21 February 2013 22:20 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
On 21/02/13 21:53, Richard wrote:

>
> When I looked at it, it felt like COBOL. SOOOOOO much typing.
>
Don't ever take up coding professionally.

The rule of 'more lines of comment than code' holds...




--
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.
Re: simpletest vs phpunit vs ... [message #180491 is a reply to message #180489] Fri, 22 February 2013 00:51 Go to previous messageGo to next message
legalize+jeeves is currently offline  legalize+jeeves
Messages: 21
Registered: September 2010
Karma: 0
Junior Member
[Please do not mail me a copy of your followup]

The Natural Philosopher <tnp(at)invalid(dot)invalid> spake the secret code
<kg66k7$b7j$1(at)news(dot)albasani(dot)net> thusly:

> On 21/02/13 21:53, Richard wrote:
>
>>
>> When I looked at it, it felt like COBOL. SOOOOOO much typing.
>>
> Don't ever take up coding professionally.

I've been paid to write software for 35 years.

> The rule of 'more lines of comment than code' holds...

IMO, unless you are citing sources for complex scientific computation,
or algorithms from a referenced source (i.e. "I took this from Knuth,
Art of Computer Programming, Volume II, pg. 235"), the need for
comments indicates that your code isn't clear enough to begin with.

See <http://c2.com/cgi/wiki?ToNeedComments>
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
Re: simpletest vs phpunit vs ... [message #180492 is a reply to message #180479] Fri, 22 February 2013 08:05 Go to previous messageGo to next message
Goran is currently offline  Goran
Messages: 38
Registered: January 2011
Karma: 0
Member
On 21.2.2013 14:40, Scott Johnson wrote:
> On 2/21/2013 2:09 AM, Goran wrote:
>> On 19.2.2013 14:22, Jerry Stuckle wrote:
>>> I don't use any packages. I write my own tests, based on the design
>>> criteria. It isn't all that hard and can do a more thorough job of
>>> testing.
>>
>> Sounds like fun
>>
>
> If you want fun you go Skiing, if you want something done right, for the
> most part, do it yourself. :)

I just thought we are supposed to build applications, not platforms :)
Foolish me
Re: simpletest vs phpunit vs ... [message #180493 is a reply to message #180491] Fri, 22 February 2013 10:29 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 22.02.2013 01:51, schrieb Richard:
> [Please do not mail me a copy of your followup]
>
> The Natural Philosopher <tnp(at)invalid(dot)invalid> spake the secret code
> <kg66k7$b7j$1(at)news(dot)albasani(dot)net> thusly:
>
>> On 21/02/13 21:53, Richard wrote:
>>
>>>
>>> When I looked at it, it felt like COBOL. SOOOOOO much typing.
>>>
>> Don't ever take up coding professionally.
>
> I've been paid to write software for 35 years.
>
>> The rule of 'more lines of comment than code' holds...
>
> IMO, unless you are citing sources for complex scientific computation,
> or algorithms from a referenced source (i.e. "I took this from Knuth,
> Art of Computer Programming, Volume II, pg. 235"), the need for
> comments indicates that your code isn't clear enough to begin with.
>
> See <http://c2.com/cgi/wiki?ToNeedComments>
>

Haha. Strong words.

You want a cite from Donald Knuth? See literate programming, and WEB, 1981.

/Str.
Re: simpletest vs phpunit vs ... [message #180494 is a reply to message #180491] Fri, 22 February 2013 10:49 Go to previous messageGo to next message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma: 0
Senior Member
X-Received: by 10.180.98.103 with SMTP id eh7mr421709wib.3.1361530169362;
Fri, 22 Feb 2013 02:49:29 -0800 (PST)
MIME-Version: 1.0
Path: textnews.cambrium.nl!feeder3.cambriumusenet.nl!feed.tweaknews.nl!209.85.212 .215.MISMATCH!yu2no1994278wib.0!news-out.google.com!bp2ni51444wib.1!nntp.go ogle.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: Tim Streater <timstreater(at)greenbee(dot)net>
Newsgroups: comp.lang.php
Subject: Re: simpletest vs phpunit vs ...
Date: Fri, 22 Feb 2013 10:49:27 +0000
Organization: Personal Software
Lines: 32
Message-ID: <timstreater-DEEF13(dot)10492722022013(at)news(dot)individual(dot)net>
References: <aogq26FaaoiU1(at)mid(dot)individual(dot)net> <aond6kFphqiU1(at)mid(dot)individual(dot)net> <kg650d$818$1(at)news(dot)xmission(dot)com> <kg66k7$b7j$1(at)news(dot)albasani(dot)net> <kg6ff8$jvi$1(at)news(dot)xmission(dot)com>
X-Trace: individual.net dZ7e/qi+vicGGDy4pQrMcguy1uAmqx2iEmmX56lAO6wm/EXL+JTucL1/rHiOyFg+Qg
X-Orig-Path: timstreater
Cancel-Lock: sha1:WgG/n7SoB0hRfH+b8Avxk80z1w0=
User-Agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X)
X-No-Trace: yes
X-No-Archive: yes
X-Face: "[^yYK92E9|LVD+Av;Fa,giW>Pb"sKU%_`r0#U!ev`>CN2z}$mY(LO@{)-Mo1
)]#\pO1*O.$hdJl67V?e*J6`nb]cCpJ5<<e&qJ+e+,TI|2/F\d`C#*a*9d^?WOq'H.*%
V?'C+Fi^W-l)f"Ru5V("V~M:yU/Y"W3F[O!t|M1+Q&_.LEV>ZRWNth$Kw0j3f8lz:H9T
N5[T'*t:]%pcYSZM
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Xref: textnews.cambrium.nl comp.lang.php:139981

In article <kg6ff8$jvi$1(at)news(dot)xmission(dot)com>,
legalize+jeeves(at)mail(dot)xmission(dot)com (Richard) wrote:

> [Please do not mail me a copy of your followup]
>
> The Natural Philosopher <tnp(at)invalid(dot)invalid> spake the secret code
> <kg66k7$b7j$1(at)news(dot)albasani(dot)net> thusly:
>
>> On 21/02/13 21:53, Richard wrote:
>>
>>>
>>> When I looked at it, it felt like COBOL. SOOOOOO much typing.
>>>
>> Don't ever take up coding professionally.
>
> I've been paid to write software for 35 years.
>
>> The rule of 'more lines of comment than code' holds...
>
> IMO, unless you are citing sources for complex scientific computation,
> or algorithms from a referenced source (i.e. "I took this from Knuth,
> Art of Computer Programming, Volume II, pg. 235"), the need for
> comments indicates that your code isn't clear enough to begin with.

That code might be clear now, but wait a few months. Or get the view of
the poor bugger who is now *maintaining* your code.

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
Re: simpletest vs phpunit vs ... [message #180495 is a reply to message #180491] Fri, 22 February 2013 11:24 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 22.02.2013 01:51, schrieb Richard:
> [Please do not mail me a copy of your followup]
>
> The Natural Philosopher <tnp(at)invalid(dot)invalid> spake the secret code
> <kg66k7$b7j$1(at)news(dot)albasani(dot)net> thusly:
>
>> Don't ever take up coding professionally.
>
> I've been paid to write software for 35 years.
>
>> The rule of 'more lines of comment than code' holds...
>
> IMO, unless you are citing sources for complex scientific computation,
> or algorithms from a referenced source (i.e. "I took this from Knuth,
> Art of Computer Programming, Volume II, pg. 235"), the need for
> comments indicates that your code isn't clear enough to begin with.
>

Well, I generally like strong ideas.

But you should have experienced that especially geniuses need to comment their code,
to make clear to posterity that what others see as obfuscated code, is geniality in
reality.

/Str.
Re: simpletest vs phpunit vs ... [message #180496 is a reply to message #180491] Fri, 22 February 2013 12:06 Go to previous messageGo to next message
Simon is currently offline  Simon
Messages: 29
Registered: February 2011
Karma: 0
Junior Member
On 2013/02/22 12:49 PM, Tim Streater wrote:>>
>> I've been paid to write software for 35 years.

I have only been doing it for +15 years...

>>
>> >The rule of 'more lines of comment than code' holds...
>>
>> IMO, unless you are citing sources for complex scientific computation,
>> or algorithms from a referenced source (i.e. "I took this from Knuth,
>> Art of Computer Programming, Volume II, pg. 235"), the need for
>> comments indicates that your code isn't clear enough to begin with.
>
> That code might be clear now, but wait a few months. Or get the view of
> the poor bugger who is now *maintaining* your code.
>

I tend to take the middle ground approach.
Good function/variable names usually go a long way to make the code clear.

I also tend to limit the length of my functions, the shorter the better.

I add comments as much as possible, but I don't write an essay
explaining why I took the approach I did.

Well written code should speak for itself, even many months later.

Simon
Re: simpletest vs phpunit vs ... [message #180497 is a reply to message #180459] Fri, 22 February 2013 12:15 Go to previous messageGo to next message
Simon is currently offline  Simon
Messages: 29
Registered: February 2011
Karma: 0
Junior Member
On 2013/02/19 10:05 AM, Simon wrote:
> Hi,
>
> I currently use Simpletest to unit test the back-end of my site, (I
> still aim to cover the front-end at some later stage).
> I have about 2000 tests and try to write more every day...
>

As a test<sic>, I installed PHPUnit and migrated the tests, (it turns
out it is fairly straightforward for most cases).

Off the bat, I cannot see any major improvements in the tests, apart
from code coverage.

Code coverage alone is, to me at least, a major plus.

Some of my critical code, that I thought was well covered, only has 37%
coverage.

While I don't want to become militant about code coverage, there are
some functions that I definitely need to re-look at and add some testing
for.

So PHPUnit is a definite win for now.

Simon
Re: simpletest vs phpunit vs ... [message #180498 is a reply to message #180492] Fri, 22 February 2013 13:52 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/22/2013 3:05 AM, Goran wrote:
> On 21.2.2013 14:40, Scott Johnson wrote:
>> On 2/21/2013 2:09 AM, Goran wrote:
>>> On 19.2.2013 14:22, Jerry Stuckle wrote:
>>>> I don't use any packages. I write my own tests, based on the design
>>>> criteria. It isn't all that hard and can do a more thorough job of
>>>> testing.
>>>
>>> Sounds like fun
>>>
>>
>> If you want fun you go Skiing, if you want something done right, for the
>> most part, do it yourself. :)
>
> I just thought we are supposed to build applications, not platforms :)
> Foolish me
>

I do build applications. Tests are not platforms.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: simpletest vs phpunit vs ... [message #180499 is a reply to message #180491] Fri, 22 February 2013 13:57 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/21/2013 7:51 PM, Richard wrote:
> [Please do not mail me a copy of your followup]
>
> The Natural Philosopher <tnp(at)invalid(dot)invalid> spake the secret code
> <kg66k7$b7j$1(at)news(dot)albasani(dot)net> thusly:
>
>> On 21/02/13 21:53, Richard wrote:
>>
>>>
>>> When I looked at it, it felt like COBOL. SOOOOOO much typing.
>>>
>> Don't ever take up coding professionally.
>
> I've been paid to write software for 35 years.
>
>> The rule of 'more lines of comment than code' holds...
>
> IMO, unless you are citing sources for complex scientific computation,
> or algorithms from a referenced source (i.e. "I took this from Knuth,
> Art of Computer Programming, Volume II, pg. 235"), the need for
> comments indicates that your code isn't clear enough to begin with.
>
> See <http://c2.com/cgi/wiki?ToNeedComments>
>

Gee, you mean you've been fooling people for 35 years? I've been doing
it for about 10 years longer. Comments are the MOST IMPORTANT part of
your programming.

Your code says WHAT you are doing. Comments say WHY you are doing it.
Two entirely different things - and the why is more important than the what.

You've obviously never worked on large projects with multiple
programmers, or in a structured programming environment. Both demand
the need for comments - and for good reason.

And the fact you had to find a wiki by some unknown "programmer" on the
internet to "prove" your case shows just how weak it is.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: simpletest vs phpunit vs ... [message #180500 is a reply to message #180498] Fri, 22 February 2013 15:03 Go to previous messageGo to next message
Goran is currently offline  Goran
Messages: 38
Registered: January 2011
Karma: 0
Member
On 22.2.2013 14:52, Jerry Stuckle wrote:
> On 2/22/2013 3:05 AM, Goran wrote:
>> On 21.2.2013 14:40, Scott Johnson wrote:
>>> On 2/21/2013 2:09 AM, Goran wrote:
>>>> On 19.2.2013 14:22, Jerry Stuckle wrote:
>>>> > I don't use any packages. I write my own tests, based on the design
>>>> > criteria. It isn't all that hard and can do a more thorough job of
>>>> > testing.
>>>>
>>>> Sounds like fun
>>>>
>>>
>>> If you want fun you go Skiing, if you want something done right, for the
>>> most part, do it yourself. :)
>>
>> I just thought we are supposed to build applications, not platforms :)
>> Foolish me
>>
>
> I do build applications. Tests are not platforms.

True, but platform for running tests is... well, platform
Re: simpletest vs phpunit vs ... [message #180501 is a reply to message #180500] Fri, 22 February 2013 15:14 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/22/2013 10:03 AM, Goran wrote:
> On 22.2.2013 14:52, Jerry Stuckle wrote:
>> On 2/22/2013 3:05 AM, Goran wrote:
>>> On 21.2.2013 14:40, Scott Johnson wrote:
>>>> On 2/21/2013 2:09 AM, Goran wrote:
>>>> > On 19.2.2013 14:22, Jerry Stuckle wrote:
>>>> >> I don't use any packages. I write my own tests, based on the design
>>>> >> criteria. It isn't all that hard and can do a more thorough job of
>>>> >> testing.
>>>> >
>>>> > Sounds like fun
>>>> >
>>>>
>>>> If you want fun you go Skiing, if you want something done right, for the
>>>> most part, do it yourself. :)
>>>
>>> I just thought we are supposed to build applications, not platforms :)
>>> Foolish me
>>>
>>
>> I do build applications. Tests are not platforms.
>
> True, but platform for running tests is... well, platform
>

Testing is best done by someone other than the programmer(s) writing
tests according to the documentation. That way you can thoroughly test
what the code is supposed to do according to the design.

In such a case, not only do you not need a platform - a platform will
generally get in the way of performing the necessary tests.

Some people think they need a platform for everything. That isn't true.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: simpletest vs phpunit vs ... [message #180502 is a reply to message #180499] Fri, 22 February 2013 15:55 Go to previous messageGo to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 2/22/2013 8:57 AM, Jerry Stuckle wrote:...
>
> Gee, you mean you've been fooling people for 35 years? I've been
> doing it for about 10 years longer. Comments are the MOST
> IMPORTANT part of your programming.
>
> Your code says WHAT you are doing. Comments say WHY you are
> doing it.

Actually comments say what you THINK you are doing and why.

Two entirely different things - and the why is more
> important than the what.

Assuming I do not die unexpectedly, I am the poor programmer that
has to maintain code I wrote 1-10 years ago. A well commented
script is a lifesaver.

bill
Re: simpletest vs phpunit vs ... [message #180504 is a reply to message #180493] Fri, 22 February 2013 20:21 Go to previous messageGo to next message
legalize+jeeves is currently offline  legalize+jeeves
Messages: 21
Registered: September 2010
Karma: 0
Junior Member
[Please do not mail me a copy of your followup]

"M. Strobel" <sorry_no_mail_here(at)nowhere(dot)dee> spake the secret code
<aoovl2F5f8gU1(at)mid(dot)uni-berlin(dot)de> thusly:

>>> The rule of 'more lines of comment than code' holds...
>>
>> IMO, unless you are citing sources for complex scientific computation,
>> or algorithms from a referenced source (i.e. "I took this from Knuth,
>> Art of Computer Programming, Volume II, pg. 235"), the need for
>> comments indicates that your code isn't clear enough to begin with.
>>
>> See <http://c2.com/cgi/wiki?ToNeedComments>
>>
>
> Haha. Strong words.

See also Refowler, "Refactoring" where comments as a code smell are
discussed.

The point of mentioning Knuth was simply to use AoCP as a source of
algorithms one might use in a bibliographic style citation comment.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
Re: simpletest vs phpunit vs ... [message #180505 is a reply to message #180494] Fri, 22 February 2013 20:23 Go to previous messageGo to next message
legalize+jeeves is currently offline  legalize+jeeves
Messages: 21
Registered: September 2010
Karma: 0
Junior Member
[Please do not mail me a copy of your followup]

Tim Streater <timstreater(at)greenbee(dot)net> spake the secret code
<timstreater-DEEF13(dot)10492722022013(at)news(dot)individual(dot)net> thusly:

> In article <kg6ff8$jvi$1(at)news(dot)xmission(dot)com>,
> legalize+jeeves(at)mail(dot)xmission(dot)com (Richard) wrote:

>> IMO, unless you are citing sources for complex scientific computation,
>> or algorithms from a referenced source (i.e. "I took this from Knuth,
>> Art of Computer Programming, Volume II, pg. 235"), the need for
>> comments indicates that your code isn't clear enough to begin with.
>
> That code might be clear now, but wait a few months. Or get the view of
> the poor bugger who is now *maintaining* your code.

In my decades of experience looking at other people's code, code that
is aggressively refactored and has well chosen identifiers -- in other
words the kind of code that I and others are saying doesn't need
explanatory comments -- is much, much more maintainable years or
decades later than code festooned with explanatory comments.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
Re: simpletest vs phpunit vs ... [message #180506 is a reply to message #180495] Fri, 22 February 2013 20:26 Go to previous messageGo to next message
legalize+jeeves is currently offline  legalize+jeeves
Messages: 21
Registered: September 2010
Karma: 0
Junior Member
[Please do not mail me a copy of your followup]

"M. Strobel" <sorry_no_mail_here(at)nowhere(dot)dee> spake the secret code
<aop2rdF66hsU1(at)mid(dot)uni-berlin(dot)de> thusly:

> Am 22.02.2013 01:51, schrieb Richard:
>> IMO, unless you are citing sources for complex scientific computation,
>> or algorithms from a referenced source (i.e. "I took this from Knuth,
>> Art of Computer Programming, Volume II, pg. 235"), the need for
>> comments indicates that your code isn't clear enough to begin with.
>
> Well, I generally like strong ideas.
>
> But you should have experienced that especially geniuses need to comment
> their code,
> to make clear to posterity that what others see as obfuscated code, is
> geniality in
> reality.

The kind of code I'm saying doesn't require explanatory comments in
order to be understood is *not* the kind of "genius code" to which you
refer above. "Genius" code tends to be long complicated methods, is
poorly factored, has no unit tests, and generally has poorly named
identifiers. I've had more than one occasion where I had to contend
with "genius" code. It's generally of poor quality, very difficult to
adapt to changing needs in the future, has high cyclomatic complexity
and should live only in a proof-of-concept prototype and be adapted to
production use by someone who actually understands software
engineering.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
Re: simpletest vs phpunit vs ... [message #180507 is a reply to message #180499] Fri, 22 February 2013 20:27 Go to previous messageGo to next message
legalize+jeeves is currently offline  legalize+jeeves
Messages: 21
Registered: September 2010
Karma: 0
Junior Member
[Please do not mail me a copy of your followup]

Jerry Stuckle <jstucklex(at)attglobal(dot)net> spake the secret code
<kg7te0$ces$1(at)dont-email(dot)me> thusly:

> Gee, you mean you've been fooling people for 35 years? [...]

....and here's where I stopped listening.

If you want me to take you seriously, it's best to *not* open your
comment with an ad-hominem attack.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
Re: simpletest vs phpunit vs ... [message #180508 is a reply to message #180505] Fri, 22 February 2013 20:41 Go to previous messageGo to next message
Anders Wegge Keller is currently offline  Anders Wegge Keller
Messages: 30
Registered: May 2012
Karma: 0
Member
legalize+jeeves(at)mail(dot)xmission(dot)com (Richard) writes:

> In my decades of experience looking at other people's code, code that
> is aggressively refactored and has well chosen identifiers -- in other
> words the kind of code that I and others are saying doesn't need
> explanatory comments -- is much, much more maintainable years or
> decades later than code festooned with explanatory comments.

You are right. And you're wrong.

I guess that your primary work is on a _product_ that is debugged,
refined, relased over and over again, but never exist in more than one
incarnation. If that is so, you are probably right.

If on the other hand, you had been working in a project organization,
like I do, you would have known that there isn't enough time to
refactor all 600.000 lines of code[1], for each of the 600 projects
that has 92-96% of the code in common. What you'd do, had you worked
at my job, would be commenting the changes to the routing code
algorithm. And depending on the particulars, it could be so simple as
to explain that characters 4 to 17 of the barcode scanned is looked up
in a table that tells the destination, or refering to a 17 page
flowchart[2], that explains the neccesity of all the backtracking
that's done.

My claim is that what works for you, doesn't neccesarily work for
everyone else.


1. C in this case. But that's probably not changing the merits of my
observations.

2.I'm not making this up. There's one postal order company in Germany,
that sorts parcel according to the individual rules of 13 different
european countries, an with barcode labels having a rather limited
amount of expression, wrt. length and typology, this leads to
insanity.

--
/Wegge

Leder efter redundant peering af dk.*,linux.debian.*
Re: simpletest vs phpunit vs ... [message #180510 is a reply to message #180507] Fri, 22 February 2013 21:57 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/22/2013 3:27 PM, Richard wrote:
> [Please do not mail me a copy of your followup]
>
> Jerry Stuckle <jstucklex(at)attglobal(dot)net> spake the secret code
> <kg7te0$ces$1(at)dont-email(dot)me> thusly:
>
>> Gee, you mean you've been fooling people for 35 years? [...]
>
> ...and here's where I stopped listening.
>
> If you want me to take you seriously, it's best to *not* open your
> comment with an ad-hominem attack.
>

You can't read, anyway.

I've got a few more (about 12) years of experience than you do. I've
worked on small, one-person projects and large (100+ person) projects,
and about anything in between.

I have NEVER found code - no matter who it's written by, that couldn't
be improved by well designed comments. I have, however, found a number
of programmers who were too lazy to comment their code and said the code
didn't need commenting. Without exception, NONE of them wrote decent code.

If you can hand your code to a newbie and have that person explain what
a 100+ loc function does and how it does it in five minutes or less,
then you don't need to comment your code. Anything else and you need
comments.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: simpletest vs phpunit vs ... [message #180514 is a reply to message #180508] Sat, 23 February 2013 00:39 Go to previous messageGo to next message
legalize+jeeves is currently offline  legalize+jeeves
Messages: 21
Registered: September 2010
Karma: 0
Junior Member
[Please do not mail me a copy of your followup]

Anders Wegge Keller <wegge(at)wegge(dot)dk> spake the secret code
<87ehg8ozce(dot)fsf(at)huddi(dot)jernurt(dot)dk> thusly:

> If on the other hand, you had been working in a project organization,
> like I do, you would have known that there isn't enough time to
> refactor all 600.000 lines of code[1], for each of the 600 projects
> that has 92-96% of the code in common.

This is a straw-man argument, because I didn't assert any such thing.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
Re: simpletest vs phpunit vs ... [message #180515 is a reply to message #180514] Sat, 23 February 2013 00:51 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/22/2013 7:39 PM, Richard wrote:
> [Please do not mail me a copy of your followup]
>
> Anders Wegge Keller <wegge(at)wegge(dot)dk> spake the secret code
> <87ehg8ozce(dot)fsf(at)huddi(dot)jernurt(dot)dk> thusly:
>
>> If on the other hand, you had been working in a project organization,
>> like I do, you would have known that there isn't enough time to
>> refactor all 600.000 lines of code[1], for each of the 600 projects
>> that has 92-96% of the code in common.
>
> This is a straw-man argument, because I didn't assert any such thing.
>

It's a straw-man argument to you, because you refuse to admit he's right.

It isn't to anyone who knows what real coding is all about.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: simpletest vs phpunit vs ... [message #180520 is a reply to message #180514] Sat, 23 February 2013 06:51 Go to previous messageGo to next message
Anders Wegge Keller is currently offline  Anders Wegge Keller
Messages: 30
Registered: May 2012
Karma: 0
Member
legalize+jeeves(at)mail(dot)xmission(dot)com (Richard) writes:

> [Please do not mail me a copy of your followup]
>
> Anders Wegge Keller <wegge(at)wegge(dot)dk> spake the secret code
> <87ehg8ozce(dot)fsf(at)huddi(dot)jernurt(dot)dk> thusly:
>
>> If on the other hand, you had been working in a project organization,
>> like I do, you would have known that there isn't enough time to
>> refactor all 600.000 lines of code[1], for each of the 600 projects
>> that has 92-96% of the code in common.
>
> This is a straw-man argument, because I didn't assert any such thing.

Didn't you say that refactoring could eliminate the need for
comments?

But that aside, I only did mention this example, because this is the
reality I have to work with. And although I'm in favor of writing easy
understood code, I also know that it becomes impractical at codebases
this large.


--
/Wegge

Leder efter redundant peering af dk.*,linux.debian.*
Re: simpletest vs phpunit vs ... [message #180521 is a reply to message #180459] Sat, 23 February 2013 07:38 Go to previous messageGo to next message
Martin Musatov is currently offline  Martin Musatov
Messages: 9
Registered: January 2013
Karma: 0
Junior Member
On Tuesday, February 19, 2013 12:05:25 AM UTC-8, Simon wrote:
> Hi,
>
>
>
> I currently use Simpletest to unit test the back-end of my site, (I
>
> still aim to cover the front-end at some later stage).
>
> I have about 2000 tests and try to write more every day...
>
>
>
> But am now wondering if, long term, Simpletest is the right tool for
>
> unit testing my app.
>
>
>
> If for not other reason than it has not been updated for some time now,
>
> and, maybe more importantly, there hardly seems to be any support for it
>
> in general.
>
> PHPUnit on the other hand, seems to have a much larger support base,
>
> regular updates/discussions.
>
>
>
> Most comparisons I Googled seem to be 2 or 3 years old, nothing really
>
> 'current', (for example,
>
> http://stackoverflow.com/questions/34312/simple-test-vs-phpunit).
>
>
>
> Shall I bite the bullet now and move to phpunit or will simpletest still
>
> be good enough for a long, long time?
>
>
>
> Any comments/suggestions?
>
>
>
> Thanks
>
>
>
> Simon

Main menu
Blog
About
Forum
RSS/FEED

Linux FAQ / Howtos
HowTo: Use grep Command In Linux / UNIX [ Examples ]

by nixCraft on August 2, 2007 · 231 comments· last updated at December 11, 2011

How do I use grep command in Linux and Unix like operating systems? Can you give me a simple example of grep command?

The grep command searches the given file for lines containing a match to the given strings or words. By default, grep displays the matching lines. Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines.

The name, "grep", derives from the command used to perform a similar operation, using the Unix/Linux text editor ed:
g/re/p
grep Command Syntax

grep 'word' filename
grep 'string1 string2' filename
cat otherfile | grep 'something'
command | grep 'something'
command option1 | grep 'data'
grep --color 'data' fileName

How Do I Use grep To Search File?

Search /etc/passwd for boo user:
$ grep boo /etc/passwd

You can force grep to ignore word case i.e match boo, Boo, BOO and all other combination with -i option:
$ grep -i "boo" /etc/passwd
Use grep recursively

You can search recursively i.e. read all files under each directory for a string "192.168.1.5"
$ grep -r "192.168.1.5" /etc/
Use grep to search words only

When you search for boo, grep will match fooboo, boo123, etc. You can force grep to select only those lines containing matches that form whole words i..e. match only boo word:
$ grep -w "boo" /path/to/file
Use grep to search 2 different words

use egrep as follows:
$ egrep -w 'word1|word2' /path/to/file
Count line when words has been matched

grep can report the number of times that the pattern has been matched for each file using -c (count) option:
$ grep -c 'word' /path/to/file
Also note that you can use -n option, which causes grep to precede each line of output with the number of the line in the text file from which it was obtained:
$ grep -n 'word' /path/to/file
Grep invert match

You can use -v option to print inverts the match; that is, it matches only those lines that do not contain the given word. For example print all line that do not contain the word bar:
$ grep -v bar /path/to/file
UNIX / Linux pipes and grep command

grep command often used with pipes. For example print name of hard disk devices:
# dmesg | egrep '(s|h)d[a-z]'
Display cpu model name:
# cat /proc/cpuinfo | grep -i 'Model'
However, above command can be also used as follows without shell pipe:
# grep -i 'Model' /proc/cpuinfo
How do I list just the names of matching files?

Use the -l option to list file name whose contents mention main():
$ grep -l 'main' *.c
Finally, you can force grep to display output in colors:
$ grep --color vivek /etc/passwd
Sample outputs:
Grep command in action

Grep command in action

If you enjoyed the grep tutorial, then you might like to read our "Regular Expressions in Grep" tutorial.

submit to reddit
Download this as pdf



You should follow me on twitter here or grab rss feed to keep track of new changes.
This FAQ entry is 1 of 7 in the "Linux / UNIX grep Command Tutorial" series.. Keep reading the rest of the series:
Table of Contents:

How To Use grep Command In Linux / UNIX
Regular Expressions In grep
Search Multiple Words / String Pattern Using grep Command
Grep Count Lines If a String / Word Matches
Grep From Files and Display the File Name
How To Find Files by Content Under UNIX
grep command: View Only Configuration File Directives

{ 231 comments… read them below or add one }

1 Ivan June 16, 2008 at 11:22 pm

I found this tutorial being the most clear and helpful one. Thank you.

Reply

2 Sandeep October 18, 2010 at 6:09 am

for searching multiple string using GREP command, please use below commands

grep -ir ‘string1\|string2′

Reply

3 Jeyaram May 10, 2011 at 7:05 am

above command is not working, instead

grep -ir “string1\|string2″ dir_name

Reply

4 manik sikka November 25, 2008 at 12:26 am

This Tutorial is so far helpful to me.

Thank you..!!

Reply
5 turbolinux December 12, 2008 at 11:46 am

what if i want to search keyword
like “$this->Products” or “['status']” ?

grep -R "['status']"

doesnt work

Reply
6 Vivek Gite December 12, 2008 at 11:53 am

Try

grep -R "\['status'\]" filename

Reply
7 Arunkumar December 26, 2008 at 5:32 am

this is very useful to me.. thanks …

Reply
8 Vikram January 30, 2009 at 11:39 am

Very helpful. saved my time.

Reply
9 Domino March 18, 2009 at 9:40 pm

What is the best way to grep recursively through a directory tree, and display the pattern matches, that occur in just all the *.cpp files?

For example:

grep -HRn “look for this” *.cpp

doesn’t work (on Linux)

Reply

10 nRon May 16, 2011 at 12:32 pm

You could try

grep -r –include=”*.cpp” ‘look for this’ .

(“.” is current directory)

Reply
11 durga reddy August 12, 2011 at 1:02 pm

find . -name “*.cc” |args grep -n “pattern”

Reply

12 durga reddy August 12, 2011 at 1:03 pm

small correction
find . -name “*.cc” |xargs grep -n “pattern”

Reply

13 eswar March 23, 2009 at 9:02 am

what if i want to search like this

Reply
14 Tauqueer March 25, 2009 at 8:13 am

Hi
I want to search t1.spq, t2.spq ….. tn.spq words from a file
grep -i “*.spq” filename doesn’t work
Please tell how can search such words??

-Regards,
tauqueer

Reply

15 Mohanraj October 24, 2011 at 3:04 pm

Hi,

you can try out the below command..

grep -i ^t..spq filename

Best regards,
Mohanraj

Reply

16 eswar March 25, 2009 at 9:45 am

avoid using * grep -i “spq” tt.sh
then you will get all the words which will have spq.
if some thing need to refrained from that need to get the desired out put then use

grep -i “spq” tt.sh | grep -v ” somepattern”

Reply
17 Humphrey April 25, 2009 at 9:03 am

How can i grep for an output that come after a statement: eg Expires: 10/May/2009.

If i want to caputure only the date, how can i grep for what comes after the colon (:)

please help

Reply

18 Vivek Gite April 25, 2009 at 10:21 am

Try,

echo 'Expires: 10/May/2009' | cut -d: -f2
OR
echo 'Expires: 10/May/2009' | awk -F':' '{ print $2}'

Reply

19 Rizvana April 29, 2009 at 6:01 pm

This is quite informative…… Thanks.

Also I have a question, what is the expansion of ‘grep’? Can anyone answer?

Reply
20 Ravi April 30, 2009 at 7:30 am

This helps a lot,,

Reply
21 Humphrey May 2, 2009 at 12:11 am

Rizvana,
grep means Get Regular Expression and Print

Reply
22 Humphrey May 2, 2009 at 8:44 pm

How can i do calculation on dates;
eg to know the number of days between ‘todays date’ and a day like ’15/may/2009′

please help

Reply
23 divya May 4, 2009 at 2:24 pm

thanks .i gt the rite information.

Reply
24 Blury May 11, 2009 at 3:34 am

Hi,
How can I use grep function to search files that file name with “ord” or “rec” from specific dir??

Reply

25 Sandeep August 2, 2010 at 10:19 am

grep -ir “ord\|rec” filename

Reply

26 kandida June 2, 2009 at 7:04 am

Hi
lets say i have some data :
a
a
a
b
b
b
c
c
c
can I use grep comand to make like this :
a
b
c
thanks

Reply

27 Vivek Gite June 2, 2009 at 8:04 am

Try uniq command.

Reply
28 Sandeep August 2, 2010 at 10:20 am

grep -ir “a\|b\|c” filename

Reply

29 kandida June 2, 2009 at 9:52 am

Vivek,
its working…..thanks a lot

Reply
30 Blury June 3, 2009 at 12:51 am

Hi,
No one know how to use grep function to search files that file name with “ord” or “rec” from specific dir??

Reply
31 Vivek Gite June 3, 2009 at 1:03 am

Try

cd /dir/to/search
grep "word" ord*
grep -R "word" rec*

Reply
32 snake June 10, 2009 at 3:46 pm

Hi,
I’d like to get the total cpu and memory usage easily and I think of using ‘dstat’ command. Can I get the values corresponding to the free and used column with grep?

------memory-usage-----
used buff cach free
153M 876k 24M 4392k

cheers!

Reply
33 Nazeem S June 22, 2009 at 10:34 am

The Most Helpful POST

Reply
34 Thomas K June 26, 2009 at 1:36 pm

For those who want to search files with wild cards and the like, try the find command with -exec. find /dir/to/search/ -iname *.cpp -exec grep 'word' '{}' \;
and snake, I do not think it is possible to search columns with grep, I’m 98% sure that it is line (row) only.

Reply
35 sankar July 10, 2009 at 6:45 am

As i was i beginner , it helped me a lot . I would like to thank all the people who contributed it to the public … than you so much

Reply
36 manavi July 25, 2009 at 4:01 am

whats the use of egrep and fgrep
give examples of both

Reply
37 Vivek Gite July 25, 2009 at 6:22 am

egrep is for regex and fgrep is for fixed string. I will update FAQ with more examples.

Reply
38 SANA August 2, 2009 at 4:35 am

this is the best and the most understandable tutorial i have seen till date.
GOOD WORK!!

Reply
39 Patrick August 7, 2009 at 10:43 am

This tutorial is very easy to follow and enabled me to learn so much within a very short time

Reply
40 prathamesh August 7, 2009 at 10:52 am

hi this is really really helpful and very fast introduction for grep very nice

Reply
41 harshal August 8, 2009 at 10:27 am

exact and accurate content with no irrelevent text..

Reply
42 Rob August 11, 2009 at 7:19 am

clean and accurate .thanks

Reply
43 Syed August 16, 2009 at 1:40 pm

Can someone help me for a data like:

aa:abc
bb:def
cc:ghi
dd:ijk

aa:lmn
bb:opq
cc:
dd:uvw

aa:pqr
bb:stu
cc:vwx
dd:yza

Description of data:
aa, bb, cc, dd comprise one record. Blank line is dividing 100 of such records.
question1) How to grep “cc:” that is empty value? i am unable to do this because it gives all the values.
question2) I need to print value of “aa:…” for all the records whose “dd:uvw”. How to do this?

Reply
44 jyothi September 17, 2009 at 12:35 pm

hi all..
can anyone please explain me about this command
ps -ef|grep -v grep|grep $orasid >>/dev/null

Reply
45 learner September 22, 2009 at 11:39 pm

ok..i feel really dumb..but i got this task i have to do to find a hidden file or something like that and im pretty sure i would use the command ls or whatever.
my problem is i dont no what im doing at all here..were do i type the command in at? all these things jus teach u the commands. yea im dumb i dont have a clue were to type it in..
if someone could please help me out here it be much appreciated! thanx

Reply

46 Ismael August 5, 2011 at 9:08 pm

You can get more information in here: linuxcommand.org

Reply

47 jyothi September 23, 2009 at 12:56 pm

hii..
It goes something like this..
i want to list a oracle database instance (say orasid=tiger) using this command..
I issued each command in separate and could understand little bit..
1. ps -ef :: lists all running processes
2. the output is directed to “grep -v grep” now
3. what that command does is just filter out any text containing ‘grep’..
4. this output is now sent to “grep $orasid”
5. it will just fetch only those running process like ” tiger ”
6. the output is redirected to /dev/null where the output is just discarded..
after this command here they used
if[ $? -ne 0]
then ————–
else —————–
fi
where $? reads output of previous command…
here $? is 0 as the command hasn’t thrown any error

Reply
48 AKHIL September 23, 2009 at 5:03 pm

VERY HELPFUL

Reply
49 Samer September 27, 2009 at 6:49 am

Dear All ,
Thnaks for this nice chart it helps me a lot .
1- If i need to search for a “word” inside a directory that holds files, these files are located in the “www”
2- Also to search for a “word” in the all the databases in : /var/lib/mysql/database

Thanx all

Reply
50 ed September 27, 2009 at 11:48 pm

What is the grep command to find a string of charactes like below

KY 41099-1000

I am just using KY as an example but I want the command to list any state abbreviations followed by a 9 digit zip code. Any help is appreciated

Reply
51 nitin September 30, 2009 at 5:06 pm

@ed
use
# grep ^[A-Z][A-Z] [0-9] [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$ filename

i hope it work………

Reply
52 faiz_zaidy October 3, 2009 at 11:52 am

please solve my query
my query is “how can i search the text in hole file of a particular dirctory”

Reply
53 Sam October 18, 2009 at 7:46 pm

Hi i wud like an explanation of the ff command
grep[a-g]…[r-z]text.txt

Reply
54 Mukesh October 25, 2009 at 1:44 am

This Tutorial is very helpful to me.

Thanks….!!

Reply
55 dev November 11, 2009 at 4:26 am

@ Syed:

** answer1 **
cat file | grep '^cc:$'
will produce only cc: with no value.
Learn regular expressions. In the command above regular expression is between ”. So… cat is normal command that prints file. Then result is piped (|) to grep, which processes it with regular expression (^cc:$).
This regex basically matches lines, which contain:
^ – beginning of the line
c – letter c
c – letter c
: – colon
$ – end of line
If there is something between colon (:) and end of line ($), in your case some value, then line is not produced by grep.

** answer2 **
How to check value of aa: in the record which contain dd:uvw?
cat file | grep -B3 'dd:uvw'
-B3 tells grep to show line that matches our pattern and 3 lines Before (you can change this number if you want, you can use -A4 to show 4 lines After).
So the command above will produce whole 4 line of each record, which contain dd:uvw. To show only lines with aa: and their values, we can simply add subsequent grep:
cat file | grep -B3 'dd:uvw' | grep 'aa'
…and that’s it.

Reply
56 dev November 11, 2009 at 4:37 am

@Sam
grep [a-g]…[r-z]text.txt
will match files which names start with a,b,c,d,e,f or g, then there are subsequent three characters – each dot (.) represent any character – then there is r,s,t,u,v,w,x,y or z and then text.txt.
So it can be:
bokkrtext.txt
aiiiztext.txt
and so on…

Reply
57 dev November 11, 2009 at 5:14 am

@ jyothi
ps -ef|grep -v grep|grep $orasid » /dev/null

Explanation:
ps -ef lists processes. On that list there is number of every process, even number of grep process executed in this line. For example if i try to grep non-existing process john by issuing command ps -ef | grep john, then i will get this:
dev 6271 5933 0 05:43 pts/0 00:00:00 grep john
Even if there is no process I am looking for, I will get result from grep. I don’t want that to happen, so I will have to grep invert match (grep -v pattern as explained in the howto above).
In other words ps -ef | grep -v grep will list processes excluding grep..

After next pipe you have:
grep $orasid
$string recalls value of variable defined before.
If you define $orasid=bob, then your command will be equal to:
ps -ef | grep -v grep | grep bob » /dev/null
If $orasid=william, then your command will be equal to:
ps -ef | grep -v grep | grep william » /dev/null
and so on…

» /dev/null redirects output of the command to nowhere, because we don’t want to see grep output on our screen, when executing the code.

Summarizing:

$orasid=tiger
...
ps -ef | grep -v grep | grep $orasid >> /dev/null
...
if [ $? -ne 0]
then echo 'claws'
else echo 'no claws'
fi

If there is process named tiger (output of grep is not empty), then we get claws, else we get no claws :)

Reply
58 marlenchen November 16, 2009 at 7:45 pm

How can i search users, who can sign in in the system, with the grep commando?

Reply
59 swarna December 1, 2009 at 8:12 pm

very useful to me..

Reply
60 parveen dabas December 3, 2009 at 6:38 am

i found this tutorial really helpful…
well i hv one doubt…
if i have two files, namely..
cat >file1
1
2
3
4
cat >file2
1
2
Now wat command and iptions to use if i want only 3 and 4 as output i.e..
only those lines which are not present in file2 wen compaed to file1… plz help….

Reply
61 krishna December 3, 2009 at 1:17 pm

Simply super ,,,thanks

Reply
62 Shan December 14, 2009 at 5:07 pm

This is great article , i am using the windows Grep editor, I am search the text content \”*hello*\” to get the world hello in all the files. I didnt get any output . Could suggest me what i should use.

Reply
63 parveen dabas December 15, 2009 at 9:44 am

Reply to Shan
wel u can use
grep -iw hello filename
if two words ex. Hello and
Hi, use foll syntax
egrep -iw ‘hello|Hi filename

Reply
64 Ram January 12, 2010 at 9:57 pm

Please do explain, what do the values between colons represent? For example:

grep video /etc/groups

video:x:33:rtalkad

Reply

65 Vivek Gite January 13, 2010 at 4:25 am

See /etc/group file format.

Reply

66 hammy January 25, 2010 at 10:21 am

How to count total number of lines using shell script..????

Reply
67 Runal January 28, 2010 at 10:44 am

Hi
I have an outstanding issue with me…
i know the specific pattern in the file but dont know the name of the file/script and dont know the location either.
How to search the filename with simply the pattern in that file.

Actually at some xyz location a .dmp file gets created (xyz.dmp (everyday it creates the same file as it a daily backup.))…now i dont know which script creates this .dmp file…So i need to know the name of the script.

Reply
68 Marco January 30, 2010 at 3:33 am

Well, I didn’t found an answer, maybe you can help me.

I look for something like:

grep “[string a] AND [string b]” (print all lines who include [string a] and [string b])
grep “[string a] OR [string b]” (print all lines who include [string a] or [string b])
grep “NOT [string a]” (print all lines except [string a])

Reply
69 Salman January 30, 2010 at 10:59 pm

@Hammy

In order to count “words” , “characters” and “lines” in a file there is a bash command called “wc”.

example:

wc -l < filename
or
cat filename | wc -l
will count number of lines in file named "filename"

wc -w < somefile
cat somefile | wc -w
will count number of words or strings in the file named "somefile"

wc -c < somefile
cat somefile | wc -c
will count number of characters in the file named "somefile"

cheers!!

Reply
70 esakki February 11, 2010 at 5:50 am

How can i found system ip with mac address using grep command, pls tell anyone.

Reply
71 my11 February 22, 2010 at 8:19 pm

how do u run a grep cmd to exclude multiple lines from a file and write remaining lines to a file?

Reply
72 naveen.divi February 25, 2010 at 7:58 am

this is tutorial is very helpful for me.

thanks a lot.

Reply
73 linu February 25, 2010 at 9:20 am

REALLY GREAT BLOG!!1

Reply
74 novice March 3, 2010 at 6:29 am

hi, I am trying to to something like this…

filename_$CLIENT_NUMBER_sapphire_$DATE.var
filename_0_sapphire_20102002.var
filename_1_sapphire_20102002.var

filename_22_sapphire_20102002.var
filename_23_sapphire_20102002.var

so i wanna get a list of all these vars that are set for a certain day for all the client_number(0 to 24)… in an ordered fashion.

set | grep 20102002 | grep filename_ — gives me the 0 file and then 10, 11… and then the 1, 2 (filename_0_sapphire_20102002.var)

i have tried sort and got no results. Please can u help.

did this too:
set | grep 20102002 | grep filename_ ‘[0-9]‘ — and get only 0 to 9 vars… how do i get 0 to 23 in order.

Thanks!

Reply
75 dillip kumar jena March 12, 2010 at 10:53 am

really it is very help ful to me.

Reply
76 SNW March 16, 2010 at 9:40 pm

How you go about listing user and last day of access ?

Reply
77 Rutvik March 31, 2010 at 7:34 pm

Hey,
I have a question. I have two files and I want to compare the files in such a way that it takes the data that is in file 1 and searches in file 2 and returns me items it did not find. (file 2 has more/newer data). I tried this command but it doesn’t look like it is working.

grep -v -f Currentsttuf.txt newstuff.txt> difference.txt

Reply
78 Ankur April 2, 2010 at 8:51 am

Very clearly explained… thanks!

Reply
79 vikas kumar May 7, 2010 at 6:13 am

very good ………………
please send me useful shell programing in my mail..
vikas kumar

Reply
80 Pam July 8, 2010 at 7:11 pm

my script:

for f in ‘cat all_logs0708′;
do
echo $f
grep ‘ZYXzyx’ $f >> all_out
done

I’d like to have the $f value prefixed on each output line. Is this possible?

Reply
81 karthik July 9, 2010 at 12:54 pm

How to find the files accessed in last 3 days using a grep command.

Reply
82 Catherine July 12, 2010 at 11:55 pm

what if i want to exclude a certain word, for instance ‘prism’
This what I have so far:
grep -i ‘ism$’ /usr/dict/words

i want it to list every word that ends in ‘ism’ but would like to exclude ‘prism’
i only want to use one grep.

Reply
83 Alima July 14, 2010 at 8:35 pm

I have 2 files. File1 contains a list of long-strings with their corresponding informations. File2 contains portions of the strings (short-strings) found in file 1. I would like to find which “short-strings” within file2 is present in file1 and extract the corresponding information. The ideal output file should be short-strings + long-string + corresponding information. Please help… Thanks in advance

Reply
84 krasaviza July 28, 2010 at 7:42 am

Hi xD
I have a problem with grep -v. I want to exclude a line and the 6 lines before it, so I tried
|grep -v -B6 ‘my text’ but that doesn’t work, only without the -B6.
But how else can i exclude the other lines in quite a simple way because i need this option many times :S ?
Please help me

Krasaviza

Reply
85 Bhavin Vyas August 18, 2010 at 7:50 am

I have a data like
:
** SPFS310 LED is on
LED is on: MB . See prtdiag -v and /var/adm/messages for more details.
cbm850=TCS-CBM2;NODE=TCS-CBM2-unit0;CLASS=HW;HWTYPE=SYSBOARD
Mon Aug 9 11:05:50 2010

* APCL303 Trouble condition asserted.
Communication with the Core is not established
cbm850=TCS-CBM2;NODE=TCS-CBM2-unit0;CLASS=APPL;APPLTYPE=SDM_BASE.logs:s
tart_sdmlaq
Tue Aug 10 04:38:30 2010

I want to display the paragraph which contains ** i.e my output should be
** SPFS310 LED is on
LED is on: MB . See prtdiag -v and /var/adm/messages for more details.
cbm850=TCS-CBM2;NODE=TCS-CBM2-unit0;CLASS=HW;HWTYPE=SYSBOARD
Mon Aug 9 11:05:50 2010

What command should I use.Can anyone please tell me.

Reply
86 rahul August 29, 2010 at 2:10 am

How me difference between egrep and fgrep and how to use these commands

Reply
87 sujith September 8, 2010 at 12:58 pm

Try the below shell programs.

1. Locate build.number in the input file. Get the value and increment the value to next number for every run.
2. Have a some parm=value in input file. From shell read these and use it in the shell, pass it to another shell.
Take two file that have parm=value. From source file copy all the parms that exists in another file (target) with the values retrieved from the source file

Reply
88 Cletus September 10, 2010 at 5:32 pm

I hope this doesn’t sound silly. How do I eliminate a directory from a grep search? Let’s say I want to look for the word “hamburger” that exists somewhere is a file in a subdirectory. I want to recursive search but I want to eliminate one of the subdirectories from being searched. To search I would normally do something like #grep -r hamburger /var/www/* But let’s suppose that /var/www/waterpipe is 2 TB in size and I don’t think hamburger is in there anywhere. How do I search everything else in /var/www/* but not /var/www/waterpipe? MANY thanks.

Reply
89 priya September 14, 2010 at 3:07 am

plz tel me the command to fetch a line from a file where the 5th col states a particular word like say “YES”

Reply
90 Geeta October 1, 2010 at 4:14 pm

Hi Vivek,
I have a concerns how to use grep for the following
1. In a file of 100 lines how to get contents from line number 75 to 90?
2. Cut lines in a file with index numbers 6, 7, 10, 11
3. Print lines with index 70 to 95 from a file using head and tail.

Anticipating u r reply
Thanks a lot

Reply

91 manish June 15, 2011 at 5:50 pm

3) head -90 filename | tail -25

Reply

92 Pradeep October 2, 2010 at 2:56 am

grep “^t[0-9].spq” filename

Reply
93 Geey Matlola October 5, 2010 at 7:20 am

that was so helpful, I now understand how does grep command works. now what soes this rilly specify “ps -ef | grep”

Reply
94 Erik October 7, 2010 at 12:57 am

Hi to all,
I just started to learn linux a month ago
Can I extract 2 to 6 letter words from a text file using one grep command only!
To mention that each word is on its own line
what’s the grep command to do this job?
I tried any combination of grep and not the result which I am looking for

Reply
95 Geey Matlola October 7, 2010 at 12:43 pm

someone reffred me the “man grep” command it rilly helped me to understand it.

Reply
96 Tommy K October 7, 2010 at 9:14 pm

How do I copy a text file but remove any blank lines held within that text using egrep?

Reply
97 kuldeep October 18, 2010 at 4:10 am

Hi,
i want to search path for file which has a no say ’7000000000′ in a server , how i can do this ??
please help

Reply
98 innocentfornow November 5, 2010 at 12:05 pm

I’ve got to list up all files that located in a folder like /usr/local/somefolder
to see every file it uses an absolute path for a command like

require once /some path/some file

for example
so I need a wildcard showing all files that uses all commands dealing with
absolute paths
for managing a migration to another platform…

Reply
99 Ron November 9, 2010 at 12:26 pm

i need to get the last line from a match with grep although there are multiple matches
Thanks for the tutorial, i learnt a lot

Reply
100 Excellent Apps November 25, 2010 at 9:28 am

Great post. Very Helpful.

Reply
101 girlatromance December 2, 2010 at 11:50 pm

Wow. This is exactly what I was waiting for.

Reply
102 hoxy December 15, 2010 at 2:42 am

I have files as below:

contents in ‘tempFile’ file
===================
file1.bak.p
file1.p
abcd.h
abcd.bak.h

how do I search only for the non “.bak: file?

I tried as below but not working

grep -v ‘.*\..*\.[a-z]*’ tempFile > tempFile2

Reply
103 hoxy December 15, 2010 at 2:48 am

actually I need to seach for words that only has one occurance of “.”

as such, ‘abcd.bak.h’ has two “.”

Reply
104 vikbenq January 5, 2011 at 11:51 pm

how can i grep logs using today’s date or say last 24 hours.

for ex: grep “logs” messages | grep “Jan 5″ — i can find logs for Jan 5 using this syntax but I want to use this in a script where I don’t have to use the date every time instead use something like 24 hours

Reply
105 Armand January 11, 2011 at 7:05 am

I know that it should work like this….

grep error /home/armand/Documents/errors.txt

but how do i ignore the ‘shutdown_error’ grep?

Reply
106 Gaurav Saxena January 25, 2011 at 8:26 am

This is Gaurav..

Plz tell about:-
$ grep -l”`echo ‘\t’`” foo

here foo is a file name….

thank u…

Reply
107 Gaurav Saxena January 25, 2011 at 8:29 am

now i can reduce my time in study,by using this website….
thanks for that…

Reply
108 Manu February 2, 2011 at 10:45 am

Hey that colour thing is not working on my machine….?

Reply
109 Marie February 13, 2011 at 12:23 am

How can we grep for patterns in an Arabic file? When I type the pattern to look for, it shows nothing. The file is in Arabic script, but the search on command line does not show that search string?
Can we read the binary file and search for a particular pattern there? Is there any website/ tutorial on using grep with Arabic script?
Thanks

Reply
110 Dinesh February 22, 2011 at 6:44 am

Dear all,
i have a folder consisting of some hundred txt files.Each file has a description about 10 lines ,then followed by six columns and more than 500 rows.My job( for each txt file) is among the six columns i have to search the string(contains numbers) in the second column ,if there is any matches in any row ,then in the output file i want the few specified lines in the description followed by the row which matches the string.so atlast i want single file which contains the desription of the file as well as the rows that matched the string.Thanks in advance..

Reply
111 Vani February 23, 2011 at 8:32 am

How to search any string or filname in all the sub-directory starting with same word. Example :
i need to search the word = ‘dataload’ in all the sub-directory which starts with ‘data2011′ in dir: DATA.
Where DATA directory has following sub-directory = data2010Jan, data2010mar, data2010july, data2011jan, data2011aug, data2009dec

Reply

112 Armand Groenewald February 23, 2011 at 9:17 am

you would use

grep -r dataload /?folders/DATA/data2011*

Hope that helps :)

Reply
113 Armand Groenewald February 23, 2011 at 9:19 am

if you want the results to go into a text file
use

grep -r dataload /?folders/DATA/data2011* > results.txt

then you can nano results.txt or vi results.txt or what ever program you want to use…

:)

Reply

114 Pascal March 3, 2011 at 8:32 am

I need to know how can i used the line that contain word “accused” without regarding for upper or lower case.using “greb command.
example: greb “accused” file

Reply

115 mikewolf March 5, 2011 at 11:14 am

grep -i “accused” file

Reply

116 murugesh March 13, 2011 at 4:57 am

How do i search a string in two files.

Reply
117 N.Srinivasan March 13, 2011 at 4:07 pm

If some can tell about how to search a particular srting in directory and its sub Directories, It would be of great help!!!!!!!!!!!

Reply
118 huisan March 18, 2011 at 7:00 am

HI, I wish to ask on how to use grep to produce the output result as below. Thank you very much

Input
FEATURES Location/Qualifiers
source 1..94601
gene 1..2685

Output
FEATURES Location/Qualifiers
source 1..94601

Reply
119 type8code0 March 20, 2011 at 6:32 am

@murugesh March 13, 2011
How do i search a string in two files?

Try

cat file1 file2 | grep string

Reply
120 type8code0 March 20, 2011 at 6:33 am

@N.Srinivasan March 13, 2011
If some can tell about how to search a particular srting in directory and its sub Directories, It would be of great help!!!!!!!!!!!

Try

grep -r string /dir/

Reply
121 arul March 28, 2011 at 11:49 pm

Hi,
I use a grep command to search a file.
That ouput is sent to a folder.
Since there are no space in the server, its showing an error like no space in desk

Any possibility of zipping the output of Grep command before writing to disk?

Reply
122 Nayabrasool April 18, 2011 at 3:37 pm

Pretty good description thank you

Reply
123 Heman April 21, 2011 at 4:44 am

How do i grep for lines containing a specific string, say “forrest”, while not containing another string “gump”?

Reply
124 neeraj April 24, 2011 at 10:22 am

There is a variant of grep known as gzgrep to search for a term in the argz archives.

Reply
125 Besso April 27, 2011 at 6:35 pm

Consider the file data.txt which contains the following data (MSISDN, credit, status, error_code, location_id) in the below format
MSISDN,Credit,Status,location_id,Error_code

0123318739,13213,A,300,abcde
0120123456,3423,C,200,xcvfe
0120453576,5563,A,201,fgsa
0110445654,3000,A,400,gcaz
0120432343,3000,A,402,dewa
0129423324,3000,A,206,dea
0104323433,3000,A,303,a
01232132134,3000,A,200,a
0122344242,4233,N,204,ghfsa
————————————————–
my question is how can i perform the follwing query
Try to have a command that can match all the below criteria

• MSISDNs range between 01201xxxxx to 0122xxxxxx
• And have credit over 3000
• Status either A or N
• location_id contains letter ‘a’
• Error_code starting with 2

The output of the command you will use when applied on the above file should be

0120453576,5563,A,201,fgsa
0122344242,4233,N,204,ghfsa

Reply
126 Besso April 28, 2011 at 1:11 am

please i need your reply urgently
ahanks in advance
=================
Consider the file data.txt which contains the following data (MSISDN, credit, status, error_code, location_id) in the below format
MSISDN,Credit,Status,location_id,Error_code

0123318739,13213,A,300,abcde
0120123456,3423,C,200,xcvfe
0120453576,5563,A,201,fgsa
0110445654,3000,A,400,gcaz
0120432343,3000,A,402,dewa
0129423324,3000,A,206,dea
0104323433,3000,A,303,a
01232132134,3000,A,200,a
0122344242,4233,N,204,ghfsa
————————————————–
my question is how can i perform the follwing query
Try to have a command that can match all the below criteria

• MSISDNs range between 01201xxxxx to 0122xxxxxx
• And have credit over 3000
• Status either A or N
• location_id contains letter ‘a’
• Error_code starting with 2

The output of the command you will use when applied on the above file should be

0120453576,5563,A,201,fgsa
0122344242,4233,N,204,ghfsa

Reply

127 Dinesh April 29, 2011 at 4:46 am

Try this i got correct output.
$grep -w ’012[0-2][1-9][0-9][0-9][0-9][0-9][0-9]‘ filename |
grep -w ‘[3-9][0-9][0-9][0-9][0-9]‘ |
grep -v -w ’3000′ |
egrep -w ‘A|N’ |
grep -w ’2[0-9][0-9]‘ |
grep ‘a’

Reply

128 Dinesh April 29, 2011 at 4:53 am

important change : change the first line into following two lines :
$grep -w ’012[0-2][0-9][0-9][0-9][0-9][0-9][0-9]‘ filename |
grep -v -w ’01200[0-9][0-9][0-9][0-9] |

Reply

129 Besso April 29, 2011 at 11:18 am

Thanks alot you are genuios
some of the lines worked normally but when I tried to combine them it gives me error so all iam asking is to give me the whole bulk of code to run it in one sentence. Thanks again man you are really a life saver
BR,
Besso

Reply

130 Dinesh May 5, 2011 at 4:14 am

hi..,i m not getting getting exactly what u r asking ..? they are all piped ones../
u write all these in .sh file then run…also u want to use these code for single files or multiple files..?

Reply

131 swapnil May 4, 2011 at 7:26 am

question : i want to find only the directory in a particular directory.so what is the command in linux to do so.pls help

Reply

132 neeraj May 5, 2011 at 4:06 am

@swapnil:
find -type d

Reply

133 Divya May 17, 2011 at 9:14 am

Hi,

i want to move few file from one location to another. there is no pattern except for one -> “05_ _2011″. these two underscores can be any number. i am not able to move or do a grepfor this pattern.

Please help.

Reply

134 Dinesh May 18, 2011 at 4:50 am

let /home/user/folder be the direcrory contains .txt(or any other extensions) files.Some of these files contains the string ’05_ _2011′.We find those files which contains the string ,and copy files to a new location.
Save the follwing code as program.sh file
———————————————-
#! /bin/bash
grep -l ’05[0-9][0-9]2011′ *.txt |
while read line
do
cp /home/user/folder/$line /new/path
done
—————————————-
the files will be copied to a new path with a same file name.
hope it helps.

Reply

135 faizan May 20, 2011 at 8:11 am

its a very helpful for me, thank u
Regards,
Faizan Khan
Pakistan Karachi

Reply
136 Gobinda June 13, 2011 at 7:10 am

How to grep a log file
from 07-JUN-2011 01:13:00 to 09-JUN-2011 15:03:04

my date format is 07-JUN-2011 01:13:00

Reply

137 Dinesh June 13, 2011 at 12:33 pm

$grep ’0[7-9]\-JUN\-2011′ logfile

if we specify time, we need to filter more with grep,that makes code difficult and writing lots of lines.Instead get the list for three days, delete the contents before 07-JUN-2011 01:13:00 and after 09-JUN-2011 15:03:04 in a notepad.
Hope it helps

Reply

138 Gobinda June 13, 2011 at 1:03 pm

its a very helpful for me, thank u
Regards,
Gobind

Reply

139 matias June 29, 2011 at 8:56 pm

why are all tutorials incomplete?

Reply

140 Vivek Gite June 30, 2011 at 12:43 am

What are you talking about?

Reply

141 shihab June 30, 2011 at 3:48 am

Hi,

This helps a lot, Could you please explain grep command.

” ls -l | grep -a “^d” | tee dir.lst | wc -l “

Reply

142 Vivek Gite June 30, 2011 at 5:39 am

The grep will get only name of directories from the ls -l output.

Reply

143 Ihab July 8, 2011 at 3:01 pm

How can I grep for 2 strings (string1 and string 2) in 2 log files: log1 and log2 in one command? I also need to show 3 lines before and 3 lines after each string.
Thanks in advance.

Reply

144 Dinesh July 11, 2011 at 4:16 am

for the first step you use the following command
$ cat log1 log2 | grep ‘string1.*string2′
I didn’t get the appropriate meaning for ’3 lines before and 3 lines after each string’
hope it helps

Reply

145 sid April 20, 2012 at 4:42 pm

@lhab
Can you try this and let me know if it suits your requirement:

$ cat log1 log2 | egrep ‘string1|string2′| egrep -A3 -B3 ‘string1|string2′

Reply

146 jetole April 21, 2012 at 7:15 am

This is what the bash zealots refer to as unnecessary use of cat.

either
grep -e ‘string1′ -e ‘string2′ log1 log2

or
grep ‘string[12]‘ log1 log2

or
egrep ‘string(1|2)’ log1 log2

though… assuming string1 and string2 are completely different / not entirely the same except for the last two into digits then we could use either
egrep ‘(string1|string2)’ log1 log2

or
grep -E ‘(string1|string2)’ log1 log2

or
grep -e ‘string1′ -e ‘string2′ log1 log2 (which I already mentioned)

Enjoy! :-)

Reply

147 jetole April 21, 2012 at 7:16 am

I meant to say “int digits” referring to integers and not “into digits”.

…long night.

Reply

148 Dinesh July 11, 2011 at 4:16 am

for the first step you use the following command
$ cat log1 log2 | grep ‘string1.*string2′
I didn’t get the appropriate meaning for ’3 lines before and 3 lines after each string’
hope it helps

Reply
149 Ihab July 11, 2011 at 6:22 am

Dinesh,
Thanks for your reply. What I am trying to search for is a log with multiple daily entries. One log may contain many days worth of entries. One day of log entry can be located in 2 different logs (log1 and log2). For example:

1) Let’s say that I want to search all the log entries of July 1, 2011 that are located in log1 and log2.
The first string in the log entry is the date which has the following format: 2011-07-01 and the second string which can be anywhere in the same log entry: ‘transaction failed’ or ‘error’ or ‘unsuccessful’.
So string1 = 2011-07-01 and string2 = ‘transaction failed’ or ‘error’ or ‘unsuccessful’.
2) Each log entry can be up to 6 lines long. Log entries are seperated by 1 blank line and another line says: END OF ENTRY. I believe grep command would only display the line that contains both strings (string1 and string2) but I would like also to display the whole log entry that contains both string1 and string2 if possible.

Regards,
Ihab

Reply
150 ROHIT July 11, 2011 at 11:59 am

I have to search for two words..for ex animal …after it is present i need to search for dog …Basically i have to AND both the terms…suggest me the command?

Reply

151 anoop July 26, 2011 at 12:27 pm

hi rohit
u can use pipelining command
for example
suppose ur file name is animal_list
then u give a command like this
cat animal_list | egrep -i ‘ex | animal’ | grep -i ‘dog’

Reply

152 Gobinda July 15, 2011 at 12:57 pm

In RHEL 4 update 8 RAM is 16GB what will be the suggested SWAP and kernel parameters for running oracle 10.2.0.3

Reply
153 anoop July 26, 2011 at 12:23 pm

very intresting and good way.. thanks

Reply
154 naveen July 27, 2011 at 11:06 am

this is useful everyone

Reply
155 kintelk July 29, 2011 at 8:16 am

hi

this site is very hopeful to me !
thanks to this website , i can lean the grep command esilry~
bye. i will expect more precious info .

Reply
156 rajasekhar vinjamuri August 2, 2011 at 7:48 am

how to find a string in a file..that string is middle of the file

grep ‘^string’ file name..starting of the line
grep ‘string$’ filename ending of the line

then how to fine that string middle of the line by using grep

Reply
157 i August 11, 2011 at 1:26 pm

can somebody give me examples of

grep –color=auto foo myfile
$ grep –color vivek /etc/passwd

I need to find a string using grep then i want to see the searched text in a specified color where it’s present.

Reply
158 Jesse Leal dos Santos August 18, 2011 at 9:14 pm

Nice tuts. Thanks!

Reply
159 jsengu August 25, 2011 at 6:09 am

Hi…. I have a xml file named ” Customer_REPEAT_159.xml”, this file has an element named
940547722

My requirement is to use a shell script to extract the value of the ARC_ACCT_NUM; i.e. 940547722, and rename this file, to (ARC_ACCT_NUM)_CurrentTimestamp, i.e. 940547722_20110825114523.

Can someone help me out in this?
Please let me know, if any further information is required on this…. :)

Reply
160 amirali August 25, 2011 at 8:40 am

it was realllyyyyyyyy awsoome! really useful! thank you very much!

Reply
161 Ram September 6, 2011 at 5:18 am

This tutorial s realy gud..simple and lso 2 point..

Reply
162 Brian September 8, 2011 at 5:17 am

I have a line in a file i.e.

/a/b/c/d

How do I use grep and awk to print
a b ?

Thanks

Brian

Reply

163 Brian September 10, 2011 at 8:42 am

cat file | awk -F ‘/’ ‘{print$1, $2}’

Reply

164 santhosh September 9, 2011 at 4:45 am

Wonderful,simple ,nice and clean Thankyou

Reply
165 sudheer September 11, 2011 at 2:55 am

1) Use grep (or awk) to output all lines in a given file which contain employee ID numbers. Assume that each employee ID number consists of 1-4 digits followed by two letters: the first is either a W or a S and the second is either a C or a T. ID numbers never start with 0s. Further assume that an employee ID is always proceeded by some type of white space – tab, blank, new line etc. However, there might be characters after it, for example punctuation.
What to turn in: Turn in three things:

a. A file with the regular expression which can directly be used by grep (or awk)

b. A text file which you used to test your regular expression. Make sure that you include valid and ‘invalid’ employee IDs, have them at the beginning and the end of lines, sentences, etc.

c. A second document which re-writes the regular expression in a more human-readable form and explains the purpose of the different components of the regular expression. Also include a short explanation of your test cases..

2) Use grep (or awk) to output all the lines in a given file which contain a decimal number (e.g. a number which includes a decimal point). Decimal numbers do not have leading zeros but they might have trailing zeros. Assume the number is always surrounded by white space.

What to turn in: The same three things as above (except, of course, for this problem).

3) Write a regular expression for the valid identifiers in Java. You are allowed to use ‘shortcuts’, but need to make sure that you specify exactly what they are (e.g. if you use digit specify that that means 0, 1, 2, 3, ….9.)

Reply
166 sapna September 22, 2011 at 3:09 pm

Q1)by using grep command how do display all those lines in a file that has only uppercase characters?
Q2)by using grep select all those lines which has a ?(mark) at the end of line?
Q3) by using grep command how select all those lines which has any of the following patterns ab,aab,aaab or like others?

Reply
167 Aim&fire September 29, 2011 at 3:42 am

How can I use grep with wc and uniq commands? I know I have to use piping, but I can’t seem to get the order right. Can someone help?

Reply
168 ghafil October 18, 2011 at 10:18 am

how to do grep -v ‘pattern1|pattern2|pattern3′ with invert search?

Reply
169 chandu October 19, 2011 at 12:03 pm

very usefull………

Reply
170 David November 3, 2011 at 6:46 pm

Can anybody give me an idea what these grep commands are doing? I know they are specific to the business process where I am, but any insight would be helpful.
$tzfile is a timezone file
$alist is a file with three lines each with a numeric value

grep NEWPOS $tzfile |cut -c1-4 >$alist
grep FUTQP $tzfile |cut -c1-4 >>$alist
grep CLB $tzfile |cut -c1-4 >>$alist

Reply
171 zip November 21, 2011 at 3:23 pm

how do i do this- grep -w “…” filename
with the comand “sed”?

Reply
172 siva November 23, 2011 at 8:04 am

Hi ,

I’m new to linux , I would like to know how to identify the blank files in linux for example
[root@xxxxxx]# cat dfpAL52sS5028817

[root@xxxxxx]#
[root@xxxxxx]#
[root@xxxxxx]#

I would like to identify the files that shows blank as above , can any one help me this.

advance thanks,
Siva.

Reply
173 jetole December 2, 2011 at 1:47 pm

while some of this may work, it’s a rather inefficient way to do it. You can read the man page for both ps and grep by running the commands “man ps” and “man grep” (without the quotes) to get a good idea of what all the options are. In doing so you can see that you can specify the command you are looking for in ps using the -C option, you can format the output to look how you want with the -o option and you can remove the headers which usually become negligible when using the -o option by adding the –no-headers option. Additionally with grep their is a -q option which causes grep to be quiet so there would be no need to redirect output to /dev/null and another useful option would be -w which matches word only. Additionally, if you run the command “help if” (without the quotes) or “man bash” and skip to the if section, you will see that the if clause evaluates the return of a command. “[" in a if clause is a command (either a shell built-in usually or can be an external command. You likely have both but the shell built-in takes precedence. Because of this, you can use grep as the command if looks at instead of the [ command/clause and narrow down your code a lot more to look something like this:

orasid="tiger"
if ps -C oracle --no-headers -o cmd | grep -qw "${orasid}"
then
echo "${orasid} found. PID's of oracle containing ${orasid} listed below"
ps -C oracle -o pid,cmd | grep -qw "${orasid}"
else
echo "No instance of oracle found with ${orasid} in the options"
fi

I'm using ${variable} here instead of $variable which isn't supported under all shells but it is supported under bash. It's not necessarily needed here however I prefer to use most of the time (there are some instances where you shouldn't use it) because I consider it a safe way to protect against errors where perhaps part of the text is not part of the variable, for example, if my variable named num containing the number 5 and I want to echo "The 5th number" where I use the variable num for 5 then, if I wrote

num=5
echo "The $numth number"
# This would print "The number". It would not know that th isn't part of the variable and it would
# think the variable is all of $numth which doesn't exist so it wouldn't print anything for that var

instead

num=5
echo "The ${num}th number"
# This would print "The 5th number". Using ${} we define exactly where the variable starts and
# ends so it knows that num is the variable and the "th" is just text to echo.

Again, the ${} isn't portable and may not work outside of bash but, on the other hand, bash has been around longer then Linux (bash since 89, Linux since 91) and is the default shell on most Linux distros so it's there to use and really worth it.

Another plus about using bash, again, this isn't portable and may not work outside of bash but you can use if [url=_my_test_] my_test [/url] instead of if [ my_test]. [url=_should_only_be_a_shell_built_in._What_I_mean_by_this_is_that_[_can_ei ther_be_a_shell_built_in_function_(and_most_commonly_it's_the_shell_built_i n_you_use_or_[_can_also_be_a_command._[[_is_almost_never_the_name_of_a_comm and_and_hence_why_it's_not_portable_but_if_you_are_using_bash_and_you_proba bly_should_be_unless_you_know_why_you_want_to_use_another_shell_more_but_if _you_are_using_bash_then_[[_and_] should only be a shell built in. What I mean by this is that [ can either be a shell built in function (and most commonly it's the shell built in you use or [ can also be a command. [[ is almost never the name of a command and hence why it's not portable but if you are using bash and you probably should be unless you know why you want to use another shell more but if you are using bash then [[ and [/url] is less error prone then [ and ] and allows you to perform multiple checks within the same test clause, for example if you wanted to test to make sure one check is correct or another check is correct using [ and ], the safest way to do so would be to have to separate if statements outside of each other both performing the same action i.e..

if [ 1 == 1 ]
then
perform this action
fi
if [ 2 == 2 ]
then
perform that exact same action
fi

In theory you can use:

if [ 1 == 1 ] || [ 2 == 2 ]

However chaining or statements like that in multiple commands to the if statement to see if either one is true is frowned upon and is said that it may cause unexpected results. The proper way to do this from bash would be:

if [[ 1 == 1 || 2 == 2 ]]
then
perform this action
fi

If you try to use:

if [ 1 == 1 || 2 == 2 ]
then
perform this action
fi

then it will fail because it will treat the || inside the [ and ] as a bash || statement between two command thinking “2 == 2 ]” is a second command and will complain that you are missing the closing ] on the first command. Using [url=_and_] and [/url] you can also use an AND clause to all tests are true instead of if any one test is true, for example

if [[ 1 == 2 || 2 == 2 ]]
# This returns true and performs the action in the if clause because it only needs one of the two to be true.
if [[ 1 == 2 && 2 == 2 ]]
# This returns false and performs the action in the else clause of nothing if their is no else clause and this would require both statements to be true which they are not because 1 does not equal 2.

For more details on all the operators you can use within [ ] and [url=_] [/url], run the commands “help test”, “help [" or "man bash" (without the quotes) and if you look at the man page for bash, jump down to the section "CONDITIONAL EXPRESSIONS" (again, without the quotes).

Another handy test method to use inside bash is (( and )) for number only comparison. You can use [url=_and_] and [/url] to test if a file exists or if a string is the string you want etc but (( and )) are only for numbers (more specifically integers but I’ll dive into that in a moment). which can help make sure you are not accidentally testing something you shouldn’t be. Also you can use “declare -i” (sans the quotes) to define a variable that can only hold an integer and if anything is assigned to it that is not an integer then it will default to 0.

declare -i my_variable="boat"
echo "${my_variable}"
# This will print 0 because my_variable doesn't allow strings when defined with declare -i
declare -i my_variable=15
echo "${my_variable}"
# This will print 15 because it will only accept integers.
declare -i my_variable=15
if ((${my_variable} > 10))
then
echo "${my_variable} is greater then 10"
else
echo "${my_variable} is 10 or less then 10"
fi
# This will print "15 is greater then 10". If you change the var to 10 or lower then it will execute the else statement.
declare -i myvar=1.5
# This will cause an error because integer variable only allow whole numbers.
if (( 1.5 == 1.5 ))
# This will also cause an error because (( and )) will work with integers / whole numbers.

You can store decimal numbers in regular variables though there is no special var designed only for decimals. There are a lot of different ways to test decimal numbers but the best, IMHO, is using the command bc command. You can use this inside a (( )) test by calling the bc command using command substitution with $(command). People often use back ticks, ” `command` ” (sans the double quotes), for command substitution which, again, back ticks are portable and $( ) doesn’t work in all shells however, if you are using bash and you probably should be using bash then $(command) is more more powerful and back ticks are much more prone to error. When using bc to perform a comparison, for example, if x < y, then bc prints 1 when it is true (if x really is < y) else it prints 0 if the comparison is false (compare x < y but if x is greater then or equal to y then x < y is false so it prints a one). Here's an example of how to compare decimal numbers.

myvar=12.5
if (( $(bc <<< "${myvar} < 15.2") == 1)
then
echo "${myvar} is less then 15.2"
else
echo "${myvar is greater then or equal to 15.2"
fi
# The if statement is checking to see if the number printed by the $(command) is equal to 1.
# bc is saying is 12.5 less then 15.2 which it is so the comparison is true so bc prints 1
# Knowing that bc is printing one, then the if statement could be simplified for demonstation purposes by saying
if (( 1 == 1 ))
# That doesn't make sense to test just because it's too simple but that gives you an example of how the if statement looks to bash based on the result provided by bc in the command substitution.
# If you just want to do basic math on decimal numbers in bash you can do something like
num1=12.5
num2=15.2
sum=$(bc <<< "${num1} + ${num2}"
echo "The sum of ${num1} + ${num2} = ${sum}"
# This will print: The sum of 12.5 + 15.2 = 27.7

I think I have gone pretty sufficiently above, beyond and way off topic here so I am going to quit before I bore anyone to death but I really did want to enlighten people to the power of bash and it’s capabilities and I believe I have done a good job at starting that so far so I’m gonna quit while I’m ahead, if I am ;-)

Reply
174 jetole December 2, 2011 at 1:53 pm

Oh P.S.

I don’t know what the name of the oracle server is. In my first example I used:

ps -C oracle

This was assuming the name of the command running is oracle however if you know part of the command name then you can find the exact command name by running:

ps -A -o ucmd --no-headers | grep -i oracle

The output ucmd shows only the command running without any arguments unlike “-o cmd” which shows the command and all arguments so when you use “-o ucmd” and do a case insensitive search for oracle, it will return any commands that are either named oracle or have oracle as part of their name but it will not return the grep command because ucmd isn’t showing arguments so the ps listing, for the line which matches grep will just show “grep” and not “grep -i oracle”. When you run the above command to find your commands name, you can replace oracle with all or part of the command you are looking for and it will return any patches, case insensitive (-i option for case insensitive).

Reply
175 Manish December 7, 2011 at 5:34 am

so much helpful nd clear document………………………………………

thanks

Reply
176 ayat sbih December 11, 2011 at 8:54 pm

can you help me to learn how i can replace one word with another?

Reply
177 Shehzad-Bilal December 21, 2011 at 7:22 am

Great tutorial

Reply
178 mj December 30, 2011 at 6:13 am

i need find the starting “p” in all the files at a single directory.

Reply

179 Babu August 1, 2012 at 11:27 am

grep ‘p*’ /.

Reply

180 Sangita August 9, 2012 at 11:15 am

grep -i ‘p*’ /

Reply

181 vivek January 2, 2012 at 2:49 pm

i need to grep only one occurrence of character / in std output another unix coomand on LINUX platform as example below :-

jar -tf *.zip | grep ‘\/’
installer/
installer/lib/
installer/lib/
………

anyone please help me with solution

Reply
182 Akiva January 5, 2012 at 7:51 am

thanks

Reply
183 perg January 20, 2012 at 5:51 am

Thanks, was helpful even 4 years later!

Reply
184 simmy January 22, 2012 at 3:59 pm

really super site to learn linux grep command

Reply
185 joanna January 23, 2012 at 7:00 am

what if we want the output of one command, minus the output of another command… ? by using grep -v?
for example ls and ls file.txt (i.e print the output of ls, except the ones related to file, using grep… )

Reply
186 jay February 6, 2012 at 3:38 am

how to find/search particular date & time when we have more than 1000(times and date) stored in a log file?

Reply
187 dv5855 February 21, 2012 at 10:28 pm

Your explanation is exactly what I needed.

Reply
188 Alex February 26, 2012 at 4:13 am

How would I find the number of words in a file which do not contain any of the letters a, e, i, o, or u?

Reply

189 Alex February 26, 2012 at 4:33 am

wait never mind i figured it out — the tutorial was very helpful in letting me understand grep.

Reply

190 Shreya March 8, 2012 at 4:45 pm

Hi,I get multiple entries as given below in logs, If i find the keyword ” Failed to process change of products” in logs, then i want to print the partyid (which is at 4th line given below) i.e (13312221435179143122818) number should be printed.
Can some one please help me ?

Logs:
Failed to process change of products . Message XML is [

13312221435179143122818

13744
CN
2012-03-08T16:24:38Z

Reply

191 jetole August 23, 2012 at 9:59 pm

grep -A3 “^Failed to process change of products” data | grep ‘^[url=:blank:]:blank:[/url]\+[0-9]\{10,100\}[url=:blank:]:blank:[/url]*$ ’ | sed -e ‘s/^[url=:blank:]:blank:[/url]\+//;s/[url=:blank:]:blank:[/url]\+$//’

This is actually three commands where we pipe the output of one command as the input to another. I created a test file called data. The first command is

grep -A3 “^Failed to process change of products” data

This is saying to search the file named data for a line that starts as “Failed to process change of products”. The ^ at the front represents the start of the line. The -A3 option says to return 3 lines after the match. Your match is the first line and then the three below it make 4. The next command

grep ‘^[url=:blank:]:blank:[/url]\+[0-9]\{10,100\}[url=:blank:]:blank:[/url]*$ ’

This one gets a little trickier. We have a match to a whole line (which be careful when you write those). The $ symbol represents the end of the line so all characters always have to be between ^ and $. the part after the start of line, [url=:blank:]:blank:[/url] represents any blank character. This can be a tab or a space or there are a couple other matches (man 7 regex). The \+ says to match as many of these as you see referring to the item right before it. This means if it sees one space or 20 spaces it all becomes part of that regex. If you search for “a[url=:blank:]:blank:[/url]b” then you are searching for exactly “a b” with one space or one tab, etc. If you are searching for “a[url=:blank:]:blank:[/url]\+b” then you could be searching for “a b” or “a b” or any variation of spacing in that order. [0-9] says to match all number characters (between 0 and 9) and the \{10,100\} says how many have to match so by saying 10, 100 it means we need to find a string of numbers with between 10 digits and 100 digits. This makes sure if get a short number that turns out to be not really the number we wanted and if we know the number is going to be at least x digits then we can use logic to eliminate false positives. This is followed by another [url=:blank:]:blank:[/url] and now with a * witch means match non or more. Unlike \+ witch matched one or more, * is ok if it doesn’t see any. This is followed by the $ for the end of line. What this rule means is match any line with an unknown amount of spacing at the start followed by a number at least 10 digits long and no longer then 100 digits long. We then say if there is any space at the end of the line to match that as well and then, either way, that has to be the end of the line. This means if the line contains anything other then space to numbers and maybe space then we don’t want it. if we insert a letter or symbol anywhere on the line then it wont match. If there is no spacing at the start of the line then it wont match. The last command,

sed -e ‘s/^[url=:blank:]:blank:[/url]\+//;s/[url=:blank:]:blank:[/url]\+$//’

This is actually two sub commands for sed combined. The -e option says execute this sed request. The ; symbol ends the command and you can start another one (this applies to bash and most shells and many programming languages too. The two commands are

s/^[url=:blank:]:blank:[/url]\+//
s/[url=:blank:]:blank:[/url]\+$//

The first one says if there is any empty space at the start of the line then to delete it. The format is s/search/replace/ so if I wanted to replace the word cat with dog I would use s/cat/dog/ where it matches cat and replaces it with dog. Using the two forward slashes at the end says replace it with nothing or delete it. The second rule says if there is any space at the end then delete it. This gives you back just the number without any tab/space padding on either end.

I just realized how I could have wrote the second grep and the final sed as just one sed but I’ve said too much already.

Reply

192 raj April 1, 2012 at 7:17 pm

hey
can u help me with this i’m trying to find all the file extension present in a directory

like i have a folder with files
rqed.txt
few.pdf
fwd.xml
fwesdx.c
gfwd.sh
frw.mp3
fws.avi
then grep command should get all the extension txt pdf xmlc sh mp3 avi...

Reply
193 jetole April 2, 2012 at 3:28 am

Not sure this is a grep “thing”. If you just want to find all files with a certain extension in a directory then run
find . -type f -maxdepth 1 -iname ‘*.pdf’

If you want to see all file extensions in a dir then run
find . -type f -maxdepth 1 -iname ‘*.*’ | sed -e ‘s/^.*\.// | sort -u

Reply

194 raj April 2, 2012 at 3:17 pm

getting an error with this syntax
sed: -e expression #1, char 1: unknown command: `�’
find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.

Reply

195 Tom April 23, 2012 at 8:11 pm

Hey guys,

Do you guys know how to use the grep command when in a txt.file or any file.
for example when there are so many lines, how do you the grep command to return the result and its highlighted?

/someword — highlights the result

Reply

196 Vivek Gite April 23, 2012 at 8:25 pm

Tested with gnu/grep :)

grep --color 'word' file
grep -o --color 'word' file

Reply

197 Tom April 23, 2012 at 9:27 pm

Can you explain what GNU/Grep is?

Here is what im trying to do. When I vi or view (ie View sometextfile) I want to be able to grep or / and find the word?

Thanks,
Tom

Reply

198 jetole April 24, 2012 at 1:42 am

vim is a command (and view is a symlink to vim). grep is a command. While vim gives you the ability to call commands from inside of it, it’s not what you are referring to or what you want. If you want to search from within vim (which has nothing to do with grep and cannot have anything to do with grep, then you press / followed by your search term, for example if I wanted to search for “dogs” inside a document I am accessing via vim/view then I would press /dogs followed by enter though it will start trying to match immediately before I hit enter but will go back to where it was initially if I press escape.

If you want to highlight search matches in vim/view then type :set hlsearch followed by enter and all of your matches will now be highlighted. If you want to turn the current highlighting off without searching for something else (even non-existant) then type :nohl followed by enter. If you want to make sure that vim does this highlighting automatically then add the following line to your ~/.vimrc: set hls and that will turn hlsearch on by default (which I personally prefer to have this set in my .vimrc).

Seeing as how vim/view have nothing to do with grep, please don’t reply to this comment. It’s better for you to start a new thread or follow up on a existing thread relative to the topic instead of asking about new topics in existing threads about other topics but seeing as how I can tell you didn’t know the difference before you asked, that’s why I chose to give you an answer anyways ;-)

Hope this helps.

Reply

199 jetole April 24, 2012 at 1:45 am

P.S.: My reply was supposed to have a more clear representation of the commands but I should have read allowed HTML commands first.. Oops.

For clarity, these are the lines/commands I meant to have stand out:
/dogs
:set hlsearch
:nohl
set hls

Reply

200 Paul May 3, 2012 at 3:29 pm

What Im wondering is where the “HERE” in below is a variable or other..
It works, but Im not sure why its needed?

In this find, Im looking for files in the directory that are 10days or older and then gzipping them. Its more complex than just this, but this was the previous find before we fixed a production problem that is now ok.. This runs ok btw.

Im new enought to this to not be sure whats what.

find $log_directory -type f -mtime +10 | xargs -iHERE gzip HERE

Reply
201 Paul May 3, 2012 at 3:40 pm

Another grep question:

Lets say I want to look at files in a directory that has all sorts of dates for their files.
SALES might be SALESTulsa.txt or SALESDallas.txt but differnt dates.

Scenario:
ls | grep SALES | grep ‘May 3′ – would give me the files for that date.

But how would i get may 3 & may 2? or even just current date (today)?

Reply
202 Paul May 3, 2012 at 4:02 pm

in the above… some sort of ‘may 2′ or ‘may 3′ mix would be what Im after.

Reply
203 jetole May 4, 2012 at 3:20 am

Paul,

On my system and I think this is standard on all *nix, not sure, but on my system, ls -l shows dates like 2012-05-03.

With dates like such, there are a few ways to grep this out:
ls -l SALES* | grep -w ’2012-05-0[23]‘
ls -l SALES* | grep -w -E ’2012-05-0(2|3)’
ls -l SALES* | egrep -w ’2012-05-0(2|3)’

[ and ] is used to match any of the chars between [ and ] so [23] matches either 2 or 3. [0-9] matches any single number. [a-f0-9x_] matches a through f, 0 through 9, x or _. The -w option says match that word so it will match 2012-05-02 and 2012-05-03 but not 2012-05-026 or x2012-05-03. The -E option is extended regex (though you really don’t need it in this case) and egrep is grep set to automatically do the extended regex without the option. The first example really should be all you need but extended grep can be used for more complex matches for example:

egrep -i ‘(dog|bird) house’ pet_accessories.txt

That would go through the file pet_accessories.txt and match any lines containing “dog house” or “bird house” regardless of upper case or lower case (-i option).

Reply
204 BIGuy June 5, 2012 at 10:30 am

Thanks Vivek, great tutorial!

Reply
205 Nemanja June 7, 2012 at 9:12 am

hi everybody,
how i can search String in whole Folder and subfolders?

Reply
206 jetole June 7, 2012 at 4:33 pm

Nemanja,

-r

Reply
207 Raj June 14, 2012 at 11:34 pm

I want to just redirect o/p to different file who is between “2012-06-14 13:00:00″ – “2012-06-14 14:00:00″..

I have tried comparing the $3 with epoch time but if file is huge it takes around 15 mins to parse every single line time. Could you please help me with efficient command that will fetch data with above mentioned file and redirect it to New.dat?

file Name Raj.dat
CLASS 2012-06-14 11:04:41,076 3 A 22
CLASS 2012-06-14 11:11:56,217 4 B 23
CLASS 2012-06-14 11:31:41,601 5 D 23
CLASS 2012-06-14 12:34:12,813 1 B 45
CLASS 2012-06-14 12:35:57,845 2 H 12
CLASS 2012-06-14 12:49:43,112 3 O 44
CLASS 2012-06-14 12:04:41,076 3 A 22
CLASS 2012-06-14 13:04:41,076 3 A 22
CLASS 2012-06-14 13:11:56,217 4 B 23
CLASS 2012-06-14 13:31:41,601 5 D 23
CLASS 2012-06-14 14:34:12,813 1 B 45
CLASS 2012-06-14 14:35:57,845 2 H 12
CLASS 2012-06-14 14:49:43,112 3 O 44
CLASS 2012-06-14 15:04:41,076 3 A 22
CLASS 2012-06-14 15:11:56,217 4 B 23
CLASS 2012-06-14 15:31:41,601 5 D 23
CLASS 2012-06-14 15:34:12,813 1 B 45
CLASS 2012-06-14 15:35:57,845 2 H 12
CLASS 2012-06-14 15:49:43,112 3 O 44

———–
Thanks,
Raj

Reply
208 jetole June 17, 2012 at 5:14 pm

grep -w -e ’2012-06-14 13:[0-9]\{2\}:[0-9]\{2\}’ -e ’2012-06-14 14:00:00′

Reply
209 jetole June 17, 2012 at 5:17 pm

I’m thinking I can’t guarantee what I just would be the fastest option out there but this is a grep forum board we’re on and that’s one of many ways to do it in grep. If I really wanted the fastest time then I would import it into mysql and use RDBMS queries to select where timestamp >= x and timestamp <= y.

Reply
210 Jason June 28, 2012 at 3:40 pm

In your 6th example, “Use grep to search 2 different words”, you use the command:
$ egrep -w ‘word1|word2′ /path/to/file

Is the “egrep” intentional? Or is it a typo.

Jason

Reply
211 zhaoyou July 4, 2012 at 9:08 am

cool, it is good for newer

Reply
212 Sushant Singh July 20, 2012 at 6:45 am

does the command ‘grep cron messages’ show all the scheduled tasks?

Reply
213 jetole July 20, 2012 at 11:20 am

No. grep shows matches of a word in a file. You can use at to list scheduled tasks through at. You also need to check out the crontab files/dirs in etc and also each users crontab personal file which I believe would be /car/spool/cron (not sitting in front of computer) or can also be viewed by running: sudo -u crontab -e

Reply
214 jetole July 20, 2012 at 11:22 am

I meant to say you can use atq but my phone auto corrected it.

Reply
215 sinah August 23, 2012 at 4:14 pm

Hi everyone!
I want to write a command that will search all files in the current directory for any files that contain the word “cat”.By using a grep command

Reply

216 jetole August 23, 2012 at 9:26 pm

did you the command
man grep
If that doesn’t work try
grep -riIl ‘cat’ .
-r is recursive meaning it searches recursively in whatever directory you specify. -i is case insensitive so it matches cat, CAT, cAT and more.. -I says ignore binary files and don’t check them. This speeds the search up and it means it’s not looking in the content of a executable or any other type of file not designed to be read by humans (this would include a write doc as excluded so leave that option off if you need a match in those types of files but keep in mind words are not necessarily stored in the format you wrote in those types of docs so it might not match what you’re looking for either if that’s the type of file you’re looking to match in. -l says list the files. This shows you all files that match but doesn’t show you each the matching line. If you don’t use this option it will display every line in the file that matched your regex. Speaking of regex, try the command
man 7 regex.
It’s very handy and useful :-D

Reply

217 jetole August 23, 2012 at 9:27 pm

Opps. Those commands were supposed to be in a code box. The last command is

man 7 regex

Not “man 7 regex.” (no period).

Reply

218 Michael September 1, 2012 at 7:10 am

Thanks a lot. Straight and to the point.

Reply
219 NILESH September 29, 2012 at 6:15 am

what is a ‘grep’ command to find the users who are not using terminals pts/2, pts/3.

Reply
220 Shahzad Hussain October 10, 2012 at 12:29 pm

can you help me how can i learn Unix and shell scripting within few months (3 months)

waiting for reply on mail

Reply
221 pupu October 22, 2012 at 12:53 pm

cat /etc/passwd | grep -i -e ‘Administrator’ > output.txt

Reply
222 Jan January 9, 2013 at 2:00 am

Hi,
great article, just one smartass-necessity:
cat file.txt | grep ‘some text’
and
grep ‘some text’ file.txt
do actually the same, with the difference that the first line uses an additional process, ‘cat’. So the second line is more efficient and less to type :)

Greets,
Jan

Reply
223 Rao January 11, 2013 at 10:20 am

Need a small help w.r.t to grep.. I have file which contains some strings with “.wav” word. When I search using grep I am getting a lines which contains the keyword i am searching for.
Now the problem is I want to fetch the word that is coming along with .wav also , for example
File content:
=============
“2013-01-10/10:08:45.049 METRIC 0002010E-2498E59A prompt /usr/local/phoneweb/tmp/0002010E-2498E59A-0001//promptsfile.0002010E-2498E5 9A-0001.3:audio|file:///opt/product/tomcat/webap
ps/LycaIVRS_2.1.8.1_Rev0/promptFiles/audio/ORANGE/ENGLISH/validityexpiry.wa v;audio|file:///opt/product/tomcat/webapps/LycaIVRS_2.1.8.1_Rev0/promptFile s/audio/ORANGE/ENGLISH/Digits
/mon-01.wav;audio|file:///opt/product/tomcat/webapps/LycaIVRS_2.1.8.1_Rev0/ promptFiles/audio/ORANGE/ENGLISH/Digits/day-09.wav;audio|file:///opt/produc t/tomcat/webapps/LycaIVRS_2.1
.8.1_Rev0/promptFiles/audio/ORANGE/ENGLISH/Digits/year-2014.wav;
GLISH/roamingAnnc.wav;”

Above example is a single line. Like these many lines will be present in the file.
Now I want to pick only “validityexpiry.wav”,”mon-01.wav”,”year-2014.wav” basically the words that are ending with .wav
I tried with egrep -o “*.wav;” pw_metricsfile –> but its printing only “.wav;” as output
Can some body suggest how can i exactly pick the word i want.

Thanks inadvance

Reply

224 jetole January 11, 2013 at 10:25 am

I named the file test.txt and used the command grep -io ‘[^/]\+\.wav’ test.txt

Reply

225 Rao January 11, 2013 at 11:32 am

Thanks a lot Jetole…its working like a charm….can you please explain the syntax usage for my better understanding.

Reply

226 jetole January 11, 2013 at 12:00 pm

I’ll give you the quick and dirty answer but this is regex which grep relies on so I would make a point of learning that. A good place to start would be man 7 regex or the regex book from O’Reilly.

Anyways, anything between [ and ] match any character between them so [abc] will match a or b or c [a-z] will match any lower case letter, [a-zA-Z0-9. ] will match any letter, number, period or space.
Preceding the match with ^ inverts it so [^abc] will match anything except a or b or c. [^/] says match anything except a forward slash.

+ says match at least one or more occurrences but it needs to be escaped to work otherwise it will match a literal + instead of the regex action. It’s similar to * but * is 0 or more.
A period (.) is a special character in regex that says match any character so if I want to search for a literal period instead of regex for any character I have to escape it with a bask slash.

Combine all of this together and it matches any string preceding “.wav” and the “.wav” that does not contain a forward slash. If there is a forward slash then it starts right after it. Hope this helps.

Reply

227 Rao January 11, 2013 at 12:30 pm

Thanks again…it was a great learning.. :-)

Reply

228 Rao January 11, 2013 at 1:12 pm

@Jetole: Sorry to bother you…when I run the command on single file its working fine.
I have around 700 files to search for. when I searched for multiple files,file name is being prefixed to some of the output strings.

grep -io ‘[^/]\+\.wav’ pw_metricsfile*
O/p:
pw_metricsfile:validityexpiry.wav
mon-01.wav
day-09.wav
year-2014.wav
promoBalance.wav
1.wav
Thousand.wav
9.wav
Hundred.wav
73.wav
Rupees.wav
and.wav
91.wav
Paise.wav
roamingAnnc.wav
pw_metricsfile:bundlewithoutcustcare.wav
pw_metricsfile:WelcomePrompt.wav
pw_metricsfile:YourBalanceTalkTime.wav
1.wav
Thousand.wav
9.wav
Hundred.wav
85.wav
Rupees.wav
and.wav
46.wav
Paise.wav
pw_metricsfile:plzenterbundleno.wav
pw_metricsfile:YouHaveEntered.wav

You might have observed that file name “pw_metricsfile” is being prefixed to output.
I managed by piping grep -v “pw_metricsfile” ,but just wanted to check is there some ways we still need to modify the command to overcome this issue ??

Reply

229 jetole January 11, 2013 at 1:16 pm

I’m on my phone at the moment so all I can say is run man grep. I know the option is there but don’t remember what it is.

Reply

230 Jan January 11, 2013 at 10:51 pm

Rao:
assuming you don’t want that preceding filenames:

from ‘man grep’:
-h, –no-filename
Suppress the prefixing of file names on output. This is the
default when there is only one file (or only standard input) to
search.

Reply

231 Rao January 15, 2013 at 5:19 am

Sorry for the late reply as it was a local holiday here….
Thanks again…with “-h” option its working perfect ….kudos to you :-)

Reply

Leave a Comment

Name *

E-mail *

Website

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <kbd> <blockquote> <pre> <a href="" title="">

Notify me of followup comments via e-mail




Tagged as: command to search a word in all files unix, egrep, file search, grep, grep command, grep command example, grep command howto, grep command in linux, grep command usage, grep howto, grep in linux, grep linux, grep unix, how to use grep command in unix, Linux, linux grep, search file, search recursively, UNIX, unix grep

Previous Faq: Linux: Openssh (ssh server) deny root user access

Next Faq: FreeBSD install Perl language
GET FREE TIPS
Make the most of Linux Sysadmin work!


Youtube | Twitter | Google +



Related Faqs
Fig.01: Linux / UNIX Archive ManagerList the contents of a tar.bz2
Bash Find All File Names With White SpacesBash Find All File Names With White Spaces
Grep Words From a FileGrep Words From a File
Linux / UNIX View Only Configuration File Directives ( uncommented lines of a config file )Linux / UNIX View Only Configuration File Directives ( uncommented lines of a config file )
Solaris UNIX Case-Insensitive Find File Search Solaris UNIX Case-Insensitive Find File Search
grep in Bashgrep in Bash
Fig.01: grep command displaying searched patternHow To Find Files by Content Under UNIX
Unix / Linux: grep Word Count CommandUnix / Linux: grep Word Count Command
Linux / Unix: Find All The Files Owned By a Particular User / GroupLinux / Unix: Find All The Files Owned By a Particular User / Group
Linux / UNIX Recursively Search All Files For A StringLinux / UNIX Recursively Search All Files For A String

©2006-2013 niCraft. All rights reserved. Cannot be reproduced without written permission.
Privacy Policy | Terms of Service | Questions or Comments | Sitemap
Re: simpletest vs phpunit vs ... [message #180536 is a reply to message #180515] Sun, 24 February 2013 23:49 Go to previous messageGo to next message
legalize+jeeves is currently offline  legalize+jeeves
Messages: 21
Registered: September 2010
Karma: 0
Junior Member
[Please do not mail me a copy of your followup]

Jerry Stuckle <jstucklex(at)attglobal(dot)net> spake the secret code
<kg93pg$1to$1(at)dont-email(dot)me> thusly:

> On 2/22/2013 7:39 PM, Richard wrote:
>> [Please do not mail me a copy of your followup]
>>
>> Anders Wegge Keller <wegge(at)wegge(dot)dk> spake the secret code
>> <87ehg8ozce(dot)fsf(at)huddi(dot)jernurt(dot)dk> thusly:
>>
>>> If on the other hand, you had been working in a project organization,
>>> like I do, you would have known that there isn't enough time to
>>> refactor all 600.000 lines of code[1], for each of the 600 projects
>>> that has 92-96% of the code in common.
>>
>> This is a straw-man argument, because I didn't assert any such thing.
>
> It's a straw-man argument to you, because you refuse to admit he's right.

Admitting he is right is irrelevant because I didn't tell him to
refactor 600,000 lines of code.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
Re: simpletest vs phpunit vs ... [message #180537 is a reply to message #180520] Mon, 25 February 2013 00:03 Go to previous messageGo to next message
legalize+jeeves is currently offline  legalize+jeeves
Messages: 21
Registered: September 2010
Karma: 0
Junior Member
[Please do not mail me a copy of your followup]

Anders Wegge Keller <wegge(at)wegge(dot)dk> spake the secret code
<87wqtzo752(dot)fsf(at)huddi(dot)jernurt(dot)dk> thusly:

> legalize+jeeves(at)mail(dot)xmission(dot)com (Richard) writes:
>
>> [Please do not mail me a copy of your followup]
>>
>> Anders Wegge Keller <wegge(at)wegge(dot)dk> spake the secret code
>> <87ehg8ozce(dot)fsf(at)huddi(dot)jernurt(dot)dk> thusly:
>>
>>> If on the other hand, you had been working in a project organization,
>>> like I do, you would have known that there isn't enough time to
>>> refactor all 600.000 lines of code[1], for each of the 600 projects
>>> that has 92-96% of the code in common.
>>
>> This is a straw-man argument, because I didn't assert any such thing.
>
> Didn't you say that refactoring could eliminate the need for
> comments?

Which is not the same thing as me telling you to go refactor half a
million lines of code. Where did I tell you to do that? Nowhere.

I also did not say that refactoring eliminates the *need* for comments.
In fact, I specifically stated an example where comments *are* needed
(i.e. bibliographic citation of an algorithm).

So yes, making these assertions is indeed a straw-man argument because
you're arguing against a point that I did not make and specifically
stated the opposite of the point you are arguing against.

> But that aside, I only did mention this example, because this is the
> reality I have to work with. And although I'm in favor of writing easy
> understood code, I also know that it becomes impractical at codebases
> this large.

There's a difference between commenting every block of code and
providing documentation in a very large codebase.

Very large code bases have their own set of problems that arise simply
due to the scale of the project. In a very large code base, you need
some kind of documentation and such documentation often takes the form
of doxygen (or some other system) style comments that can be processed
to produce documentation directly from the source code.

If you read the references I've been citing they do not say that comments
per se are bad. I have not said comments are bad. However, inside a
method or function implementation if I have a small block of code,
which people tend to offset from neighbouring blocks by blank lines,
and I feel that this block of code needs a comment to explain what
it's doing, then chances are that block of code has details that are
best moved off into a separate method or function and what we need in
place of this comment is simply a call to a well named method.

Note that the above statement does not define an axiomatic MUST ALWAYS
process, but employs human judgment in deciding when to extract method
or not. That is why such indications are referred to as "code smells"
and not "code errors".

If my entire point of view in this thread seems alien to you, then I
suggest you read "Refactoring" by Martin Fowler and/or "Refactoring to
Patterns" by Joshua Kierievsky.
< http://www.amazon.com/Refactoring-Improving-Design-Existing-Code/dp/0201485 672>
<http://www.amazon.com/Refactoring-Patterns-Joshua-Kerievsky/dp/0321213351>

If you don't like reading books, go find some Smalltalk guys and talk
to them about how refactoring.

Seriously, these ideas are not new (Refactoring is almost 15 years in
print and the idea was not new when it came out), they are not mine, and
they are pretty pervasive in the software engineering industry.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
Re: simpletest vs phpunit vs ... [message #180538 is a reply to message #180536] Mon, 25 February 2013 01:17 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/24/2013 6:49 PM, Richard wrote:
> [Please do not mail me a copy of your followup]
>
> Jerry Stuckle <jstucklex(at)attglobal(dot)net> spake the secret code
> <kg93pg$1to$1(at)dont-email(dot)me> thusly:
>
>> On 2/22/2013 7:39 PM, Richard wrote:
>>> [Please do not mail me a copy of your followup]
>>>
>>> Anders Wegge Keller <wegge(at)wegge(dot)dk> spake the secret code
>>> <87ehg8ozce(dot)fsf(at)huddi(dot)jernurt(dot)dk> thusly:
>>>
>>>> If on the other hand, you had been working in a project organization,
>>>> like I do, you would have known that there isn't enough time to
>>>> refactor all 600.000 lines of code[1], for each of the 600 projects
>>>> that has 92-96% of the code in common.
>>>
>>> This is a straw-man argument, because I didn't assert any such thing.
>>
>> It's a straw-man argument to you, because you refuse to admit he's right.
>
> Admitting he is right is irrelevant because I didn't tell him to
> refactor 600,000 lines of code.
>

No, you didn't. However, it's a real fact in programming - if you work
on more than toy web sites, anyway.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: simpletest vs phpunit vs ... [message #180543 is a reply to message #180537] Mon, 25 February 2013 18:28 Go to previous message
Anders Wegge Keller is currently offline  Anders Wegge Keller
Messages: 30
Registered: May 2012
Karma: 0
Member
legalize+jeeves(at)mail(dot)xmission(dot)com (Richard) writes:

> [Please do not mail me a copy of your followup]

Talking about strawmen-arguiments :)

> Anders Wegge Keller <wegge(at)wegge(dot)dk> spake the secret code
> <87wqtzo752(dot)fsf(at)huddi(dot)jernurt(dot)dk> thusly:

>> Didn't you say that refactoring could eliminate the need for
>> comments?
>
> Which is not the same thing as me telling you to go refactor half a
> million lines of code. Where did I tell you to do that? Nowhere.

You made a blanket statement about refactoring being the replacement
for comments. I provided you with a real-world situation, where
refactoring is not a practical option in my world view. At that point,
you could have augmented your original position with a limitation to
its usefullness. That would have provided a starting point for a
discussion about the practical limits of different philosophies in
that regard.

So instead of riling yourself up, please tell me what you consider an
upper limit wrt. code complexity and SLOC for refactoring.

And at the same time, let me add another concern about refactoring
code. This also stems from my work in a project-oriented
organization. On average, 92-93% of the code is identical with the
base code. It's not the same parts that are identical in the
individual projects. From time to time[1] we find a bug in the base
code. The SOP in this case is to test it throughly in the base code,
and then patch the difference onto each project derived from that
base. This is normally an easy operation, as the code in most cases
look identical. But if we had refactored each project agressively, we
would be in a bad situation, when applying a fix from the base
software. I already mentioned a guess about your experience being in
product development, where you are not bound by these concernes in the
same way, but I'm curious if I'm right or wrong?


1. Mostly whenever we are forced onto faster hardware. Then we always
seem to find a new race condition, that have been hiding for 25
years.

--
/Wegge

Leder efter redundant peering af dk.*,linux.debian.*
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: unset multidimensional array element
Next Topic: php url question
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 06:53:04 GMT 2024

Total time taken to generate the page: 0.02775 seconds