分析包含在使用awk领域commans csv文件 [英] parse a csv file that contains commans in the fields with awk

查看:84
本文介绍了分析包含在使用awk领域commans csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用AWK在csv文件打印出4个不同的列。问题是字符串是在$ X,XXX.XX格式。当我运行规律awk命令。

  awk的-F,{$打印1} testfile.csv

我的输出`结束了看起来像

  307.00
$ 132.34
30.23

我是什么做错了。

141,818.88 $,$ 52,831,578.53,$ 52,788,069.53
 这大致是输入。该文件我要解析90000行和40列
这是输入的布局方式,或至少它,我有件来处理。很抱歉,如果我让你觉得这是不是我在说什么。

如果输入是307.00 $,$ 132.34,$ 30.23
我想输出是在一个

  $ 307.00
$ 132.34
$ 30.23


解决方案

奇怪的是,我不得不前一段时间解决这个问题,我不停的code各地去做。你几乎拥有它,但你需要获得一个有点棘手与你的字段分隔符(S)。

 的awk -F',| ^|$''{$打印2}testfile.csv

输入

 #猫testfile.csv
$ 141,818.88,$ 52,831,578.53,$ 52,788,069.53
$ 2,558.20,$ 482,619.11,$ 9,687,142.69
$ 786.48,$ 8,568,159.41,$ 159,180,818.00

输出

 #awk的-F,| ^|$''{$打印2}testfile.csv
$ 141,818.88
$ 2,558.20
$ 786.48

您会注意到,第一字段实际上是 $ 2 因为字段分隔符 ^。小的代价,如果你问我付出了短短的1班轮。

i have to use awk to print out 4 different columns in a csv file. The problem is the strings are in a $x,xxx.xx format. When I run the regular awk command.

awk -F, {print $1} testfile.csv 

my output `ends up looking like

307.00
$132.34
30.23

What am I doing wrong.

"$141,818.88","$52,831,578.53","$52,788,069.53" this is roughly the input. The file I have to parse is 90,000 rows and about 40 columns This is how the input is laid out or at least the parts of it that I have to deal with. Sorry if I made you think this wasn't what I was talking about.

If the input is "$307.00","$132.34","$30.23" I want the output to be in a

$307.00
$132.34
$30.23

解决方案

Oddly enough I had to tackle this problem some time ago and I kept the code around to do it. You almost had it, but you need to get a bit tricky with your field separator(s).

awk -F'","|^"|"$' '{print $2}' testfile.csv 

Input

# cat testfile.csv
"$141,818.88","$52,831,578.53","$52,788,069.53"
"$2,558.20","$482,619.11","$9,687,142.69"
"$786.48","$8,568,159.41","$159,180,818.00"

Output

# awk -F'","|^"|"$' '{print $2}' testfile.csv
$141,818.88
$2,558.20
$786.48

You'll note that the "first" field is actually $2 because of the field separator ^". Small price to pay for a short 1-liner if you ask me.

这篇关于分析包含在使用awk领域commans csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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