Piping in bash

Discussion in 'macOS' started by Lingonsylt, Dec 5, 2012.

  1. Lingonsylt macrumors newbie

    Joined:
    Dec 5, 2012
    #1
    Hi,

    I'm sort of ashamed that I have to ask this, as a long-time Linux user, but: why the error message below? (Mountain Lion)

    'grep' exists and is in $PATH:

    js-MacBook-Pro:~ j$ grep
    Usage: grep [OPTION]... PATTERN [FILE]...
    Try 'grep --help' for more information.

    But I can't pipe through it:

    js-MacBook-Pro:~ j$ find . | grep -i jpg
    -bash: grep: command not found

    Help appreciated!
     
  2. rhoydotp macrumors 6502

    rhoydotp

    Joined:
    Sep 28, 2006
    #2
    you should run

    find . -type f |grep -i jpg

    EDIT: actually, your command worked for me as well. strange :(

    mlmac:~ rhoy$ find . | grep -i jpg | head -2
    ./Documents/Misc Docs/GBC/concert/Calvary.jpg
    ./Documents/Misc Docs/GBC/concert/cross.jpg
    mlmac:~ rhoy$
     
  3. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #3
    :confused:

    Code:
    find . | grep -i jpg
    works fine for me?!?

    B
     
  4. Lingonsylt thread starter macrumors newbie

    Joined:
    Dec 5, 2012
    #4
    Wow, this is so frustrating.. It just seems that sometimes it works, sometimes it doesn't. I haven't been able to figure out the pattern.

    However, it seems that I can always manifest the problem by doing this:

    1. If Terminal is running, quit it.
    2. Open a new Terminal.
    3. TYPE (don't use up arrow go retrieve an old command, and don't paste anything) " ls -l . | grep jpg". Again, it's important that you TYPE, NOT PASTE.
    4. If you do happen to reproduce the problem by doing (3), if you the press up and edit away the space between the pipe and word "grep", then it *will work*.

    It'd be very interesting to know if anyone can reproduce this. Myself.. I'm stumped. ;)
     
  5. Lingonsylt thread starter macrumors newbie

    Joined:
    Dec 5, 2012
    #5
    It does seem that, as suggested by rhoydotp, that it always works if I don't put a space between the | and grep (or less, or whatever command I want).

    I've been using "<space><pipe>" for 20 years, and it's always worked for me.. And OS X seems to be using bash. I think I know how to work around this problem, but it'd be interesting to learn its cause.
     
  6. therealseebs macrumors 65816

    therealseebs

    Joined:
    Apr 14, 2010
    #6
    My guess: You have typed an invisible character, so you have a character that you can't see, but which is not a space and is part of the name, so it's really "Xgrep: command not found" where X is invisible to you.
     
  7. rhoydotp macrumors 6502

    rhoydotp

    Joined:
    Sep 28, 2006
    #7
    real strange ... this is not an answer but more of a suggestion of what I would have done instead of this pipe

    find . -name \*jpg -print

    btw, i didn't really suggest anything with the spaces. spaces before/after pipe shouldn't matter, at least in my experience.
     
  8. Lingonsylt thread starter macrumors newbie

    Joined:
    Dec 5, 2012
    #8
    therealseebs: I see what you mean with the pasting thing. However, I often see the problem even when not pasting, just typing.

    Thanks for the input, guys. I'll work around the problem for now.
     

Share This Page