Mark
Mark

Reputation: 5038

Extracting HTTP content in bash doesn't work with nc output

Let's say I have this HTTP response:

POST / HTTP/1.1
Content-Type: text/plain;charset=UTF-8
Content-Length: 5
Connection: Keep-Alive
Accept-Encoding: gzip
Accept-Language: en,*
User-Agent: Mozilla/5.0
Host: 127.0.0.1:55764

Hello

And I'm interested only in content ("Hello"). I found this command to work if the text is fed from a file:

cat data.txt | tr '\n' '#' | sed "s/.*##//" | tr '#' '\n'
Hello

where data.txt contains the text above. But if I try to feed it with the output of nc:

#!/bin/bash
while true
do
  echo -e "HTTP/1.1 200 OK\n\n" | ./busybox-armv7l nc -l -p 55764 | tr '\n' '#' | sed "s/.*##//" | tr '#' '\n'
done

it doesn't work, i.e. it just print out everything:

POST / HTTP/1.1
Content-Type: text/plain;charset=UTF-8
Content-Length: 5
Connection: Keep-Alive
Accept-Encoding: gzip
Accept-Language: en,*
User-Agent: Mozilla/5.0
Host: 127.0.0.1:55764

HelloPOST / HTTP/1.1
Content-Type: text/plain;charset=UTF-8
Content-Length: 5
Connection: Keep-Alive
Accept-Encoding: gzip
Accept-Language: en,*
User-Agent: Mozilla/5.0
Host: 127.0.0.1:55764

Hello

Why the piping works with cat but not with nc?

Upvotes: 0

Views: 81

Answers (1)

gopy
gopy

Reputation: 328

output of nc goes to stderr just add & after second | to make the pipe effective:

echo -e "HTTP/1.1 200 OK\n\n" | ./busybox-armv7l nc -l -p 55764 |& tr '\n' '#' | sed "s/.*##//" | tr '#' '\n

Upvotes: 1

Related Questions