AWKの特定の列数を増やす方法

AWKの特定の列数を増やす方法

合計46列を含む大きなテキストファイルがあります。

入力する

Column1 Column2 ... Column46

17列から46列まで繰り返される回数を列挙して計算する必要があります。たとえば、

私のファイルは次のとおりです。

Column17 Column18 Column19 Column20 Column21 Column22 Column23 ... Column46

Column17 Column18 Column19 Column20 Column21 Column22 Column23 ... Column46
Column17 Column18          Column20          Column22
         Column18          Column20
                           Column20

希望の出力:

1 Column17 - 2 times
2 Column18 - 3 times
3 Column19 - 1 time
4 Column20 - 4 times
5 Column21 - 0 times
6 Column22 - 2 times
7 Column23 - 1 time
    " "    - N times
29 Column46 - 1 times

私のawkコマンド:

awk '{n=1;if(NR==1)n=25;for(i=n;i<=NF;i++) a[$i]++} END{for(val in a) print val,a[val]}' filelog.txt

実際の出力:

Column1 ... Column46
bla 1 blaN...3 bla 3 
bla 3 blaN...2 bla 5
bla 7 blaN...4 bla 7

ファイル全体からすべての文字列を取得し、繰り返される回数を計算します。 たとえば、

Column1 //There are 54 lines, takes all words/strings of that column
bla 3 //The 3 and 4, is the number of repetitions.
bla 4

しかし、私の順序は重要です。みんな列(1、まで46)そして各フレーズ自体は17から46の数字です。希望の出力私が持っているのと同じコマンドを使ってこれを作成する方法はありますか?それとも他の人が作るべきですか?

ベストアンサー1

使用命じる-N列挙型オプション。

指定してn=17そこから始めたいからです。

次のコマンドを使用します。

cat -n <(awk '{n=17;if(NR==1)n=25;for(i=n;i<=NF;i++) a[$i]++} END{for(val in a) print val,a[val]}' filelog.txt)

おすすめ記事