Kombinieren Sie Pipe und Weiterleitung auf Curl und JQ

Kombinieren Sie Pipe und Weiterleitung auf Curl und JQ

Wenn ich zu einer Site curle, kann ich direkt JSON erhalten:

curl http://httpbin.org/ip

{ "origin": "37.77.126.22"}

zur Verschönerung mache ich:

curl http://httpbin.org/ip | jq

{
   "origin": "37.77.126.22"
}

um es zu verschönern und zu speichern, leite ich um...aber es funktioniert nicht

curl http://httpbin.org/ip | jq > output.txt

{
   "origin": "37.77.126.22"
}

(23) Failed writing body

Wie soll es gemacht werden?

Antwort1

Ich bin überrascht, dass Sie im letzten Beispiel die JSON-Ausgabe erhalten, aber möglicherweise handelt es sich hierbei um einen Ausschneide- und Einfügefehler in der Frage.

Auf meinem System jqwird eine Fehlermeldung ausgegeben, die auch Nutzungsinformationen enthält:

$ curl "http://httpbin.org/ip" | jq >file
jq - commandline JSON processor [version 1.5]
Usage: jq [options] <jq filter> [file...]

        jq is a tool for processing JSON inputs, applying the
        given filter to its JSON text inputs and producing the
        filter's results as JSON on standard output.
        The simplest filter is ., which is the identity filter,
        copying jq's input to its output unmodified (except for
        formatting).
        For more advanced filters see the jq(1) manpage ("man jq")
        and/or https://stedolan.github.io/jq

        Some of the options include:
         -c             compact instead of pretty-printed output;
         -n             use `null` as the single input value;
         -e             set the exit status code based on the output;
         -s             read (slurp) all inputs into an array; apply filter to it;
         -r             output raw strings, not JSON texts;
         -R             read raw strings, not JSON texts;
         -C             colorize JSON;
         -M             monochrome (don't colorize JSON);
         -S             sort keys of objects on output;
         --tab  use tabs for indentation;
         --arg a v      set variable $a to value <v>;
         --argjson a v  set variable $a to JSON value <v>;
         --slurpfile a f        set variable $a to an array of JSON texts read from <f>;
        See the manpage for more options.
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    33  100    33    0     0    115      0 --:--:-- --:--:-- --:--:--   136
(23) Failed writing body

Die Meldung „Schreiben des Hauptteils fehlgeschlagen“ kommt daher, curldass jqder Text aufgrund des Fehlers beendet wurde und zum Lesen des Hauptteils (Inhalt der Webseite) nicht verfügbar ist.


Wenn Sie an etwas anderes als ein Terminal schreiben möchten, jqbenötigt 1.5 einen Filterausdruck. Der einfachste Filter ist .(dot), der wie ein „Pass-Through“-Filter funktioniert (was in den Nutzungsinformationen oben als „Identitätsfilter“ bezeichnet wird):

$ curl "http://httpbin.org/ip" | jq . >file

Spätere Versionen jqverwenden standardmäßig den Identitätsfilter, auch beim Schreiben in eine Datei oder Pipe, wenn kein Filter explizit angegeben ist.

verwandte Informationen