Link to home
Start Free TrialLog in
Avatar of guochu
guochu

asked on

How to use grep command?

I want to search patterns of this string ->  "if example1::example2().exxample3(ok)" by using grep and want the output results only display as below -

example2()
example3(ok)

Is it possible to do it?

Avatar of MikeOM_DBA
MikeOM_DBA
Flag of United States of America image


grep 'example' <TheFile.txt


PS: man grep
Avatar of ozo
tr ' :.' '\n' < file | grep 'ex*ample.*('
grep command is used to find the lines where a particular string is present in a file/dir
Eg: to find if example1::example2().exxample3(ok), write the command as

grep -n "if example1::example2().exxample3(ok)" *

or

grep -n "if example1::example2().exxample3(0k)*" *

This command will display the lines with line numbers and file names, where it is written.
grep is having many flags, for line numbers, for case sentiveness, etc. but in your case where you want to file a string the above will work.

Regards
Prashant Sabnekar
ASKER CERTIFIED SOLUTION
Avatar of ahoffmann
ahoffmann
Flag of Germany 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
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 mightyone
mightyone

still think awk is shorter and better:

echo "example1::example2().example3(ok)" | awk -F:: '{ print $2 }'|awk -F. '{print $1"\n" $2 }'
Sorry to disagree with you mightyone, but by my reckoning your solution is much slower and not correct. My reasons:

1. sed uses fast regular expression parsing, which uses a pre-built state machine to process characters from the text. awk must first parse lines, then must execute the interpretive language. But worst of all you're filtering the input through TWO SEPARATE awk processes, with two pipes. This is unnecessary.

2. Your solution does nothing to match only lines that fit the stated syntax, but will instead process every line, generating much undesired output.

3. I'm sure it's possible to write a correct solution using awk, even as a single process. But it would certainly be longer and more complicated than my sed solution. Why is this important anyway? Are we counting bytes here? Does it matter if it's 52 characters versus 71 characters?

The Unix philosophy is to provide a toolbox in which you can pick the right tool for the job. In my opinion, sed is certainly the right tool for this job.

But in the end it's up to the questioner to decide what they'd prefer to do.
> 1. sed uses fast regular expression parsing, ..
not sure which sed you mean, but except Gnu's sed all UNIXish sed are much slower than awk, even piped ones, unfortunately :-(
This applies to files of some k-bytes and more, don't know why this is ...

i.g. I agree that sed is the most human readable solution for this problem, but the question was:
  ".. using grep and want the output results only display as below "
and therfore the answer ist simply no, see http:#16350785
lol

than use grep.



@Tim & ahoffman

just make a testcase containing a file with 100 lines of Mike's code

and do this quite couple of time:
 time cat yourfile | grep  'example.*(' | awk -F:: '{ print $2 }'|awk -F. '{print $1 "\n" $2 }'

and
 time sed -n 's/.*if example1::\(example2()\)\.\(exxample3(ok)\).*/\1\\2/p' yourfile

as i do not know sed syntax i didn't get any result for the sed part as there seems to be a problem, but would be interested in the result if you could correct it?
lol

using sed on a small file with only two lines you win

Mighty@mightyone ~
$ time cat ex.txt | grep  'ex*ample.*(' | awk -F:: '{ print $2 }'|awk -F. '{print $1"\n" $2 }'
example2()
example3(ok)

real    0m0.105s
user    0m0.152s
sys     0m0.061s

Mighty@mightyone ~
$ time sed  -n 's/.*example1::\(example2()\)\.\(example3(ok)\).*/\1\\2/p' ex.txt
example2()\2

real    0m0.024s
user    0m0.030s
sys     0m0.015s

*********************************************************************************
using awk on bigger files i win

$ time sed  -n 's/.*example1::\(example2()\)\.\(example3(ok)\).*/\1\\2/p' ex.txt
example2()\2
example2()\2

real    0m12.346s
user    0m12.171s
sys     0m0.093s

Mighty@mightyone ~
$ time cat ex.txt | grep  'ex*ample.*(' | awk -F:: '{ print $2 }'|awk -F. '{print $1"\n" $2 }'
example2()
example3(ok)
example2()
example3(ok)

real    0m0.752s
user    0m0.277s
sys     0m0.528s


mightyone
.-)
mightyone, you just prooved that I win, see http:#16398264
**
not sure which sed you mean, but except Gnu's sed all UNIXish sed are much slower than awk, even piped ones,
**

or is you time's unit "s" somthing like 1/seconds ;-))
and it would be even better without the useless use of cat ...
hm i would appreciate the points as ai had most effort....
Tim deserves some even though his approach was a bit ....

and hoffman always has something good to say


.....

MightyOne