开发者

How to get a column from a text file in csv format using awk?

My text file is like

0.1
0.2
0.3
0.4
0.5

As this file is generated dynamically, there can me n number of rows

I want to convert this to csv开发者_如何学编程 format.

like this :

0.1,0.2,0.3,0.4,0.5


I'd just use tr:

cat file.txt | tr "\n" ","

But if you insist on awk, here you go:

awk '{printf "%s,",$0};' file.txt


Blender's answer works fine, but if you want to remove the last , and of course, use only awk, here is a way to do it:

awk '{printf "%s,",$0};' file.txt | awk '{print substr($0, 0, length($0)-1)}'

Here's an example of how to do the same using sed:

sed ':q;N;s/\n/,/;t q' file.txt


echo cat file | sed -e s/" "/","/g


If you want to get a column from a text file (with several columns separated by comma) you can go for

awk -F"," '{print $2,$3,$5.}' yourfile.csv

extracting second, third and fifth columns.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