There I said it !

  • allywilson
    link
    fedilink
    arrow-up
    17
    ·
    1 month ago

    Yeah, it’s a pain. Leads to bad one liners:

    for i in $(ls); do zcat $i || cat $i; done

    • MonkderVierte
      link
      fedilink
      arrow-up
      11
      ·
      edit-2
      1 month ago

      Btw, don’t parse ls. Use find |while read -r instead.

      find -maxdepth 1 -name "term" -print |while read -r file
         do zcat "$file" 2>/dev/null || cat "$file"
      done
      
      • allywilson
        link
        fedilink
        arrow-up
        4
        ·
        1 month ago

        Won’t this cause cat to iterate through all files in the cwd once zcat encounters an issue, instead of just the specific file?

      • gnuhaut
        link
        fedilink
        arrow-up
        1
        ·
        edit-2
        30 days ago

        You can just do for f in * (or other shell glob), unless you need find’s fancy search/filtering features.

        The shell glob isn’t just simpler, but also more robust, because it works also when the filename contains a newline; find .. | while read -r will crap out on that. Also apparently you want while IFS= read -r because otherwise read might trim whitespace.

        If you want to avoid that problem with the newline and still use find, you can use find -exec or find -print0 .. | xargs -0, or find -print0 .. | while IFS= read -r -d ''. I think -print0 is not standard POSIX though.

        • MonkderVierte
          link
          fedilink
          arrow-up
          1
          ·
          30 days ago

          because it works also when the filename contains a newline

          Doesn’t that depend on the shell?

          • gnuhaut
            link
            fedilink
            arrow-up
            1
            ·
            30 days ago

            I don’t think so and have never heard that, but I could be wrong.

    • interdimensionalmemeOP
      link
      fedilink
      arrow-up
      6
      arrow-down
      1
      ·
      edit-2
      1 month ago

      Thanks !

      But still we shouldn’t have to resort to this !

      Also, can’t get the output through pipe

      for i in $(ls); do zcat $i || cat $i; done | grep mysearchterm

      this appears to work

      find . -type f -print0 | xargs -0 -I{} sh -c 'zcat "{}" 2>/dev/null || cat "{}"' | grep "mysearchterm"

      Still, that was a speed bump that I guess everyone dealing with mass compressed log files has to figure out on the fly because zcat can’t read uncompressed files ! argg !!!

      for i in $(ls); do zcat $i 2>/dev/null || cat $i; done | grep mysearchterm