Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

grep command usage

Hi,

what are the best practices and features, options while using grep.

i am using these days to search in log files, files, directories, content etc

When should i give single quote '  '
like
zgrep 'xyz' aaa.gz


When should i give double quote " "
like
zgrep "xyz" aaa.gz


how to use tail and head etc along with grep to effectively monitor last or first 100 lines etc
please advise
ASKER CERTIFIED SOLUTION
Avatar of Phil Phillips
Phil Phillips
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

 With double quotes, you can put stuff like variables, commands, arithmetic, etc.  Single quotes will just be literal.

how the literal different from variables and commands etc??
Avatar of gudii9

ASKER

VS single quotes:
$ echo '${SHELL}'
${SHELL}

$ echo '$(pwd)'
$(pwd)

$ echo '$((2+2))'
$((2+2))

above not linterals right. they are more like functions?
Single quotes prevents the shell, bash, from processing meaning what you place in single quote is literal.

If you want the items processed use double quotes.

echo "$((2+2))"
You took one step, all you needed is to take the second and try the double quotes.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Understood, though the same resource consumption is in place whether you uncompress|grep ir you use zgrep.
Compress
Search using .....

I
Yes, but it's done in place without having to create a separate visible extracted file to work on.  For the user it looks like a single operation that's done a single pass.
I have provided both option examples; I am trying to see if the possibility/ requirement is such that it might be better to have the data extracted and searchable versus compress/search a compressed file ... If this is done regularly multiple times a day, or as a mechanism of retro review of an event that happened.
The data crunch/searchable could be used proactively to alert when certain event/s occur. Once setup, one could review events retroactively as well as indicate that future such occurances should generate notifications/alerts.
Those new gnu z' tools allow the compressed files to be searchable without actually creating an extracted file on disk, maybe just in /tmp.  They work just fine on plain text as well and are convenient enough to run multiple times a day.  They also take the equivalent options as the corresponding tools that operate on plain text.