具有多行和单行的文本文件中的行数 [英] line count with in the text files having multiple lines and single lines

查看:95
本文介绍了具有多行和单行的文本文件中的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在oracle中使用UTL_FILE实用程序将数据获取到csv文件中.在这里,我正在使用脚本.

i am using UTL_FILE utility in oracle to get the data in to csv file. here i am using the script.

所以我正在获取一组文本文件

so i am getting the set of text files

情况:1

test1.csv文件中的输出示例为

sample of output in the test1.csv file is

"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 as来计数test1.csv中的记录数

now i am counting the number of records in the test1.csv by using linux commans as

egrep -c "^\"[0-9]" test1.csv

在这里,我得到的记录数为

here i am getting the record count as

2      (ACCORDING TO LINUX)

但是如果我通过使用test中的select *来计算记录数;

but if i calculate the number of records by using select * from test;

 COUNT(*)
----------                 (ACCORDING TO DATA BASE)
    2

情况:2

test2.csv文件中的输出示例为

sample of output in the test2.csv file is

"sno","name","p" "","","" "","","ramesh is in USA" "","",""

"sno","name","p" "","","" "","","ramesh is in USA" "","",""

现在我正在通过使用Linux commans as来计数test2.csv中的记录数

now i am counting the number of records in the test2.csv by using linux commans as

egrep -c "^\"[0-9]" test2.csv

在这里,我得到的记录数为

here i am getting the record count as

0      (ACCORDING TO LINUX)

但是如果我通过使用test中的select *来计算记录数;

but if i calculate the number of records by using select * from test;

 COUNT(*)
----------                 (ACCORDING TO DATA BASE)
    2

任何人都可以帮助我如何使用单个命令计算出case:1和case:2的确切行数

can any body help me how to count the exact lines in case:1 and case:2 using the single command

提前谢谢.

推荐答案

这两种情况下的列都不同.为了使其通用,我编写了一个perl脚本来打印行.它从头生成正则表达式,并用它来计算行.我以为第一行总是代表列数.

Columns in both case is different. To make it generic I wrote a perl script which will print the rows. It generates the regex from headers and used it to calculate the rows. I assumed that first line always represents the number of columns.

#!/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

Just store it as row_count.pl and run it as ./row_count.pl filename

这篇关于具有多行和单行的文本文件中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