add proper error handling for all final exec calls
[hband-tools.git] / user-tools / awk-cut
blobfe69b3086113bfdb8952dc3a323566bf173c4501
1 #!/bin/bash
3 true <<EOF
4 =pod
6 =head1 NAME
8 awk-cut - Select fields from input stream with awk
10 =head1 SYNOPSIS
12 awk-cut [B<COLUMNS-SPEC>]
14 Where B<COLUMNS-SPEC> is a variation of these:
16 =over 8
18 =item B<COLUMN>-
20 =item -B<COLUMN>
22 =item B<COLUMN>-B<COLUMN>
24 =item B<COLUMN>[,B<COLUMN>[,B<COLUMN>[,...]]]
26 =back
28 =head1 SEE ALSO
30 cut.awk(1)
32 =cut
34 EOF
37 shopt -s extglob
39 awk-cut()
41 local expr
42 local narg=0
44 for colspec
46 if [ $narg != 0 ]
47 then
48 expr=$expr"printf \"%s\", RS;"
51 local x=''
52 local y=''
54 case "$colspec" in
55 +([0-9])-)
56 x=${colspec:0:-1}
57 y=NF
59 -+([0-9]))
60 x=0
61 y=${colspec:1}
63 +([0-9])-+([0-9]))
64 x=${colspec%%-*}
65 y=${colspec##*-}
67 [0-9]*([0-9,]))
68 expr=$expr"N=0; split(\"$colspec\", A, /,/); for(X in A){printf(\"%s%s\", N==0 ? \"\" : OFS, \$A[X]); N++};"
71 echo "Invalid column spec: $colspec" >&2
73 esac
75 if [ -n "$x" ]
76 then
77 expr=$expr"for(F=$x;F<=$y;F++) printf(\"%s%s\", F==$x ? \"\" : OFS, \$F);"
80 narg=$((narg + 1))
81 done
83 awk "{ $expr printf \"%s\", RS; }"
86 awk-cut "$@"