cat file1
024170529s18687522049000005000011868443700001479N
我们如何以固定宽度向文件行添加分隔符,
?
3digitfield,6digitfield,12digitfield,10digitfield,14digitfield,3digitfield,1digitfield
答案 0 :(得分:3)
GNU awk和FIELDWIDTHS
:
$ awk 'BEGIN{FIELDWIDTHS="3 6 12 10 14 3 1";OFS=","}{$1=$1}1' file
024,170529,s18687522049,0000050000,11868443700001,479,N
修改:awk版本:
$ awk '
BEGIN {
fw="3 6 12 10 14 3 1"
n=split(fw,a)
OFS=","
}
{
for(i=1;i<=n;i++) {
printf "%s%s", substr($0,p+1,a[i]), (i<n? OFS : ORS)
p+=a[i] }
}' file
024,170529,s18687522049,0000050000,11868443700001,479,N