我设法创建了一个现在需要格式化为csv import的文件:
以下是一些记录:
(202) 111-0000
1
Full Name
Street Address
City, State ZIP
(212) 222-9999
2
Full Name
Street Address
City, State ZIP
(312) 888-2222
3
Full Name
Street Address
City, State ZIP
等
我希望转换成这个:
(202) 111-0000,1,Full Name,Street Address,City,State,ZIP
(212) 222-9999,2,Full Name,Street Address,City,State,ZIP
(312) 888-2222,3,Full Name,Street Address,City,State,ZIP
注意City State和ZIP如何在所需的输出中以逗号分隔。
愿意帮我解决这个问题的awk或sed大师?
非常感谢。
答案 0 :(得分:1)
这可能适合你(GNU sed):
sed -r ':a;$!N;s/\n(\S)/,\1/;ta;s/,\s+/,/g;P;d' file
用逗号替换换行符,除非该行以空格开头,在这种情况下删除逗号后面的任何空格并打印前行放弃空行。
答案 1 :(得分:0)
我假设您可以访问GNU awk和sed。
为了使ZIP部分正确,让我们使用它作为样本输入:
$ cat file
(202) 111-0000
1
Full Name
Street Address
City, State 10023
(212) 222-9999
2
Full Name
Street Address
City, State 10023
(312) 888-2222
3
Full Name
Street Address
City, State 10023
我们可以如下转置:
$ awk -v RS="" -F'\n' -v OFS=, '{$1=$1} 1' file | sed -r 's/ +([[:digit:]-]+)$/, \1/'
(202) 111-0000,1,Full Name,Street Address,City, State, 10023
(212) 222-9999,2,Full Name,Street Address,City, State, 10023
(312) 888-2222,3,Full Name,Street Address,City, State, 10023
如果上面的额外空格有问题,请尝试:
$ awk -v RS="" -F'\n' -v OFS=, '{$1=$1} 1' file | sed -r 's/ +([[:digit:]-]+)$/, \1/; s/, +/,/g'
(202) 111-0000,1,Full Name,Street Address,City,State,10023
(212) 222-9999,2,Full Name,Street Address,City,State,10023
(312) 888-2222,3,Full Name,Street Address,City,State,10023
答案 2 :(得分:0)
简短的awk
解决方案(最后提供额外的,
)
awk 'NR%6==5 {gsub(/,? /,",")} ORS=NR%6?",":RS' file
(202) 111-0000,1,Full Name,Street Address,City,State,ZIP,
(212) 222-9999,2,Full Name,Street Address,City,State,ZIP,
(312) 888-2222,3,Full Name,Street Address,City,State,ZIP,
答案 3 :(得分:0)
gsed -n 'H;/^$/h;/ZIP$/x;s/^\n//;s/\n/,/gp' file