Search code examples
bashsednetcat

Why doesn't sed work with my netcat input?


netcat -k -l 10080 | grep --line-buffered "GET /" \
| cut -b6- | sed "s/HTTP.*//"

It works fine if I feed it a log file but when given the real thing it won't output. So I tried to add -u to set but it still won't work

If fed:

GET /test message :) HTTP/1.1
Host: 127.0.0.1
Accept-Encoding: identity
Connection: close
Content-type: application/x-www-form-urlencoded
Accept: text/plain

It's supposed to output: test message :) When sent through cat it works fine. But when it gets the real thing from netcat it won't work. Thanks in advance


Solution

  • It's the cut that is buffering your netcat output. So sed does not get it.

    I suggest to replace the cut with a 2nd sed substitution as follows.

    $ netcat -k -l 10080 | grep --line-buffered GET | sed -e "s/GET //" -e "s/ HTTP.*//"