Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

piping to fgrep

55 views
Skip to first unread message

jack...@gmail.com

unread,
Apr 10, 2013, 4:34:24 AM4/10/13
to
Is it possible to pipe to fgrep? When I tried, it replies "fgrep: can't open -"

The input file for the fgrep is actually $0. I grep for all the variable assignments in the shell script, cut that part that is left of the equal sign and remove the quotes and then pipe that to fgrep. Is there any way to do this or do I have to write the output to a temp file and then use the temp file as input to fgrep? Thanx.

Janis Papanagnou

unread,
Apr 10, 2013, 6:56:27 AM4/10/13
to
Am 10.04.2013 10:34, schrieb jack...@gmail.com:
> Is it possible to pipe to fgrep? When I tried, it replies "fgrep:
> can't open -"

Yes, it is possible.

some_process | frep some_string

some_process | frep some_string -

>
> The input file for the fgrep is actually $0. I grep for all the
> variable assignments in the shell script, cut that part that is left
> of the equal sign and remove the quotes and then pipe that to fgrep.
> Is there any way to do this or do I have to write the output to a
> temp file and then use the temp file as input to fgrep? Thanx.

Post the relevant parts of your code to let us see what you are doing.

Janis

>

Kenny McCormack

unread,
Apr 10, 2013, 7:16:37 AM4/10/13
to
In article <kk3ggo$l9v$1...@speranza.aioe.org>,
Janis Papanagnou <janis_pa...@hotmail.com> wrote:
>Am 10.04.2013 10:34, schrieb jack...@gmail.com:
>> Is it possible to pipe to fgrep? When I tried, it replies "fgrep:
>> can't open -"
>
>Yes, it is possible.
>
> some_process | frep some_string
>
> some_process | frep some_string -

I think I see where the OP is coming from.

On my system, I get:

frep: Command not found.

--
"Remember when teachers, public employees, Planned Parenthood, NPR and PBS
crashed the stock market, wiped out half of our 401Ks, took trillions in
TARP money, spilled oil in the Gulf of Mexico, gave themselves billions in
bonuses, and paid no taxes? Yeah, me neither."

Janis Papanagnou

unread,
Apr 10, 2013, 8:03:20 AM4/10/13
to
Am 10.04.2013 13:16, schrieb Kenny McCormack:
> In article <kk3ggo$l9v$1...@speranza.aioe.org>,
> Janis Papanagnou <janis_pa...@hotmail.com> wrote:
>> Am 10.04.2013 10:34, schrieb jack...@gmail.com:
>>> Is it possible to pipe to fgrep? When I tried, it replies "fgrep:
>>> can't open -"
>>
>> Yes, it is possible.
>>
>> some_process | frep some_string
>>
>> some_process | frep some_string -
>
> I think I see where the OP is coming from.
>
> On my system, I get:
>
> frep: Command not found.

Oh, that was *my* typo, not the OP's. Hope we understand his real
problem once we saw his code.

Janis

jack...@gmail.com

unread,
Apr 11, 2013, 2:37:35 AM4/11/13
to
This is it:

dol=$
grep '="' $0 |cut -c11- |awk -F\" '{print $1}' |sort -u > /tmp/$$
fgrep -hf /tmp/$$ $file && rm /tmp/$$


Barry Margolin

unread,
Apr 11, 2013, 3:07:14 AM4/11/13
to
In article <f4bb86db-e776-4612...@googlegroups.com>,
Ahh, your original question didn't say you wanted to use a pipe for the
-f option. That's hardly obvious when you just say "pipe to fgrep".

If your shell supports process substitution, you can do:

fgrep -hf <(grep '="' $0 |cut -c11- |awk -F\" '{print $1}' | sort -u)
$file

I suspect you don't need "sort -u" in there -- fgrep should hopefully be
smart enough to remove duplicates from the -f file.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

jack...@gmail.com

unread,
Apr 11, 2013, 3:34:15 AM4/11/13
to
On Thursday, April 11, 2013 12:07:14 AM UTC-7, Barry Margolin wrote:

> Ahh, your original question didn't say you wanted to use a pipe for the
>
> -f option. That's hardly obvious when you just say "pipe to fgrep".

Sorry. I thought that was the only purpose of fgrep. :) Actually, I really don't know what else it is for since it apparently doesn't do most complex patterns.



