awk:使用以下命令将一列文件转换为多列csv文件 [英] awk: convert one column file in a multiple columns csv file using

查看:80
本文介绍了awk:使用以下命令将一列文件转换为多列csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的文件

I have a file that look like that

a
b
c
g
v
b
g
h
t
y

我正在寻找awk解决方案,将上述文件转换为类似的格式

I am looking for an awk solution to convert the above file in something like this

a,b,c,g
v,b,g,h
t,y

如果我在此处我想我可以先使用awk进行转置,然后将我的唯一行分成多行.但是,我认为我真的无法理解.有什么好的教程/链接/菜谱/建议来咨询这样做吗?

If I consult here I guess I could use awk to first transpose and then divide my unique line in multiple lines. However, I think I cannot really understand how. Any good tutorials/link/cookbook/suggestion to consult to do this?

推荐答案

您可以使用以下 awk :

awk 'NR%4 {printf $0",";next} 1; END {print ""}' file

当行号不能被四除时,只需打印不带换行符的行并添加逗号即可.否则,使用换行符打印该行.

When the line number is not dividable by four, just print the line without the newline and add a comma. Otherwise, print the line with the newline.

您可以使用 END 块在最后打印换行符.

You can print a newline at the end by using the END block.

这篇关于awk:使用以下命令将一列文件转换为多列csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