Results 1 to 9 of 9

Thread: Why doesn't this command work?

  1. #1
    Join Date
    Mar 2006
    Beans
    Hidden!

    Question Why doesn't this command work?

    Code:
    ping -c 20 www.google.co.uk
    Works fine

    Code:
    ping -c 20 www.google.co.uk | grep "36.0"
    Works fine

    Code:
    ping -c 20 www.google.co.uk > ping.txt
    Works fine

    but

    Code:
    ping -c 20 www.google.co.uk | grep "36.0" > ping.txt
    Doesn't work (well not for me!). Seen lots of examples like this offered up. Any suggestions?
    No longer participating......

  2. #2
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Why doesn't this command work?

    Quote Originally Posted by Jose Catre-Vandis View Post
    Code:
    ping -c 20 www.google.co.uk
    Works fine

    Code:
    ping -c 20 www.google.co.uk | grep "36.0"
    Works fine

    Code:
    ping -c 20 www.google.co.uk > ping.txt
    Works fine

    but

    Code:
    ping -c 20 www.google.co.uk | grep "36.0" > ping.txt
    Doesn't work (well not for me!). Seen lots of examples like this offered up. Any suggestions?
    What do you mean by "doesn't work"? What are you trying to catch with the "36.0"? part of an address? The response time?

  3. #3
    Join Date
    Oct 2011
    Beans
    33

    Re: Why doesn't this command work?

    It appears that you are attempting to grep for a particular time delay (36.0 ms) for receiving a response to an ICMP request. How can you be certain that this time will appear in your ping results?

    If 36.0 does not appear in the ping results, then obviously the file you are attempting to create will be empty. Why not experiment with something more predicable to verify that your command construct is valid? Something like:
    Code:
    ping -c 20 www.google.co.uk | grep time > ping.txt

  4. #4
    Join Date
    Jan 2008
    Beans
    28

    Re: Why doesn't this command work?

    This is working fine on Bash:

    Code:
    $ ping -c 5 www.google.com | grep icmp > a.txt
    so either your ping is not presenting 36.0 on output or you are using another shell.

  5. #5
    Join Date
    Mar 2006
    Beans
    Hidden!

    Re: Why doesn't this command work?

    By "Works fine" I mean I get output, whether to console or file.

    A initial test with ping was giving many 36.0's as the response time but I had the same problem if I chose something else more reliable that was output by ping. Sorry for the confusion.

    It's not about the criteria, it's the fact that no data is sent to the file, but I see that it does work for others?
    No longer participating......

  6. #6
    Join Date
    May 2006
    Beans
    1,790

    Re: Why doesn't this command work?

    Quote Originally Posted by Jose Catre-Vandis View Post
    By "Works fine" I mean I get output, whether to console or file.

    A initial test with ping was giving many 36.0's as the response time but I had the same problem if I chose something else more reliable that was output by ping. Sorry for the confusion.

    It's not about the criteria, it's the fact that no data is sent to the file, but I see that it does work for others?
    This is likely to work like you expect:

    Code:
    ping -c 20 www.google.co.uk | grep --line-buffered icmp >ping.txt
    'grep' normally buffers its output when writing to a file, so even if it has seen three matches, you won't see them in the file yet. The --line-buffered option makes it behave as if it was writing to the terminal.

  7. #7
    Join Date
    Mar 2006
    Beans
    Hidden!

    Re: Why doesn't this command work?

    Thanks Arndt that works

    Just out of interest, if grep is buffering the output, will it "eventually" write to the file or never?
    No longer participating......

  8. #8
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Why doesn't this command work?

    Yes it will. For more details check out BashFAQ 009 (link in my signature).

  9. #9
    Join Date
    Mar 2006
    Beans
    Hidden!

    Re: Why doesn't this command work?

    Thanks, that's a good link
    No longer participating......

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •