Fixed width to CSV

只愿长相守 提交于 2020-01-09 10:56:18

问题


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 formats, but it is "encoded" on the second line as:

Name           DOB      GENDER
============== ======== ======
JOHN DOE       19870130 M
MARY DOE       19850521 F
MARTY MCFLY    19790320 M

I want to convert ALL the files to CSV. I can write a program that reads in the first line and holds it for column names. Then, it loads the second line to get the column widths. Then, it uses awk to convert that file to CSV. What I'd prefer to do is find a program that automatically does that (can awk read the second line instead of requiring me to type in the column widths?)


回答1:


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.



来源:https://stackoverflow.com/questions/31947383/fixed-width-to-csv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!