0

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
4

1 に答える 1

1

これはあなたが必要とするものです:

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ます。そうしないと、すべてのファイルの現在の合計が得られます。

于 2013-03-31T13:46:02.710 に答える