awk 或 sed CSV 文件操作
"a004-1b","North","at006754"
"a004-1c","south","atytgh0"
"a004-1d","east","atrthh"
"a010-1a","midwest","atyu"
"a010-1b","south","rfg67"
我想打印第一列和第二列,没有任何额外的字符 我想消除所有(“”和第三列) 提前致谢
"a004-1b","North","at006754"
"a004-1c","south","atytgh0"
"a004-1d","east","atrthh"
"a010-1a","midwest","atyu"
"a010-1b","south","rfg67"
I want to print the first column and the second column without any extra character I want eliminate all ("", and the third column) Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
上面的脚本甚至可以处理嵌入双引号或逗号的字段。唯一的缺点(如果你可以这样称呼它)是第一个字段从
$2
开始概念证明
The above script will even handle fields that have embedded double quotes or commas. The only downside (if you can call it that) is that the first field starts at
$2
Proof of Concept
您需要 GNU Awk 4 才能实现此功能:
我喜欢这个新的“字段模式”功能。这是我的新锤子,一切都是钉子。请在 http://www.gnu 上阅读。 org/software/gawk/manual/html_node/Splitting-By-Content.html
(以这种方式编写,它不会考虑嵌入的逗号或引号,因为问题暗示不需要这样做。)
You need GNU Awk 4 for this to work:
I love this new "field pattern" feature. It's my new hammer and everything is a nail. Read up on it at http://www.gnu.org/software/gawk/manual/html_node/Splitting-By-Content.html
(Written this way it doesn't account for embedded commas or quotes, because the question implies this is not needed.)
如果您为此使用
awk
,为什么要在其上放置 Perl 标记呢?在 Perl 中:
If you're using
awk
for this, why put a Perl tag on it?In Perl:
不处理嵌入的双引号:
处理它们:
上述内容甚至适用于 1 或 2 字段输入。
Not handling embedded double quotes:
To handle them:
The above works even for a 1 or 2 field input.
如果你想要它“纯” awk 或 sed,这不符合要求,但除此之外它可以工作:
If you want it "pure" awk or sed, this won't fit the bill, but otherwise it works: