3 ms·
Awk '{ print $2 }' does LWSP gobbling.. cut -d' ' -f2 doesn't and this difference alone makes awk useful to me on a daily basis. Awk has a hash like perl which
by ggm 7y ago
Awk '{ print $2 }' does LWSP gobbling.. cut -d' ' -f2 doesn't and this difference alone makes awk useful to me on a daily basis.
Awk has a hash like perl which is very efficient. I use an awk expression to print uniq as they come in counted through the hash insert on new instead of uniq which prints at end.
Awk count unique over 300,000,000 ips was as fast as perl and python and smaller memory footprint
- swimfar 7y agoLWSP=Linear White Space "Linear white space is: any number of spaces or horizontal tabs, and also newline (CRLF) if it is followed by at least one space or horizontal tab." [1] I don't know what LWSP gobbling is, though. Google didn't help. [1] https://stackoverflow.com/questions/21072713/what-exactly-is-the-linear-whitespace-lws-lwsp https://stackoverflow.com/questions/21072713/what-exactly-is...
- pstuart 7y agoIt means a contiguous collection of arbitrary white space characters are considered to be a single delimiter. With the parent example it means that column #2 could have any of number whitespaces between it and column #1.
- ggm 7y agoyes. whereas cut, considers ' ' (two spaces) as an instance of a blank column separated by the -d' ' space instance. Its like ,, in CSV being a blank column.
- skrebbel 7y ago> Awk '{ print $2 }' does LWSP gobbling That's like answering "why use Haskell?" with "Because its monads are monoids in the category of endofunctors"
- ggm 7y agoThis made me very happy, since it goes to the 'you can either use awk or explain awk but not both' problem which is definitional in monads in Haskell. I thought LWSP was a thing. I forgot not everything is a thing. And gobbling is maybe very jargon ish. Python sys.stdin.rstrip().split() comes to mind.
- chme 7y agoIf you just use awk for `{ print $2 }` then I would still prefer `tr -s ' ' | cut -d ' ' -f2`, since both are part of coreutils and with `awk` you would add a additional dependency to your script.
- asicsp 7y agothat is not equivalent because awk will remove trailing/leading space/tab/newlines (newlines come into play with a different record separator) whereas, tr will still leave a trailing/leading space for example: $ echo ' a b c ' | tr -s ' ' | cut -d ' ' -f2 a $ echo ' a b c ' | awk '{print $2}' b and by default awk splits on space/tab/newlines, whereas in cut example above, you get only space as delimiter cut has its uses and will be faster than awk, but it depends on the problem being solved
- chme 7y agoYou are right it depends on the specific use case. It possible to use `xargs -L 1` to trim the separators, but then you would also add `findutils` as deps. I just wanted to point out that keeping the dependencies of scripts in mind when programming is also important.
- em500 7y agoWhy would being part of coreutils matter? Awk is part of POSIX, just like tr and cut. Even in very constraint environments you can count on it via busybox.
- chme 7y agoA lot of stuff is part in POSIX, but not all of that is available on every system. Also busybox configurations can vary a lot.
- ggm 7y agoA perpetual argument is use of too many pipe separated distinct commands. The argument suggests it's lazy to use sed | awk | grep type pipe runs because in all probability sed or awk alone could have done it and you incurred two excess fork/exec() and therefore consumed kernel and userspace beyond your need. The usual rejoinder is "get nicked"
- arexxbifs 7y agotr will handle the whitespace for you and is generally faster[0] (but awk is faster to type and reads better). [0] https://datagubbe.se/cutvawk/ https://datagubbe.se/cutvawk/
- xorcist 7y agoFor the simplest uses, cut is more readable. If argument expansion is required, consider using read. That way the argument is given a name. Use "read a b c < x" instead of "b=$(cat x | awk '{ print $2 }')". Just remember to pipe to read with care, as the right hand part of a pipe is a subshell in which variables are local. So "read a b c < <(echo 1 2 3)" works, "echo 1 2 3 | read a b c" doesn't. Another way to expand arguments is to simply define a shell function and use $2. Something like "process_line() { echo $2 }". When you have to reach for something like awk, your script would probably improve by being mostly awk. In which case most people are probably looking at perl or python anyway. Even if awk is a nice language, the arrival of perl mostly killed it. It is not wrong to say perl was the next version of awk.