I know how to use awk to change fixed width to CSV. What I have is a hard drive with a few thousand fixed width files. The all contain different column width fo
Using GNU awk for FIELDWIDTHS and \s/\S:
$ cat tst.awk
BEGIN { OFS="," }
FNR==1 { names=$0; next }
FNR==2 {
FIELDWIDTHS=""
while ( match($0,/\S+\s*/) ) {
FIELDWIDTHS = (FIELDWIDTHS ? FIELDWIDTHS " " : "") RLENGTH
$0 = substr($0,RSTART+RLENGTH)
}
$0 = names
}
{
for (i=1;i<=NF;i++) {
sub(/\s+$/,"",$i)
printf "%s%s", $i, (i<NF?OFS:ORS)
}
}
$ awk -f tst.awk file
Name,DOB,GENDER
JOHN DOE,19870130,M
MARY DOE,19850521,F
MARTY MCFLY,19790320,M
The above will work even if your first row contains spaces in the column names.