> If your shell supports process substitution, you can do:
>
> fgrep -hf <(grep '="' $0 |cut -c11- |awk -F\" '{print $1}' | sort -u)


Thanks Barry!

Ed Morton

unread,
Apr 11, 2013, 7:47:39 AM4/11/13
to
On 4/11/2013 2:34 AM, jack...@gmail.com wrote:
> On Thursday, April 11, 2013 12:07:14 AM UTC-7, Barry Margolin wrote:
>
>> Ahh, your original question didn't say you wanted to use a pipe for the
>>
>> -f option. That's hardly obvious when you just say "pipe to fgrep".
>
> Sorry. I thought that was the only purpose of fgrep. :) Actually, I really don't know what else it is for since it apparently doesn't do most complex patterns.

The purpose of fgrep is to NOT do ANY "patterns" (actually regular expressions,
"RE"s). fgrep is used to search for a string in a file instead of an RE in a
file. Look:

$ cat file
a.*b
a-b

$ grep '.*' file
a.*b
a-b

$ fgrep '.*' file
a.*b

See how grep finds all lines in file because it treats '.*' as RE metacharacters
meaning any number of repetitions of any character while fgrep only finds the
lines that contain the string '.*'?

Ed.

Janis Papanagnou

unread,
Apr 11, 2013, 8:00:24 AM4/11/13
to
(Intentionally using shell variable $0 ?)

>
>
> Thanks Barry!
>

You seem to have got your answer. So just one additional remark...

It has been mentioned that sort is unnecessary. You can also omit
the grep and the cut since you're using awk anyway; saves processes
and IPC. Assuming you have data like 12345678="somename"whatever
and want the 'somename' extracted, for example... (untested)

fgrep -hf <(
awk 'match($0,/"[^"]*"/) {print substr($0,RSTART+1,RLENGTH-2)}' "$0"
) "$file"

which picks the first quoted subexpression, or (to better reflect
your original code, but as your original code less robust) done with
sub-strings... (untested)

fgrep -hf <(awk -F\" '/="/ {$0=substr($0,11);print $1}' "$0") "$file"


Janis

Barry Margolin

unread,
Apr 11, 2013, 11:15:51 AM4/11/13
to
In article <kk68kl$ik6$4...@speranza.aioe.org>,
Janis Papanagnou <janis_pa...@hotmail.com> wrote:

> Am 11.04.2013 09:34, schrieb jack...@gmail.com:
> > On Thursday, April 11, 2013 12:07:14 AM UTC-7, Barry Margolin wrote:
> >
> >> Ahh, your original question didn't say you wanted to use a pipe for the
> >>
> >> -f option. That's hardly obvious when you just say "pipe to fgrep".
> >
> > Sorry. I thought that was the only purpose of fgrep. :)
> > Actually, I really don't know what else it is for since it apparently
> > doesn't do most complex patterns.
> >
> >
> >
> >> If your shell supports process substitution, you can do:
> >>
> >> fgrep -hf <(grep '="' $0 |cut -c11- |awk -F\" '{print $1}' | sort -u)
>
> (Intentionally using shell variable $0 ?)

That part came from the OP's code, I didn't really examine it closely, I
just rearranged everything to implement the process substitution.

This code is parsing variable assignments, it seems. I suppose it's
remotely possible that the shell script may be looking for variable
assignments in itself.

Janis Papanagnou

unread,
Apr 11, 2013, 11:49:09 AM4/11/13
to
Am 11.04.2013 17:15, schrieb Barry Margolin:
> In article <kk68kl$ik6$4...@speranza.aioe.org>,
> Janis Papanagnou <janis_pa...@hotmail.com> wrote:
>> Am 11.04.2013 09:34, schrieb jack...@gmail.com:
>>> [...]
>>
>> (Intentionally using shell variable $0 ?)
>
> That part came from the OP's code, I didn't really examine it closely, I
> just rearranged everything to implement the process substitution.

Yes, I know. Please consider my comment to be intended transitive,
meant to address the OP's code. The question was targeted to the OP.

Janis

Ralph Spitzner

unread,
Apr 11, 2013, 4:54:50 PM4/11/13
to
jack...@gmail.com wrote:
[....]
> Sorry. I thought that was the only purpose of fgrep. :) Actually, I really don't know what else it is for since it apparently doesn't do most complex patterns.
>
[....]

