Awk/Unix group by

$ awk -F, ‘NR>1{arr[$1]++}END{for (a in arr) print a, arr[a]}’ file.txt joe 1 jim 1 mike 3 bob 2 EXPLANATIONS -F, splits on , NR>1 treat lines after line 1 arr[$1]++ increment array arr (split with ,) with first column as key END{} block is executed at the end of processing the file for (a … Read more

How can I format the output of a bash command in neat columns

column(1) is your friend. $ column -t <<< ‘”option-y” yank-pop > “option-z” execute-last-named-cmd > “option-|” vi-goto-column > “option-~” _bash_complete-word > “option-control-?” backward-kill-word > “control-_” undo > “control-?” backward-delete-char > ‘ “option-y” yank-pop “option-z” execute-last-named-cmd “option-|” vi-goto-column “option-~” _bash_complete-word “option-control-?” backward-kill-word “control-_” undo “control-?” backward-delete-char

Tab separated values in awk

You need to set the OFS variable (output field separator) to be a tab: echo “$line” | awk -v var=”$mycol_new” -F’\t’ ‘BEGIN {OFS = FS} {$3 = var; print}’ (make sure you quote the $line variable in the echo statement)