1

I'm trying these two variations:

cat /var/log/zwave.log | xargs -n1 jq '.' 

&:

</var/log/zwave.log xargs -n1 jq '.' 

It seems for both the command runs but it tries to open the line as a file location. That makes sense for the second command I listed but for the first using a pipe shouldn't it act as if each line in zwave.log was piped to jq '.'?


The logging system I'm processing but have no control over producing creates a json object on each line of the log file:

{ "name": "PeriodicWork", "hostname": "myHost", "pid": 12189.20, "level": 20, "msg": "Executing [CheckFailedTask NodeId=8]", "time": "2017-12-04T00:20:30.953Z", "v": 0 }

But the logger does occasionally fail and produce something like Unable to start service: [TimeoutError: operation timed out].

That's why I can't just use jq on the file.

1

Don't use xargs.

jq '.' /var/log/zwave.log

If you need to filter out only json lines, use grep in front of it

grep -E "^{" test.txt | jq '.' 

Xargs is for appending each line as an argument to the given command; it is basically equivalent to:

jq '.' $LINE1
jq '.' $LINE2
jq '.' $LINE3

which is what you are seeing. You should use xargs when the file contains a list of other files or arguments you want to pass to a command.

8
  • I want to use jq as a separate command on the contents of each line in the file. – Philip Kirkbride Dec 4 '17 at 22:22
  • jq acts on each line in a file/stream anyway. Or rather each json block. – Michael Daffin Dec 4 '17 at 22:23
  • jq won't act on each line if the content of the file can't be interpreted as a single json object – Philip Kirkbride Dec 4 '17 at 22:25
  • A full example of the input file would help – Michael Daffin Dec 4 '17 at 22:32
  • I added the expected format to the question, but as mentioned I already know I want to execute jq on each line of the file. I'm just trying to find the correct syntax for xargs. – Philip Kirkbride Dec 4 '17 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.