在awk脚本多个现场分离器 [英] Multiple Field separator in awk script
本文介绍了在awk脚本多个现场分离器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下code,让我输出文件中的线条和文字的数量。如何使用多一个FS可以用于计算总字符(文件分隔符)。??
(输出应该是一样的 WC文件命令
)
结果
BEGIN {
FS =\\ N |;}{ 对于(i = 1; I< = NF;我++)
W¯¯++
升++
}结束 {
打印总没有台词的:升;
打印总没有的话:W;}
解决方案
您可以使用内置的变量$ 0和功能长度
BEGIN {
FS =\\ N |;}{ 对于(i = 1; I< = NF;我++)
W¯¯++
升++
C + =长度($ 0)+1
}结束 {
打印总没有台词的:升;
打印总没有的话:W;
打印总没有字符的:C;}
编辑:添加+1长度占换行
i have following code that gives me output as number of lines and words in a file. how can i use one more FS(file separator) that can be used to count total characters.??
(the output should be same as wc file command
)
BEGIN {
FS="\n| ";
}
{
for(i=1;i<=NF;i++)
w++
l++
}
END {
print "Total no of Lines:"l;
print "Total no of words:"w;
}
解决方案
You can use the built in variable "$0" and function "length"
BEGIN {
FS="\n| ";
}
{
for(i=1;i<=NF;i++)
w++
l++
c += length($0)+1
}
END {
print "Total no of Lines:"l;
print "Total no of words:"w;
print "Total no of chars:"c;
}
Edit: Add +1 to length to account for newline
这篇关于在awk脚本多个现场分离器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文