Perl脚本从一个特定行读取一个csv文件到文件结尾 [英] Perl script to read a csv file from a particular row to end of file
本文介绍了Perl脚本从一个特定行读取一个csv文件到文件结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个CSV档案 my.csv
:
abc,yyy
efg,zzz
zsc,vvv
uvw,ggg
根据变量 $ X
中的值,我需要从该特定值读取,直到文件。例如:
Depending upon the value in variable $X
I need to read from that particular value until the end of the file. For example:
如果 $ X = efg
,输出将是:
efg,zzz
zsc,vvv
uvw,ggg
$ X = zsc
:
zsc,vvv
uvw,ggg
b $ b
这是我开发的脚本,它读取CSV文件的整个内容并显示:
This is the script I developed, which reads in a CSV file's whole content and displays it:
use strict;
use warnings;
open(my $data, '<', $file) or die "Could not open '$file' $!\n";
while (my $line = <$data>) {
chomp $line;
my @field = split "," , $line;
print "$field[0] $field[1]";
}
请帮助我一个脚本来显示上述场景。 >
Please help me with a script to display the above mentioned scenario.
推荐答案
此解决方案跳过第1行到匹配的文本 if(1 .. / \b $ start_word\\ \\ b /)
,并打印从匹配文本到文件结尾的所有行。
This solution skips lines 1 to the matching text, if (1 .. /\b$start_word\b/)
and prints all lines from the matching text, to the end of the file.
#!/usr/bin/perl
use strict;
use warnings;
my $start_word = 'efg';
my @data;
while (<DATA>) {
chomp;
if (1 .. /\b$start_word\b/) {
#print if /\b$start_word\b/;
@data = [split /,/] if /\b$start_word\b/;
}
else {
#print;
push @data, [split /,/];
}
}
# Do something with @data
__DATA__
abc,yyy
efg,zzz
zsc,vvv
uvw,ggg
这篇关于Perl脚本从一个特定行读取一个csv文件到文件结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文