We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


How to use grep command?

guochu asked
Medium Priority
Last Modified: 2008-01-09
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 -


Is it possible to do it?

Watch Question

grep 'example' <TheFile.txt

PS: man grep
Most Valuable Expert 2014
Top Expert 2015

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)" *


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.

Prashant Sabnekar
> Is it possible to do it?
no (not with grep)

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
use awk loop with separator . and then reloop with separato :: and output
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\

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

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

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 }'

 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?

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 }'

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

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

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

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 }'

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

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


Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.