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.
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]}'
-c in jq --help. By the way, does jq support write to a socket, eg, 127.0.0.1:8080.-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.