Split delimited file into smaller files by column Split delimited file into smaller files by column unix unix

Split delimited file into smaller files by column


#!/bin/bash(($# == 2)) || { echo -e "\nUsage: $0 <file to split> <# columns in each split>\n\n"; exit; }infile="$1"inc=$2ncol=$(awk 'NR==1{print NF}' "$infile")((inc < ncol)) || { echo -e "\nSplit size >= number of columns\n\n"; exit; }for((i=0, start=1, end=$inc; i < ncol/inc + 1; i++, start+=inc, end+=inc)); do  cut -f$start-$end "$infile" > "${infile}.$i"done


if you only need a QAD (Quick & Dirty) solution for in my case a fixed 8 column ; separated csv

#!/bin/bash# delimiter is ;cut -d';' -f1 "$1" > "${1}.1"cut -d';' -f2 "$1" > "${1}.2"cut -d';' -f3 "$1" > "${1}.3"cut -d';' -f4 "$1" > "${1}.4"cut -d';' -f5 "$1" > "${1}.5"cut -d';' -f6 "$1" > "${1}.6"cut -d';' -f7 "$1" > "${1}.7"cut -d';' -f8 "$1" > "${1}.8"


Thanks for the help. I hoped there would be a unix command similar to split, but I ended up wrapping the cut command with perl, via SiegeX's suggestion.

#!/usr/bin/perlchomp(my $pwd = `pwd`);my $help = "\nUsage: $0 <file to split> <# columns in each split>\n\n";die $help if @ARGV!=2;$infile = $ARGV[0];chomp($ncol = `head -n 1 $infile | wc -w`);$start=1;$inc = $ARGV[1];$end = $start+$inc-1;die "\nSplit size >= number of columns\n\n" if $inc>=$ncol;for($i=1 ; $i<$ncol/$inc +1 ; $i++) {    if ($end>$ncol) {$end=$ncol;}    `cut -f $start-$end $infile > $infile.$i`;    $start += $inc;    $end += $inc;}