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?

guochuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MikeOM_DBACommented:

grep 'example' <TheFile.txt


PS: man grep
0
ozoCommented:
tr ' :.' '\n' < file | grep 'ex*ample.*('
0
Prashant SabnekarAVPCommented:
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
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

ahoffmannCommented:
> Is it possible to do it?
no (not with grep)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mightyoneCommented:
use awk loop with separator . and then reloop with separato :: and output
0
TimEliseoCommented:
This can't be done with with any grep variants directly, but it is easily accomplished with sed. The following simple script will do what you want. Paste this into a file and set it to executable.

#! /bin/sed -n -f
s/.*if example1::\(example2()\)\.\(exxample3(ok)\).*/\1\
\2/p

You could also so this on the command line of Bourne-derived shells (sh, ksh, bash, zsh; csh-derived shells can't do this because you can't enter commands with embedded newlines) or as part of a larger script:

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

0
mightyoneCommented:
still think awk is shorter and better:

echo "example1::example2().example3(ok)" | awk -F:: '{ print $2 }'|awk -F. '{print $1"\n" $2 }'
0
TimEliseoCommented:
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.
0
ahoffmannCommented:
> 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
0
mightyoneCommented:
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?
0
mightyoneCommented:
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
.-)
0
ahoffmannCommented:
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 ...
0
mightyoneCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.