如何在Linux中大于8000的制表符之间计数字符

问题描述 投票:-3回答:2

例如,我有一个用制表符分隔的文件file.dat.gz。

例如

hi ^ Iapple ^ Itoast

是否可以使用wc在选项卡之间进行计数?

由于上述计数为2、5、5 wc将返回0,但是如果它大于8000,它可以列出1还是精确值?

bash vi wc
2个回答
0
投票

不需要wc

$IFS之前的行上暂时将read设置为选项卡。这将排除空格(参见“ a b c”)。读入数组,然后循环。

测试长度> 8000,并相应地表现。这是一个您应该能够适应的简单示例。

 $: IFS="   " read -a lst < in
 $: for x in "${lst[@]}"
 >  do l="${#x}"
 >     if (( l > 8000 ))
 >     then x='<too long>'
 >     fi
 >     printf "'%s' = %d\n" "$x" "$l"
 >  done
 'hi' = 2
 'a b c' = 5
 'apple' = 5
 '<too long>' = 10000
 'toast' = 5

如果要处理一个非常大的文件,请将其写入awkperl,以获得更好的性能。


0
投票
 awk -F'\t' '{for (i=1; i<=NF;i++) if(length($i)>8000)  print $i}'

演示

$echo -e "hi\tapple\ttoast" |  awk -F'\t' '{for (i=1; i<=NF;i++) if(length($i)>2)  print $i}' 
apple
toast
$echo -e "hi\tapple\ttoast" |  awk -F'\t' '{print length($1) , length($2) , length($3)}' 
2 5 5
$echo -e "hi\tapple\ttoast"
hi  apple   toast
$echo -e "hi\tapple\ttoast" |  awk -F'\t' '{print length($1) , length($2) , length($3)}' 
2 5 5
$echo -e "hi\tapple\ttoast" |  awk -F'\t' '{for (i=1; i<=NF;i++) if(length($i)>2)  print $i}' 
apple
toast
$
© www.soinside.com 2019 - 2024. All rights reserved.