cat file_name | grep "string_A|string_B" does not work

cat file_name | grep "string_A|string_B" does not work

am 11.01.2008 16:07:54 von chen_zhitao

Sometimes I need to find two different strings in a file with one
command. For example, I need to list the lines in the file "file_name"
which has either "string_A" or "string_B". The 2 strings are totally
unrelated, so wildcard "*" is not applicable in this case. I need to
keep the correct sequence of these lines, so that separating them into
2 commands cannot meet my request. How to do that? I tried the
following command:
cat file_name | grep "string_A|string_B"
But it does not work. How to achieve this purpose? Thanks.

Re: cat file_name | grep "string_A|string_B" does not work

am 11.01.2008 16:17:45 von Ed Morton

On 1/11/2008 9:07 AM, chen_zhitao@yahoo.com wrote:
> Sometimes I need to find two different strings in a file with one
> command. For example, I need to list the lines in the file "file_name"
> which has either "string_A" or "string_B". The 2 strings are totally
> unrelated, so wildcard "*" is not applicable in this case. I need to
> keep the correct sequence of these lines, so that separating them into
> 2 commands cannot meet my request. How to do that? I tried the
> following command:
> cat file_name | grep "string_A|string_B"
> But it does not work. How to achieve this purpose? Thanks.

awk '/string_A/ && /string_B/' file_name

If you encounter problems, use GNU awk (gawk), new awk (nawk) or
/usr/xpg4/bin/awk on Solaris.

Ed.

Re: cat file_name | grep "string_A|string_B" does not work

am 11.01.2008 16:43:01 von PK

chen_zhitao@yahoo.com wrote:

> Sometimes I need to find two different strings in a file with one
> command. For example, I need to list the lines in the file "file_name"
> which has either "string_A" or "string_B". The 2 strings are totally
> unrelated, so wildcard "*" is not applicable in this case. I need to
> keep the correct sequence of these lines, so that separating them into
> 2 commands cannot meet my request. How to do that? I tried the
> following command:
> cat file_name | grep "string_A|string_B"

I think you need to escape the |, ie

cat file_name | grep "string_A\|string_B"

since grep does not treat | as a special character otherwise.

Re: cat file_name | grep "string_A|string_B" does not work

am 11.01.2008 16:45:32 von OldSchool

On Jan 11, 10:07=A0am, chen_zhi...@yahoo.com wrote:
> Sometimes I need to find two different strings in a file with one
> command. For example, I need to list the lines in the file "file_name"
> which has either "string_A" or =A0"string_B". The 2 strings are totally
> unrelated, so wildcard "*" is not applicable in this case. I need to
> keep the correct sequence of these lines, so that separating them into
> 2 commands cannot meet my request. How to do that? I tried the
> following command:
> =A0 =A0 =A0 =A0 cat file_name | grep "string_A|string_B"
> But it does not work. How to achieve this purpose? Thanks.

depending upon OS, you will need to used either egrep, or the -E
switch of grep, as in
grep -E "string_A|string_B"

you should see notes regarding ERE's or Extended Regular Expression in
the man page for same

Re: cat file_name | grep "string_A|string_B" does not work

am 11.01.2008 16:47:13 von mik3l3374

On Jan 11, 11:07 pm, chen_zhi...@yahoo.com wrote:
> Sometimes I need to find two different strings in a file with one
> command. For example, I need to list the lines in the file "file_name"
> which has either "string_A" or "string_B". The 2 strings are totally
> unrelated, so wildcard "*" is not applicable in this case. I need to
> keep the correct sequence of these lines, so that separating them into
> 2 commands cannot meet my request. How to do that? I tried the
> following command:
> cat file_name | grep "string_A|string_B"
> But it does not work. How to achieve this purpose? Thanks.

grep "string_A" file | grep "string_B" ??

Re: cat file_name | grep "string_A|string_B" does not work

am 11.01.2008 16:48:56 von wayne

chen_zhitao@yahoo.com wrote:
> Sometimes I need to find two different strings in a file with one
> command. For example, I need to list the lines in the file "file_name"
> which has either "string_A" or "string_B". The 2 strings are totally
> unrelated, so wildcard "*" is not applicable in this case. I need to
> keep the correct sequence of these lines, so that separating them into
> 2 commands cannot meet my request. How to do that? I tried the
> following command:
> cat file_name | grep "string_A|string_B"
> But it does not work. How to achieve this purpose? Thanks.

