固定宽度为CSV [英] Fixed width to CSV
问题描述
我知道如何使用awk
将固定宽度更改为CSV.我有一个带有数千个固定宽度文件的硬盘驱动器.全部包含不同的列宽格式,但在第二行中将其编码"为:
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
我想将所有文件转换为CSV.我可以编写一个程序来读取第一行并将其保存为列名.然后,它加载第二行以获取列宽.然后,它使用awk
将该文件转换为CSV.我更喜欢做的是找到一个可以自动执行此操作的程序(awk可以读取第二行,而不是要求我输入列宽吗?)
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?)
推荐答案
对FIELDWIDTHS
和\s/\S
使用GNU awk:
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.
这篇关于固定宽度为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!