Skip to content

Instantly share code, notes, and snippets.

@7171u
Last active May 24, 2018 06:43
Show Gist options
  • Save 7171u/b430021500b5a65ff834f65f964e2e69 to your computer and use it in GitHub Desktop.
Save 7171u/b430021500b5a65ff834f65f964e2e69 to your computer and use it in GitHub Desktop.
awk Script To Fold Each Field To n Characters Per Line ( works for comma separated files - CSV)
#usage awk -F"," -v fm=n -f foldfields.awk <file.csv> | column -t -s","
#Where n is the fold width
{
maxblk=1;
for (i=1;i<=NF;i++) {
blk=1;
while (length($i)) {
y[i"@"blk]=substr($i,1,fm) ;
blk++;
$i=substr($i,fm-1);
if( maxblk<blk)maxblk=blk
}
};
for (k=1;k<=maxblk;k++) {
for (i=1;i<=NF;i++){
if (!y[i"@"k]) {
printf " "FS
}
else {
printf y[i"@"k]""FS
}
}
print ""}
delete y
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment