| Author |
grepping two patterns using &&
|
|
| thecoolone 2006-12-18, 1:20 pm |
| I want to analyze a file to find two things. (i.e NEW and QUE)
I want to get the number of lines that contain both these words.
I tried doing $grep -ec "NEW && QUE" document.txt
and $grep "NEW &&
QUE" document.txt
but grep doesnt give any output. it just gets stuck.
So is there a way of using && or & in grep in the same way how we use |
or || ?
| |
| Ed Morton 2006-12-18, 1:20 pm |
| thecoolone wrote:
> I want to analyze a file to find two things. (i.e NEW and QUE)
> I want to get the number of lines that contain both these words.
> I tried doing $grep -ec "NEW && QUE" document.txt
> and $grep "NEW &&
> QUE" document.txt
>
> but grep doesnt give any output. it just gets stuck.
> So is there a way of using && or & in grep in the same way how we use |
> or || ?
>
grep doesn't support "&&". What you're doing above is running grep in
the background on no input. Use awk instead.
This will print the lines:
awk '/NEW/ && /QUE/' document.txt
This will print the number of lines:
awk '/NEW/ && /QUE/{ count++ } END{ print count }' document.txt
This will print the lines plus a count:
awk '/NEW/ && /QUE/{ print $0; count++ } END{ print count }' document.txt
Regards,
Ed.
| |
| thecoolone 2006-12-18, 1:20 pm |
|
Ed Morton wrote:
> thecoolone wrote:
>
> grep doesn't support "&&". What you're doing above is running grep in
> the background on no input. Use awk instead.
>
> This will print the lines:
>
> awk '/NEW/ && /QUE/' document.txt
>
> This will print the number of lines:
>
> awk '/NEW/ && /QUE/{ count++ } END{ print count }' document.txt
>
> This will print the lines plus a count:
>
> awk '/NEW/ && /QUE/{ print $0; count++ } END{ print count }' document.txt
>
Thank you.
How to use special characters inside the search pattern??
Example i am trying to search for "17/Dec" and ".pdf". i tried to
search it as
awk '/17//Dec/ && /.pdf/' document.txt | wc -l
I get the following error:
"awk: cmd. line:1: /17//Dec/ && /.pdf/
awk: cmd. line:1: ^ syntax error"
| |
| Ed Morton 2006-12-18, 1:20 pm |
| thecoolone wrote:
> Ed Morton wrote:
>
>
>
> Thank you.
> How to use special characters inside the search pattern??
Escape them by preceeding them with a backslash (\).
> Example i am trying to search for "17/Dec" and ".pdf". i tried to
> search it as
> awk '/17//Dec/ && /.pdf/' document.txt | wc -l
You don't need "wc -l", see above and below.
> I get the following error:
> "awk: cmd. line:1: /17//Dec/ && /.pdf/
> awk: cmd. line:1: ^ syntax error"
awk '/17\/Dec/ && /.pdf/{ count++ } END{ print count }' document.txt
Regards,
Ed.
| |
| Random832 2006-12-18, 1:20 pm |
| 2006-12-18 <1166453777.398225.144880@79g2000cws.googlegroups.com>,
thecoolone wrote:
>
> Ed Morton wrote:
>
> Thank you.
> How to use special characters inside the search pattern??
> Example i am trying to search for "17/Dec" and ".pdf". i tried to
> search it as
> awk '/17//Dec/ && /.pdf/' document.txt | wc -l
> I get the following error:
> "awk: cmd. line:1: /17//Dec/ && /.pdf/
> awk: cmd. line:1: ^ syntax error"
You want \/ not //
| |
| Kaz Kylheku 2006-12-18, 1:20 pm |
| thecoolone wrote:
> I want to analyze a file to find two things. (i.e NEW and QUE)
> I want to get the number of lines that contain both these words.
> I tried doing $grep -ec "NEW && QUE" document.txt
> and $grep "NEW &&
> QUE" document.txt
>
> but grep doesnt give any output. it just gets stuck.
> So is there a way of using && or & in grep in the same way how we use |
> or || ?
A line which contains NEW and QUE can do so in two ways: NEW can come
first or QUE. So you have to match two different patterns with an OR:
grep "NEW.*QUE|QUE.*NEW"
| |
| thecoolone 2006-12-18, 7:21 pm |
|
Random832 wrote:
> 2006-12-18 <1166453777.398225.144880@79g2000cws.googlegroups.com>,
> thecoolone wrote:
>
> You want \/ not //
what does V stand for??
| |
| Random832 2006-12-18, 7:21 pm |
| 2006-12-18 <1166473912.363661.80040@n67g2000cwd.googlegroups.com>,
thecoolone wrote:
>
> Random832 wrote:
>
> what does V stand for??
Backslash, slash. "\" followed by "/"
Get a better font.
| |
| Scott McMillan 2006-12-18, 7:21 pm |
| On 18 Dec 2006 12:31:52 -0800, "thecoolone" <jahan9@gmail.com> wrote:
>
>Random832 wrote:
>
>what does V stand for??
V stands for "Victory", of course! But that's not a V...
| |
| Ed Morton 2006-12-18, 7:21 pm |
| Kaz Kylheku wrote:
> thecoolone wrote:
>
>
>
> A line which contains NEW and QUE can do so in two ways: NEW can come
> first or QUE. So you have to match two different patterns with an OR:
>
> grep "NEW.*QUE|QUE.*NEW"
>
No:
$ echo "NEW QUE" | grep "NEW.*QUE|QUE.*NEW"
$
Ed.
| |
| Kaz Kylheku 2006-12-19, 1:30 am |
| Ed Morton wrote:
> Kaz Kylheku wrote:
>
> No:
>
> $ echo "NEW QUE" | grep "NEW.*QUE|QUE.*NEW"
Oops! That's an extended regular expression, requiring -E.
Also, of course, there is always:: grep NEW | grep QUE.
| |
| thecoolone 2006-12-19, 1:30 am |
|
Kaz Kylheku wrote:
> Ed Morton wrote:
>
> Oops! That's an extended regular expression, requiring -E.
>
> Also, of course, there is always:: grep NEW | grep QUE.
Thank you all for your replies. I can find what i am looking for now 
| |
| Michal Nazarewicz 2006-12-19, 1:22 pm |
| >> Kaz Kylheku wrote:
[vbcol=seagreen]
> Ed Morton wrote:
"Kaz Kylheku" <kkylheku@gmail.com> writes:[vbcol=seagreen]
> Oops! That's an extended regular expression, requiring -E.
Or you can just precede pipe character with a slash (which may be
handy if one ever decide to use that pattern in sed):
#v+
$ echo "NEW QUE" | grep 'NEW.*QUE\|QUE.*NEW'
NEW QUE
#v-
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
| |
| Stephane CHAZELAS 2006-12-19, 1:22 pm |
| 2006-12-19, 17:19(+01), Michal Nazarewicz:
>
>
> "Kaz Kylheku" <kkylheku@gmail.com> writes:
>
> Or you can just precede pipe character with a slash (which may be
> handy if one ever decide to use that pattern in sed):
>
> #v+
> $ echo "NEW QUE" | grep 'NEW.*QUE\|QUE.*NEW'
> NEW QUE
> #v-
That's GNU, not Unix. Note that "\" is backslash. slash usually
refers to forwardslash (/).
grep -e 'NEW.*QUE' -e 'QUE.*NEW'
is Unix and GNU.
--
Stéphane
| |
| Kaz Kylheku 2006-12-19, 1:22 pm |
| Michal Nazarewicz wrote:
>
>
> "Kaz Kylheku" <kkylheku@gmail.com> writes:
>
> Or you can just precede pipe character with a slash (which may be
The Single Unix Specification says that in an BRE (Basic Regular
Expression), a backslash can be used to escape the meta-characters in
order to suppress their meaning. However, if any other character is
backslashed, that is undefined behavior.
GNU grep takes advantage of this to provide a the conforming behavior
extension that you are describing.
> handy if one ever decide to use that pattern in sed):
Support for ERE (Extended Regular Expressions) in sed is also a GNU
extension; it is enabled by a command line option. So you can also use
a pattern from grep -E in GNU sed that way.
|
|
|
|