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

asked on

fgrep vs grep

i,

i am going through below link

https://www.petefreitag.com/item/426.cfm
when to use fgrep and when to use grep ?
are there any advantages, disadvantages on over other?
please advise
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi. fgrep (as in "fixed" grep) is used for finding a specific exact string in a file, e.g. if you wanted to find the string "Simon" (with a capital S and lower-case "imon") in a file called file.txt, then use
fgrep Simon file.txt

Open in new window


grep (Global Regular Expression and Print, it is claimed) allows you to use a regular expression to allow for variations in the string. For example, if you wanted to find "Simon" or "simon", followed by any number of spaces or commas, followed by a digit from 0 to 9, you could use
grep '[Ss]imon[ ,]*[0-9]' file.txt

Open in new window


There is even an "egrep" (extended grep) which allows more complex regular expressions, including alternative strings. To find lines with "Simon" or "George" in the file,
egrep 'Simon|George' file.txt

Open in new window


Just to confuse, you can simulate fgrep by calling "grep" with the "-F" flag, and "egrep" by calling "grep" with the "-E" flag.
Avatar of gudii9

ASKER

interesting let me try them
https://unix.stackexchange.com/questions/17949/what-is-the-difference-between-grep-egrep-and-fgrep gives a good explanation.

The big three - grep + egrep + fgrep - all started as separate code bases... long ago... in the mists of time...

Now they're all common code, so egrep + fgrep are simply grep with a few hardcoded options/arguments/flags.

The all run at the same speed + accomplish the same task.
Avatar of gudii9

ASKER

grep -C20 -E "Exception occured during decryption" crm_1.log.2017101810
above and below almost giving same results(do flag goes before -C20 or after ..does -C20 show call stack trace...there is no other command for finding call stack trace?)
 grep -C20 -F "Exception occured during decryption" crm_1.log.2017101810




egrep 'Simon|George' file.txt

does it check Simon and George side by side or each can be any where in the file?(like simon at line 121 and george at 345 line at different timestamp how to distinguish them in results?
egrep 'Simon|George' file.txt
Looks for lines containing “Simon” or “George”
fgrep 'Simon|George' file.txt
Looks for lines containing “Simon” and “George” side by side with “|” in between them

Since  an expression like "Exception occured during decryption" contains no regular expression meta characters, like . * | [ ]
-E or -F should make no difference to how it is interpreted
Avatar of gudii9

ASKER

egrep 'Simon|George' file.txt
Looks for lines containing “Simon” or “George”
fgrep 'Simon|George' file.txt
Looks for lines containing “Simon” and “George” side by side with “|” in between them

how to search Simon and George both in different lines? is that is possible?
SOLUTION
Avatar of ozo
ozo
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
Avatar of gudii9

ASKER

egrep 'Simon|George' file.txt
Looks for lines containing either “Simon” or “George”
printing both lines containing “Simon” and lines containing “George
if say both Simon and George are in line 111
does above command shows line 111?
grep -l George `grep -l Simon *`
looks for files containing both "Simon" and "George"
why we need * and single quote in above command?
if say both Simon and George are in line 111
does above command shows line 111?
Yes

why we need * and single quote in above command?

* globs all filenames  (other than filenames starting with ".")
grep -l Simon *
searches through * files to find the ones containing "Simon" and prints the names of those files
grep -l George `grep -l Simon *` searches through those files to find the ones containing "George"
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

why we need * and single quote in above command?

* globs all filenames  (other than filenames starting with ".")
grep -l Simon *
what you mean by globs?

so we do not need single quote at the end right

grep -l George `grep -l Simon *`
instead it should be as below right?
grep -l George `grep -l Simon *
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
ASKER CERTIFIED 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
The question author has not seen fit to respond, so we have to assume has has found the answers to be satisfactory