如何在shell脚本中查找列数?

时间:2013-05-11 07:03:36

标签: unix csv awk

我有文本文件说01-01-2013.txt,它的内容如下

cat 01-01-2013.txt | cut -d',' -f1-10 | head -6
TIMESTAMP,DATE,HOUR_MINUTE,CAWFM,CSQ2,DBCRIS.CA.MSG,DBI.MSBR.INCOMING.Q,DBI.MSBR.INCOMING.Q.ERROR,DBT.CA.RECEIVE.CUST,DBT.CA.RECEIVE.CUST.ERROR
20130101000019,01.01.2013,00:00,0,0,0,0,0,6893,31
20130101000133,01.01.2013,00:01,0,0,0,0,0,6858,31
20130101000246,01.01.2013,00:02,0,0,0,0,0,6661,31
20130101000359,01.01.2013,00:03,0,0,0,0,0,6314,31
20130101000512,01.01.2013,00:05,0,0,0,0,0,6074,31

当我试图找到我在下面使用的列数时,我没有得到如下所示的预期结果。它只是返回1

awk 'BEGIN {IFS=","};END{print NF}' 01-01-2013.txt

结果

1

请帮我查一下总栏数。

3 个答案:

答案 0 :(得分:2)

您的示例不起作用,因为awk不使用名称IFS,输入字段分隔符名为FS(但它确实使用OFS作为输出字段分隔器)。

$ awk 'BEGIN{FS=","}END{print NF}' csv
10

但你可以不读取整个文件,例如通过只阅读第一行

$ awk -F, 'NR==1{print NF;exit}' csv
10

或匹配/,/

的第一行
$ awk -F, '/,/{print NF;exit}' csv
10

如果您只想删除前两列,我不明白为什么您不会在问题中坚持使用cut

$ cut -d, -f3- csv
HOUR_MINUTE,CAWFM,CSQ2,DBCRIS.CA.MSG,DBI.MSBR.INCOMING.Q,DBI.MSBR.INCOMING.Q.ERROR,DBT.CA.RECEIVE.CUST,DBT.CA.RECEIVE.CUST.ERROR
00:00,0,0,0,0,0,6893,31
00:01,0,0,0,0,0,6858,31
00:02,0,0,0,0,0,6661,31
00:03,0,0,0,0,0,6314,31
00:05,0,0,0,0,0,6074,31

答案 1 :(得分:0)

$ awk '{print NF}' FS=, 01-01-2013.txt
10
10
10
10
10
10

答案 2 :(得分:0)

要查找列的总数:

<强> INPUT_FILE

TIMESTAMP,DATE,HOUR_MINUTE,CAWFM,CSQ2,DBCRIS.CA.MSG,DBI.MSBR.INCOMING.Q,DBI.MSBR.INCOMING.Q.ERROR,DBT.CA.RECEIVE.CUST,DBT.CA.RECEIVE.CUST.ERROR
20130101000019,01.01.2013,00:00,0,0,0,0,0,6893,31
20130101000133,01.01.2013,00:01,0,0,0,0,0,6858,31
20130101000246,01.01.2013,00:02,0,0,0,0,0,6661,31
20130101000359,01.01.2013,00:03,0,0,0,0,0,6314,31
20130101000512,01.01.2013,00:05,0,0,0,0,0,6074,31

Awk Code

awk 'BEGIN {FS=","} ; END{print NF}' Input_file

<强>输出

10

Perl解决方案

#! /usr/bin/perl

use strict;
use warnings;

my @cols = split(',', $_);
my $n = @cols;
print "row $. =  $n columns\n";

将代码保存为code.pl并执行为

$> perl -n code.pl Input_file

Perl输出

row 1 =  10 columns
row 2 =  10 columns
row 3 =  10 columns
row 4 =  10 columns
row 5 =  10 columns
row 6 =  10 columns