如何根据单个值获取行

时间:2014-02-08 12:56:39

标签: perl csv

假设我有csv文件

sno name amnt
1  Larry  8500
2  wall   2500
3  Perl   8500
4  admin  2500
5  sytem  1200
...............
...............
...............
like so on

如果我的值为8500,则使用.csv扩展名创建新文件

sno name amnt
1  Larry  8500
3  Perl   8500

基于amnt 该怎么做请告诉我? 你的建议会很明显。 我试过像

#!/usr/bin/perl -w
use strict;
use warnings;
use Text::CSV;

my $csv = Text::CSV->new;
open(my$new,"<", 'file.csv') or die "oops!";
open(my$old,">" ,'file2.csv' ) or die "Oops!";
while (my@line =<$new>) {
    print $old $line[1]->[1],"\n";
}
close $new;
close $old;

1 个答案:

答案 0 :(得分:2)

这是一个单行: - )

perl -ane '($.==1 || $F[2] == 8500) && print' file1.csv > file2.csv
sno name amnt
1  Larry  8500
3  Perl   8500

抱歉!我只是在开玩笑 - 我知道我的答案对于Perl的教学不是很有建设性,除了TIMTOWTDI之外。

$.==1导致标题行被打印,因为行号为1。

-a会导致Perl将每行的字段拆分为数组@F

-n说要在程序周围放一个循环(感谢@TLP)

$F[2]指的是字段3.

相关问题