Standard grep uses 'BREs' (Basic Regular Expressions). Those don't
support the '|' to mean alternation. For that you need EREs, provided
by egrep or "grep -E".

If you are matching literal strings the -F option is more efficient
(which means nothing on today's computers unless the files are
really, really large):
grep -F 'string_A
string_B' file

(i.e., The different strings are separated with by newlines.)

-Wayne

Re: cat file_name | grep "string_A|string_B" does not work

am 11.01.2008 17:10:49 von PK

mik3l3374@gmail.com wrote:

> On Jan 11, 11:07 pm, chen_zhi...@yahoo.com wrote:
>> Sometimes I need to find two different strings in a file with one
>> command. For example, I need to list the lines in the file "file_name"
>> which has either "string_A" or "string_B". The 2 strings are totally
>> unrelated, so wildcard "*" is not applicable in this case. I need to
>> keep the correct sequence of these lines, so that separating them into
>> 2 commands cannot meet my request. How to do that? I tried the
>> following command:
>> cat file_name | grep "string_A|string_B"
>> But it does not work. How to achieve this purpose? Thanks.
>
> grep "string_A" file | grep "string_B" ??

Read what he said:

"I need to keep the correct sequence of these lines, so that separating them
into 2 commands cannot meet my request"

(and, btw, your suggestion produces an even different result)

Re: cat file_name | grep "string_A|string_B" does not work

am 11.01.2008 17:48:58 von Cyrus Kriticos

chen_zhitao@yahoo.com wrote:
> Sometimes I need to find two different strings in a file with one
> command. For example, I need to list the lines in the file "file_name"
> which has either "string_A" or "string_B". The 2 strings are totally
> unrelated, so wildcard "*" is not applicable in this case. I need to
> keep the correct sequence of these lines, so that separating them into
> 2 commands cannot meet my request. How to do that? I tried the
> following command:
> cat file_name | grep "string_A|string_B"
> But it does not work. How to achieve this purpose? Thanks.

grep -e "string_A" -e "string_B" file_name

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.

Re: cat file_name | grep "string_A|string_B" does not work

am 11.01.2008 22:10:31 von Ed Morton

On 1/11/2008 9:17 AM, Ed Morton wrote:
>
> On 1/11/2008 9:07 AM, chen_zhitao@yahoo.com wrote:
>
>>Sometimes I need to find two different strings in a file with one
>>command. For example, I need to list the lines in the file "file_name"
>>which has either "string_A" or "string_B". The 2 strings are totally
>>unrelated, so wildcard "*" is not applicable in this case. I need to
>>keep the correct sequence of these lines, so that separating them into
>>2 commands cannot meet my request. How to do that? I tried the
>>following command:
>> cat file_name | grep "string_A|string_B"
>>But it does not work. How to achieve this purpose? Thanks.
>
>
> awk '/string_A/ && /string_B/' file_name

Oops:

awk '/string_A/ || /string_B/' file_name

> If you encounter problems, use GNU awk (gawk), new awk (nawk) or
> /usr/xpg4/bin/awk on Solaris.
>
> Ed.
>

Re: cat file_name | grep "string_A|string_B" does not work

am 11.01.2008 22:48:03 von mallin.shetland

chen_zhitao@yahoo.com scrisse:

> ... I need to find two different strings in a file
> with one command.
> ...

1) You don'y need cat
2) You have many choices.

$ grep -E 'string_A|string_B' file
$ egrep 'string_A|string_B' file
$ grep 'string_A\|string_B' file
$ grep -G 'string_A\|string_B' file
$ grep -e 'string_A' -e 'string_B' file
$ grep -G 'string_A\|string_B' file
$ grep -F \
'string_A
string_B' \
file
$ fgrep 'string_A
string_B' file

and so on.

man grep

Re: cat file_name | grep "string_A|string_B" does not work

am 12.01.2008 16:28:22 von Stephane CHAZELAS

On Fri, 11 Jan 2008 22:48:03 +0100, mallin.shetland wrote:
> chen_zhitao@yahoo.com scrisse:
>
>> ... I need to find two different strings in a file
>> with one command.
>> ...
[...]
> $ egrep 'string_A|string_B' file
> $ grep 'string_A\|string_B' file
> $ grep -G 'string_A\|string_B' file
> $ fgrep 'string_A
> string_B' file
[...]

Those ones are not standard. -G and \| are GNU extensions.

The one that makes most sense to me would be:

grep -Fe string_A -e string_B

--
Stephane