1. 28

  2. 13

    For grep -v there’s a much easier way to invert than the suggested

    awk '/something/ {next} 1'

    You can also use

    awk '!/something/'

    Am I wrong because that’s not in posix or something?

    1. 2

      $0 !~ /something/ is also shorter and very valid. But yours should be fine by POSIX, too:

      When an ERE token appears as an expression in any context other than as the right-hand of the ‘˜’ or “!˜” operator or as one of the built-in function arguments described below, the value of the resulting expression shall be the equivalent of: $0 ˜ /ere/

      Going by that, !/ere/ is !($0 ~ /ere/) as expected.

    2. 12

      awk uses modern (read “Perl”) regular expressions, by default – like grep -E

      Wrong. POSIX extended regexes aren’t PCRE.

      1. 3

        Although GNU grep does have -P too, for perl compatible regex. Doesn’t help awk though.

      2. 3

        I guess most grep will be quicker than awk, however.

        1. 1

          Unless we are talking an order of magnitude slower, YAGNI.

          1. 3

            ( repeat 100 cat /usr/share/dict/words ) | time grep '/[aoeiu]..[aeoiu]/' >/dev/null

            p9p grep: 0.55s
            GNU grep 3.1: 1.3s
            busybox grep: 5.2s
            ripgrep: 11s

            mawk: 2.5s
            gawk: 3.7s
            nawk: 6.6s
            p9p awk: 10s
            busybox awk: 11s

            GNU sed -n: 4.5s
            p9p sed -n: 18s
            busybox sed -n: 6.6s

            perl: 10s
            ruby: 14s

            1. 1

              How in the world did you get those timings? I can’t reproduce anything close to them with either GNU grep or ripgrep.

            2. 1

              Depends on the context. For a script processing large quantities of data, 2x could be a pretty big win. (Though in the specific case of GNU tools, awk actually uses the same DFA matcher that grep does, so it’s probably a wash in any case.)

              1. 1

                Agree on context. For everyday use it seems unlikely to matter, too much.

            3. 0

              On that basis, ag is much faster than grep.

            4. 3

              While I agree, having to quote and enclose in / even the most basic search gets tiresome quickly.

              1. 3

                Quick, somebody write an article on how sed is better than grep!

                1. 2

                  GNU sed is very, very slow in comparison to Perl. Several orders of magnitude.

                  1. 8

                    Quick, somebody write an article on how Perl is better than grep!

                    1. 2

                      I finally decided to replace all my use of sed with perl -pe last week. I’m intimately familiar with perl regex syntax, but find posix-style regexes to be mostly confusing and ugly.

                      1. 3

                        posix-style regexes

                        Which ones, BREs or EREs? BREs are confusing, horrible, ugly and lack all kinds of useful features and ideally would never have made it into POSIX. EREs are much nicer (though still vastly less feature filled than PCREs).

                        sed sucks, long live sed -r.

                  2. 2

                    Awk is that one tool I feel like I never learned fully enough to appreciate. Thanks for sharing this!

                    1. 1

                      And how do you match literal string contained in a variable ?

                      $ var='^.*$'
                      $ grep -F "$var" file

                      With sed and awk you would have to first escape the the special chars (with sed or awk).