我有一个文本格式的大数据文件,我想通过指定每列长度将其转换为 csv。
列数 = 5
柱长
[4 2 5 1 1]
观察样本:
aasdfh9013512
ajshdj 2445df
预期产出
aasd,fh,90135,1,2
ajsh,dj, 2445,d,f
GNU awk (gawk) 通过
FIELDWIDTHS
直接支持此功能,例如:
gawk '$1=$1' FIELDWIDTHS='4 2 5 1 1' OFS=, infile
输出:
aasd,fh,90135,1,2
ajsh,dj, 2445,d,f
我会使用
sed
并捕获具有给定长度的组:
$ sed -r 's/^(.{4})(.{2})(.{5})(.{1})(.{1})$/\1,\2,\3,\4,\5/' file
aasd,fh,90135,1,2
ajsh,dj, 2445,d,f
这是一个适用于常规
awk
的解决方案(不需要 gawk
)。
awk -v OFS=',' '{print substr($0,1,4), substr($0,5,2), substr($0,7,5), substr($0,12,1), substr($0,13,1)}'
它使用 awk 的
substr
函数来定义每个字段的起始位置和长度。 OFS
定义输出字段分隔符(在本例中为逗号)。
(旁注:这仅在源数据没有任何逗号的情况下有效。如果数据有逗号,那么您必须将它们转义为正确的CSV,这超出了本问题的范围。)
演示:
echo 'aasdfh9013512
ajshdj 2445df' |
awk -v OFS=',' '{print substr($0,1,4), substr($0,5,2), substr($0,7,5), substr($0,12,1), substr($0,13,1)}'
输出:
aasd,fh,90135,1,2
ajsh,dj, 2445,d,f
在
awk
中添加处理此问题的通用方法(替代FIELSWIDTH选项)(我们不需要对子字符串位置进行编码,这将按照用户在需要插入逗号的位置提供的位置编号工作)可能如下,用 GNU awk
编写和测试。要使用它,我们必须定义值(如示例中所示的OP),需要插入逗号的位置编号,awk
变量名称是colLength
给出位置编号,它们之间有空格。
awk -v colLengh="4 2 5 1 1" '
BEGIN{
num=split(colLengh,arr,OFS)
}
{
j=sum=0
while(++j<=num){
if(length($0)>sum){
sub("^.{"arr[j]+sum"}","&,")
}
sum+=arr[j]+1
}
}
1
' Input_file
解释: 简单的解释是,创建名为
awk
的变量 colLengh
,我们需要在需要插入逗号的地方定义位置编号。然后在 BEGIN
部分创建数组 arr
,它具有索引值,我们需要在其中插入逗号。
在主程序部分首先将变量
j
和sum
设为无效。然后从 j=1 运行 while
循环,直到 j 的值等于 num。在每次运行中,从当前行的开头替换(如果当前行的长度大于总和,否则执行替换是没有意义的,我已在此处进行了额外检查)根据需要将所有内容替换为所有内容 + ,
。例如:第一次循环运行时,sub
函数将变为.{4}
,然后它变为.{7}
,因为它的第7个位置我们需要插入逗号等。因此 sub
将从开始到生成的数字中的许多字符替换为匹配值 + ,
。最后在这个程序中提到 1
将打印已编辑/未编辑的行。
awk
使用适当的 substr 命令生成 awk 脚本
cat cols
4
2
5
1
1
<cols awk '{ print "substr($0,"p","$1")"; cs+=$1; p=cs+1 }' p=1
输出:
substr($0,1,4)
substr($0,5,2)
substr($0,7,5)
substr($0,12,1)
substr($0,13,1)
合并行并使其成为有效的 awk 脚本:
<cols awk '{ print "substr($0,"p","$1")"; cs+=$1; p=cs+1 }' p=1 |
paste -sd, | sed 's/^/{ print /; s/$/ }/'
输出:
{ print substr($0,1,4),substr($0,5,2),substr($0,7,5),substr($0,12,1),substr($0,13,1) }
将以上内容重定向到一个文件,例如
/tmp/t.awk
并在输入文件上运行它:
<infile awk -f /tmp/t.awk
输出:
aasd fh 90135 1 2
ajsh dj 2445 d f
或者以逗号作为输出分隔符:
<infile awk -f /tmp/t.awk OFS=,
输出:
aasd,fh,90135,1,2
ajsh,dj, 2445,d,f
如果有人仍在寻找解决方案,我用 python 开发了一个小脚本。只要你有 python 3.5,它就很容易使用
https://github.com/just10minutes/FixedWidthToDelimited/blob/master/FixedWidthToDelimiter.py
"""
This script will convert Fixed width File into Delimiter File, tried on Python 3.5 only
Sample run: (Order of argument doesnt matter)
python ConvertFixedToDelimiter.py -i SrcFile.txt -o TrgFile.txt -c Config.txt -d "|"
Inputs are as follows
1. Input FIle - Mandatory(Argument -i) - File which has fixed Width data in it
2. Config File - Optional (Argument -c, if not provided will look for Config.txt file on same path, if not present script will not run)
Should have format as
FieldName,fieldLength
eg:
FirstName,10
SecondName,8
Address,30
etc:
3. Output File - Optional (Argument -o, if not provided will be used as InputFIleName plus Delimited.txt)
4. Delimiter - Optional (Argument -d, if not provided default value is "|" (pipe))
"""
from collections import OrderedDict
import argparse
from argparse import ArgumentParser
import os.path
import sys
def slices(s, args):
position = 0
for length in args:
length = int(length)
yield s[position:position + length]
position += length
def extant_file(x):
"""
'Type' for argparse - checks that file exists but does not open.
"""
if not os.path.exists(x):
# Argparse uses the ArgumentTypeError to give a rejection message like:
# error: argument input: x does not exist
raise argparse.ArgumentTypeError("{0} does not exist".format(x))
return x
parser = ArgumentParser(description="Please provide your Inputs as -i InputFile -o OutPutFile -c ConfigFile")
parser.add_argument("-i", dest="InputFile", required=True, help="Provide your Input file name here, if file is on different path than where this script resides then provide full path of the file", metavar="FILE", type=extant_file)
parser.add_argument("-o", dest="OutputFile", required=False, help="Provide your Output file name here, if file is on different path than where this script resides then provide full path of the file", metavar="FILE")
parser.add_argument("-c", dest="ConfigFile", required=False, help="Provide your Config file name here,File should have value as fieldName,fieldLength. if file is on different path than where this script resides then provide full path of the file", metavar="FILE",type=extant_file)
parser.add_argument("-d", dest="Delimiter", required=False, help="Provide the delimiter string you want",metavar="STRING", default="|")
args = parser.parse_args()
#Input file madatory
InputFile = args.InputFile
#Delimiter by default "|"
DELIMITER = args.Delimiter
#Output file checks
if args.OutputFile is None:
OutputFile = str(InputFile) + "Delimited.txt"
print ("Setting Ouput file as "+ OutputFile)
else:
OutputFile = args.OutputFile
#Config file check
if args.ConfigFile is None:
if not os.path.exists("Config.txt"):
print ("There is no Config File provided exiting the script")
sys.exit()
else:
ConfigFile = "Config.txt"
print ("Taking Config.txt file on this path as Default Config File")
else:
ConfigFile = args.ConfigFile
fieldNames = []
fieldLength = []
myvars = OrderedDict()
with open(ConfigFile) as myfile:
for line in myfile:
name, var = line.partition(",")[::2]
myvars[name.strip()] = int(var)
for key,value in myvars.items():
fieldNames.append(key)
fieldLength.append(value)
with open(OutputFile, 'w') as f1:
fieldNames = DELIMITER.join(map(str, fieldNames))
f1.write(fieldNames + "\n")
with open(InputFile, 'r') as f:
for line in f:
rec = (list(slices(line, fieldLength)))
myLine = DELIMITER.join(map(str, rec))
f1.write(myLine + "\n")