|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
help with grep
Is there an easy way to consolidate the 2 grep parts of the following bash command into a single grep command?:
cat MyFile | grep 'Blah Blah' | grep xyz$ I'd like to not use the pipe re-direct, but instead tell grep, in one command, to grab any lines in 'MyFile' that meet both of these criteria: 1) contain the string 'Blah Blah' (with the space) AND 2) end with the string 'xyz.' I know you can use the '|' (pipe) command to separate 'or' statements in grep, but is there an 'and' statement separator? Thanks! |
|
#2
|
|||
|
|||
|
WE can not having an option on grep to use AND there.
But your way of try as, cat MyFile | grep 'Blah Blah' | grep xyz$ is not efficient. cat <filename> | grep ... will take more time to do operation. We can simply this as, grep -E 'blah blah' filename | grep -E 'xyz$' or grep -E 'blah blah' < filename | grep -E 'xyz$' We can use awk for this too as, awk ' /blah blah/ { print $0 }' <inputfile | awk ' /xyz$/ { print $0 }' HTH. Regards Muthukumar. |
|
#3
|
||||
|
||||
|
Quote:
Unfortunately grep only takes on pattern for its options so you would need to use the pipe no matter what. What's wrong with pipe? Something against it? ![]() Regards, jlk |
|
#4
|
|||
|
|||
|
nope - just thought maybe there was a cleaner way to do it. Thanks for the reply.
Quote:
|
|
#5
|
|||
|
|||
|
why not use the rigth tool?
instead of the s....d cat MyFile | grep 'Blah Blah' | grep xyz$ try sed -n '/.*Blah Blah.*xyz$/p' MyFile |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > help with grep |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|