繁体   English   中英

在Unix中使用awk结束文件

[英]end of a file using awk in Unix

我在使用awk时遇到问题。 从每个作为参数给出的文件中,打印出行长至少为10的行数。此外,打印该行的内容(首10个字符除外)。 在文件分析结束时,打印文件名和打印的行数。

到目前为止,这是我所做的:

{
if(length($0)>10)
{
 print "The number of line is:" FNR
 print "The content of the line is:" substr($0,10)
 s=s+1
}
x= wc -l //number of lines of file
if(FNR > x) //this is supposed to show when the file is over but it's not working
{           //I also tried if (FNR == 1) - which means new file
 print "This was the analysis of the file:" FILENAME
 print "The number of lines with characters >10 are:" s
}
}

这将打印文件的名称以及每行至少包含10个字符的行数,但是我想要这样的内容:

print "The number of line is:" 1
print "The content of the line is:" dkhflaksfdas
print "The number of line is:" 3
print "The content of the line is:" asdfdassaf
print "This was the analysis of the file:" awk.txt
print "The number of lines with characters >10 are:" 2

这是您需要的:

length($0) >= 10 {                             
    print "The number of line is:",FNR 
    print "The content of the line is:",substr($0,11)
    count++                                              
}
ENDFILE {                        
    print "This was the analysis of the file:",FILENAME
    print "The number of lines with characters >= 10 are:",count
    count = 0
}

将其另存为script.awk并像awk -f script.awk file1 file2 file3一样运行。

笔记:

  • 对于线长度,要求the number of lines that has the length at least 10>=10

  • except the fist 10 characters您希望从11开始以substr($0,11)

  • 条件length($0) >= 10应该在代码块外部完成。

  • 您需要使用特殊的ENDFILE块在每个文件的末尾打印分析。

  • 您需要重置count和每个文件的末尾,否则您将获得所有文件的总计。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM