linux - Command to print specific column from input -
i wondering if there built-in otherwise command available can print nth column input. e.g. awk '{print $2}'
or cut...
it create life easier when want run on piped output while running via ssh
(quoting hell), , of course of study it'll create life easier in general.
my current hack 100 limitations is:
function _printcol(){ while read data; [ -z "$fields" ] && fields=`for num in "$@"; echo -n "\"\t\",\\$$num"; done` echo $data | awk "{ print $fields }" done } alias printcol=_printcol ls -ltr | printcol 9 ls -ltr | printcol 1 9
i'm wondering if implemented it. prefer if it's built-in, or available possible (like on multiple stock distros) or in worst case installable (like yum, apt, ...).
a rewrite
printcol() { awk -v "fields=$*" ' begin { n = split(fields, f) } { (i=1; i<=n; i++) { printf "%s%s", $(f[i]), ofs } print "" } ' } seq 50 | pr -t5 | printcol 2 4 5
notes:
awk read function's stdin: don't have capture , feed awk -- advantage: single awk invocation, not 1 per line. you don't need gross pre-processing of field list. particularly, why doing inside while-loop? why using function and alias? linux awk sed gnu multiple-columns
No comments:
Post a Comment