AWK - print range of columns

The utility cut has a compact notation:

cut -d, -f2-7 <input-file>

producing:

column2,column3,column4,column5,column6,column7

Answering the comment by @PlasmaBinturong: my intent was address the issue of a short calling sequence: "... my awk command would get horribly long ...". However, one can also find codes that arrange the fields as one might desire. As much as I like awk, perl, python, I have often found it useful to build a specific utility to extend the capabilities of standard *nix. So here is an excerpt from a test script, s2, showing utilities recut and arrange, both allow re-arrangement and duplication, with arrange also allowing decreasing field ranges:

FILE=${1-data1}

# Utility functions: print-as-echo, print-line-with-visual-space.
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }

pl " Input data file $FILE:"
head $FILE

pl " Results, cut:"
cut -d, -f2-7 $FILE

pl " Results, recut (modified as my-recut):"
my-recut -d "," 7,6,2-5 < $FILE

pl " Results, arrange:"
arrange -s "," -f 5,3-1,7,5,3-4,5 $FILE

producing results from these versions:

OS, ker|rel, machine: Linux, 3.16.0-10-amd64, x86_64
Distribution        : Debian 8.11 (jessie) 
bash GNU bash 4.3.30
cut (GNU coreutils) 8.23
recut - ( local: RepRev 1.1, ~/bin/recut, 2010-06-10 )
arrange (local) 1.15

-----
 Input data file data1:
column1,column2,column3,column4,column5,column6,column7,column8

-----
 Results, cut:
column2,column3,column4,column5,column6,column7

-----
 Results, recut (modified as my-recut):
column7,column6,column2,column3,column4,column5

-----
 Results, arrange:
column5,column3,column2,column1,column7,column5,column3,column4,column5

The my-recut is a slight modification the textutils code recut, and arrange is our version of an extended cut. More information:

recut   Process fields like cut, allow repetitions and re-ordering. (what)
Path    : ~/bin/recut
Version : - ( local: RepRev 1.1, ~/bin/recut, 2010-06-10 )
Length  : 56 lines
Type    : Perl script, ASCII text executable
Shebang : #!/usr/bin/perl
Home    : http://www1.cuni.cz/~obo/textutils/ (doc)
Modules : (for perl codes)
 Getopt::Long   2.42

arrange Arrange fields, like cut, but in user-specified order. (what)
Path    : ~/bin/arrange
Version : 1.15
Length  : 355 lines
Type    : Perl script, ASCII text executable
Shebang : #!/usr/bin/perl
Modules : (for perl codes)
 warnings       1.23
 strict 1.08
 Carp   1.3301
 Getopt::Euclid 0.4.5

Best wishes ... cheers, drl


$ awk -v b=2 -v e=7 'BEGIN{FS=OFS=","} {for (i=b;i<=e;i++) printf "%s%s", $i, (i<e ? OFS : ORS)}' file
column2,column3,column4,column5,column6,column7

b=beginning field number, e=end field number. If you need to handle CSVs with quoted fields, embedded commas, newlines, etc. then see https://stackoverflow.com/q/45420535/1745001.


sed -e '
  s/,/\n/7        ;# tag the end of col7
  s/^/,/          ;# add a comma
  s/,/\n/2        ;# tag beginning of col2
  s/.*\n\(.*\)\n.*/\1/ ;# perform surgery 
' file.csv

Results:

column2,column3,column4,column5,column6,column7