awk 或 sed CSV 文件操作

发布于 2024-10-21 02:27:32 字数 210 浏览 1 评论 0原文

"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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

此岸叶落 2024-10-28 02:27:32
awk -F'^"|","|"

上面的脚本甚至可以处理嵌入双引号或逗号的字段。唯一的缺点(如果你可以这样称呼它)是第一个字段从 $2 开始

概念证明

$ awk -F'^"|","|"
 '{print $2,$3}' ./infile.csv

上面的脚本甚至可以处理嵌入双引号或逗号的字段。唯一的缺点(如果你可以这样称呼它)是第一个字段从 $2 开始

概念证明


 '{print $2,$3}' ./infile.csv
a004-1b North

a004-1c south

a010-1a midwest

a010-1b south
'{print $2,$3}' ./infile.csv

上面的脚本甚至可以处理嵌入双引号或逗号的字段。唯一的缺点(如果你可以这样称呼它)是第一个字段从 $2 开始

概念证明

awk -F'^"|","|"

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

$ awk -F'^"|","|"
 '{print $2,$3}' ./infile.csv

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


 '{print $2,$3}' ./infile.csv
a004-1b North

a004-1c south

a010-1a midwest

a010-1b south
'{print $2,$3}' ./infile.csv

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

聚集的泪 2024-10-28 02:27:32

您需要 GNU Awk 4 才能实现此功能:

$ gawk -vFPAT='[^",]+' '{print $1,$2}'

我喜欢这个新的“字段模式”功能。这是我的新锤子,一切都是钉子。请在 http://www.gnu 上阅读。 org/software/gawk/manual/html_node/Splitting-By-Content.html

(以这种方式编写,它不会考虑嵌入的逗号或引号,因为问题暗示不需要这样做。)

You need GNU Awk 4 for this to work:

$ gawk -vFPAT='[^",]+' '{print $1,$2}'

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.)

固执像三岁 2024-10-28 02:27:32

如果您为此使用 awk,为什么要在其上放置 Perl 标记呢?

在 Perl 中:

#!/usr/bin/env perl

use strict;
use warnings;

use Data::Dumper;

# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent   = 1;

# Set maximum depth for Data::Dumper, zero means unlimited
local $Data::Dumper::Maxdepth = 0;

use Text::CSV;

my $csv = Text::CSV->new();
while( my $row = $csv->getline( \*DATA )){
  print 'row: ', Dumper $row;
}

__DATA__
"a004-1b","North","at006754"
"a004-1c","south","atytgh0""a004-1d","east","atrthh"
"a010-1a","midwest","atyu"
"a010-1b","south","rfg67"

If you're using awk for this, why put a Perl tag on it?

In Perl:

#!/usr/bin/env perl

use strict;
use warnings;

use Data::Dumper;

# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent   = 1;

# Set maximum depth for Data::Dumper, zero means unlimited
local $Data::Dumper::Maxdepth = 0;

use Text::CSV;

my $csv = Text::CSV->new();
while( my $row = $csv->getline( \*DATA )){
  print 'row: ', Dumper $row;
}

__DATA__
"a004-1b","North","at006754"
"a004-1c","south","atytgh0""a004-1d","east","atrthh"
"a010-1a","midwest","atyu"
"a010-1b","south","rfg67"
围归者 2024-10-28 02:27:32
awk -F'\"|\,' '{print $2,$5}' sample
awk -F'\"|\,' '{print $2,$5}' sample
九局 2024-10-28 02:27:32

不处理嵌入的双引号:

sed -e 's/^"\([^"]*\)","\([^"]*\)".*/\1 \2/'

处理它们:

sed -n -e 's/^"//;s/"$//;s/","/ /;s/","/\n/;P'

上述内容甚至适用于 1 或 2 字段输入。

Not handling embedded double quotes:

sed -e 's/^"\([^"]*\)","\([^"]*\)".*/\1 \2/'

To handle them:

sed -n -e 's/^"//;s/"$//;s/","/ /;s/","/\n/;P'

The above works even for a 1 or 2 field input.

大姐,你呐 2024-10-28 02:27:32

如果你想要它“纯” awk 或 sed,这不符合要求,但除此之外它可以工作:

awk -F, '{print $1 " " $2}' | tr -d '"'

If you want it "pure" awk or sed, this won't fit the bill, but otherwise it works:

awk -F, '{print $1 " " $2}' | tr -d '"'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文