我正在使用oracle中的UTL_FILE
实用程序将数据获取到csv文件中。我在这里使用脚本。
所以我得到了一组文本文件
案例:1
test1.csv 文件中的输出示例为
"sno","name"
"1","hari is in singapore
ramesh is in USA"
"2","pong is in chaina
chang is in malaysia
vilet is in uk"
现在我正在使用 linux 命令来计算 test1.csv 中的记录数
egrep -c "^\"[0-9]" test1.csv
这里我得到的记录计数为
2 (ACCORDING TO LINUX)
但是如果我使用 select * from test 计算记录数;
COUNT(*)
---------- (ACCORDING TO DATA BASE)
2
案例:2
test2.csv 文件中的输出示例为
“sno”,“名称”,“p”
“”、“”、“”
"","","拉梅什在美国"
“”,“”,“”
现在我正在使用 linux 命令来计算 test2.csv 中的记录数
egrep -c "^\"[0-9]" test2.csv
这里我得到的记录计数为
0 (ACCORDING TO LINUX)
但是如果我使用 select * from test 计算记录数;
COUNT(*)
---------- (ACCORDING TO DATA BASE)
2
任何人都可以帮助我如何使用单个命令计算 case:1 和 case:2 中的确切行数
提前致谢。
最佳答案
两种情况下的列是不同的。为了使其通用,我编写了一个 perl 脚本来打印行。它从标题生成正则表达式并用它来计算行。我假设第一行始终代表列数。
#!/usr/bin/perl -w
open(FH, $ARGV[0]) or die "Failed to open file";
# Get coloms from HEADER and use it to contruct regex
my $head = <FH>;
my @col = split(",", $head); # Colums array
my $col_cnt = scalar(@col); # Colums count
# Read rest of the rows
my $rows;
while(<FH>) {
$rows .= $_;
}
# Create regex based on number of coloms
# E.g for 3 coloms, regex should be
# ".*?",".*?",".*?"
# this represents anything between " and "
my $i=0;
while($i < $col_cnt) {
$col[$i++] = "\".*?\"";
}
my $regex = join(",", @col);
# /s to treat the data as single line
# /g for global matching
my @row_cnt = $rows =~ m/($regex)/sg;
print "Row count:" . scalar(@row_cnt);
只需将其存储为 row_count.pl
并以 ./row_count.pl 文件名
运行
关于sql - 具有多行和单行的文本文件中的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24282676/