ファイルの内容を取得するシェルスクリプト

ファイルの内容を取得するシェルスクリプト

「data_logs」というディレクトリに、毎分ログファイルを生成します。

ログファイル名:

abc.log.2019041607
abc.log.2019041608..

ログファイルの内容は次のとおりです。

R_MT|D:1234|ID:413|S:1
R_MT|D:1234|ID:413|S:1
R_MT|D:1234|ID:413|S:1
R_MT|D:1234|ID:413|S:1
R_MT|D:1234|ID:413|S:1
R_MT|D:1234|ID:413|S:1
R_MT|D:1234|ID:413|S:1
R_MT|D:1234|ID:413|S:1
R_MT|D:1234|ID:413|S:1
R_MT|D:1234|ID:413|S:1

R_MT|D:1234|ID:413|S:0
R_MT|D:1234|ID:413|S:0
R_MT|D:1234|ID:413|S:0
R_MT|D:1234|ID:413|S:0 
R_MT|D:1234|ID:413|S:0

k_MT|D:1234|ID:414|S:1
k_MT|D:1234|ID:414|S:1
k_MT|D:1235|ID:413|S:1
k_MT|D:1235|ID:413|S:1

私は実行されたときに過去5分間に生成されたファイル(最後に5つのファイル、毎分1つのファイルを生成)を探して、各ファイルを1つずつ開いて処理するシェルスクリプトを作成しています。つまり、出力を生成します。R_MT|D:1234|ID:413この組み合わせの 'Committed' 列に格納された 's' の合計数 0 と Uncommitted 列に格納された 's'=1 を含む txt ファイル

私のoutput.txtは次のようになります。

Type, Number,ID,submitted,notsubmitted
R_MT,D:1234,ID:413,5,10
R_MT,D:1234,ID:414,0,2
R_MT,D:1235,ID:413,0,2

私はこれを使用してコミットされた値とコミットされていない値を取得します。

zcat abc.log.2019041607.gz |grep "R_MT"|awk -F"|" '{print $2","$3","$4}'|sort|uniq -c
      5 D:1234,ID:413,S:0
     10 D:1234,ID:413,S:1
      2 D:1234,ID:414,S:1
      2 D:1235,ID:413,S:1

上記のコマンドを使用して数を取得しましたが、出力ファイルの「コミット済み」および「コミットされていない」フィールドに書き込むことができるように変数に割り当てる方法がわかりませんが、過去5分間のドキュメントですか?

助けてください、本当にありがとう!

ベストアンサー1

input.logが入力のときにgawkで動作するこれがありますが、それでもawkとして理解しようとしています。

cat input.log |
 gawk -F"|" \
    #print the header
 'BEGIN{print"Type, Number,ID,submitted,notsubmitted"}
    #only work on non empty lines
 NF>0{ 
     #create an ID from the first three fields
    n=$1","$2","$3; 
        #everytime the ID pops up, increment subindex 1 or 2 depending on the value of field 4
        if($4=="S:1"){
            array[n][2]++}
        else{
            array[n][1]++}
}
     #print the final array
END{for(i in array){
       #if the value has never been seen declare it to be zero
        if(array[i][1]){
            m=array[i][1]}
        else {
            m=0}
        if(array[i][2]){
            n=array[i][2]}
        else {
            n=0}
    print i","m","n}
}'

おすすめ記事