3

I need a utility that

  • read from stdin, split as lines
  • write to stdout json object, {"cmd": "In", "lines": [lines from stdin]}

I heard jq being quite powerful, how to acheive above with jq or similar tools.

1 Answer 1

6

By default, jq reads from stdin. The -R flag lets you read raw input streamed linewise. Using [inputs] in combination with the -n flag lets you collect the lines into an array.

… | jq -Rn '{cmd: "In", lines: [inputs]}'
Sign up to request clarification or add additional context in comments.

3 Comments

I need output to be one line. Found -c in jq --help. By the way, does jq support write to a socket, eg, 127.0.0.1:8080.
@qeatzy The -c compacts the output to one line, the -j flag gets rid of trailing newlines. Support for I/O is minimal in general, and currently only supports reading from stdin, and writing to stdout and stderr.
jq itself doesn't write to a socket, but whatever calls jq can arrange for its standard output to be the socket you want to write to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.