cite man fgrep:
In addition, two variant programs egrep and fgrep are available. egrep
is the same as grep -E. fgrep is the same as grep -F.
Direct invocation as either egrep or fgrep is deprecated, but is
provided to allow historical applications that rely on them to
run unmodified.


:-)


Barry Margolin

unread,
Apr 11, 2013, 4:56:33 PM4/11/13
to
In article <kk77pk$q82$1...@dont-email.me>,
That just shifts his question to what the purpose of the -F option is.

Ralph Spitzner

unread,
Apr 12, 2013, 1:29:39 AM4/12/13
to
Barry Margolin wrote:
[...]
>
> That just shifts his question to what the purpose of the -F option is.
>

Well I didn't want to quote the whole manpage, it should be on your
disk somewhere :-)

-----------------------------
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by newlines,
any of which is to be matched. (-F is specified by POSIX.)
----------------------------

-rasp

Barry Margolin

unread,
Apr 12, 2013, 3:34:00 PM4/12/13
to
In article <kk85v0$t25$1...@dont-email.me>,
That explains what it does. He already seemed to know that, he wrote "it
apparently doesn't do most complex patterns.".

That's not the same as its *purpose*, i.e. why there's a need for a
command that does that. He thought the reason was mainly for using the
-f option to match strings in a file. He didn't consider the
possibility that one might want to search for literal strings rather
than regexp patterns, and fgrep means you don't have to escape all the
RE operators.

Ralph Spitzner

unread,
Apr 13, 2013, 6:15:39 AM4/13/13
to
Barry Margolin wrote:
[...]
> That's not the same as its *purpose*, i.e. why there's a need for a
> command that does that. He thought the reason was mainly for using the
> -f option to match strings in a file. He didn't consider the
> possibility that one might want to search for literal strings rather
> than regexp patterns, and fgrep means you don't have to escape all the
> RE operators.
>


Sorry about that, of course he probably wanted :
--------------
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty file contains
zero patterns, and therefore matches nothing. (-f is specified by
POSIX.)
---------------

Sorry, my fault :-(


Still doesn't hit me why he's using 'fgrep' [which implies '-F'] in the
first place.... (instead of 'grep')

-rasp

Barry Margolin

unread,
Apr 13, 2013, 2:36:47 PM4/13/13
to
In article <kkbb35$1ih$1...@dont-email.me>,
Ralph Spitzner <ra...@spitzner.org> wrote:

> Barry Margolin wrote:
> [...]
> > That's not the same as its *purpose*, i.e. why there's a need for a
> > command that does that. He thought the reason was mainly for using the
> > -f option to match strings in a file. He didn't consider the
> > possibility that one might want to search for literal strings rather
> > than regexp patterns, and fgrep means you don't have to escape all the
> > RE operators.
> >
>
>
> Sorry about that, of course he probably wanted :
> --------------
> -f FILE, --file=FILE
> Obtain patterns from FILE, one per line. The empty file contains
> zero patterns, and therefore matches nothing. (-f is specified by
> POSIX.)
> ---------------
>
> Sorry, my fault :-(

He's already using that. His question was about how to use a pipe
instead of a plain file for this parameter.

>
> Still doesn't hit me why he's using 'fgrep' [which implies '-F'] in the
> first place.... (instead of 'grep')

I suspect he didn't realize that all flavors of grep accept the -f
option. It's probably not used much with the other flavors, though; it's
pretty common to have a file full of search terms, but unusual for the
file to contain regular expressions.

So I think he conflated the "f" in "fgrep" with the -f option that it
takes.
0 new messages