#!/bin/bash ## arr -- much like the Mantes arrange-command -- by Eugene Reimer 2001-oct; ## obsoletes several of my earlier scripts: dbl, colswap; ## ## USAGE EXAMPLES: ## arr 2 1 --a filter to swap columns, for a 2-column file ## arr 1 1 --a filter to "double" a 1-column file ## arr 3 2 1 --a filter to column-reverse a 3-column file ## arr -F'\t' 2 1 --like first example but with TAB-separated fields; by default they are SPACE-or-TAB-separated ## K=$(wc F |arr 1) --get number-of-lines in file F ## ## Copyright © 2001,2011 Eugene Reimer; can be used, modified, copied, and distributed or sold under the terms of either the LGPL or the GPL (your choice); ## see http://www.gnu.org/licenses for the details of these terms. S=' ' [ $# -eq 0 ] && { echo "usage: arr [-F FLDSEP] column-nbrs"; exit; } if [[ $1 == -F ]];then S="$2"; shift 2 elif [[ $1 == -F* ]];then S="${1#-F}"; shift fi s='{print $'"$1"; shift for x do s="$s"' " "' if [[ $x == [0-9]* ]]; then s="$s"' $'"$x" else s="$s \"$x\"" fi done s="$s}" ##echo "awk -F\"$S\" $s" ##debug awk -F"$S" "$s" exit 2004-feb: added support for non-numeric literals between column-nbrs; literals & columns are separated by TAB-chars 2011-mar: finally added -F support;