小编典典

具有多行和单行的文本文件中的行数

sql

UTL_FILE在oracle中使用实用程序将数据获取到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 commans来计算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","name","p" "","","" "","","ramesh is in USA" "","",""

现在我通过使用linux commans来计算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的确切行数

提前致谢。


阅读 180

收藏
2021-04-14

共1个答案

小编典典

两种情况下的列都不同。为了使其通用,我编写了一个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 filename

2021-04-14