3 ms·
If 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 a
by chme 6y ago
If 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 6y 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 6y 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 6y 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 6y 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 6y 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"
- joosters 6y agoIMO, it would be great if 'cut' could accept more than just a single character for delimiters. e.g. a regex or even a shell-style pattern match would be very useful. `cut -d ' +' -f2` would get rid of the need for 'tr' in your example. Even just allowing >1 char delimiters would be helpful. A text list like 'foo, bar, foobar' is too much for cut, but it would be fine if cut accepted parameters like `cut -d ', '`