• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3557
  • Last Modified:

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?

0
guochu
Asked:
guochu
  • 5
  • 3
  • 2
  • +3
3 Solutions
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ahoffmannCommented:
> Is it possible to do it?
no (not with grep)
0
 
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

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

  • 5
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now