次の行で利用可能なリスト印刷数を比較してください。

次の行で利用可能なリスト印刷数を比較してください。

パターン間の数字(id)を抽出できます(ユーザーが利用できるもの:そしてユーザーが選択しました:)特定のユーザーがIDリスト内のIDの1つを選択すると一致します。

Available for user:75=1654 at Time=5504.09 
Chosen by user:75=1655

Available for user:10=1300 at Time=550.09
Available for user:10=1301 at Time=550.09
Available for user:10=1303 at Time=550.09
Chosen by user:10=1301

私の質問に答えるためにsedモードを使用しました。利用可能なリストをシェルスクリプトで選択したIDと比較する

使用される sed モードは次のとおりです。

 /^Avail/{
    s/[^=]*=([^ ]*).*/\1/;H
}
/^Chosen/{
    s/.*=//;G;h;x;y/\n/,/
    s/,/ is ab in /;s/$/,/
    /(.*) is.*,\1,/s/ab/pre/
    s/(ab|pre) in ,(.*),/\1sent in \2/
    p;s/.*//;x
}

これで、「Selected by user:」行の後にグローバルIDとローカルIDが追加されたファイルが作成されました。

Available for user:75=1654 at Time=5504.09 
Chosen by user:75=1655
globalID=1000 localID=1655

Available for user:10=1300 at Time=550.09
Available for user:10=1301 at Time=550.09
Available for user:10=1303 at Time=550.09
Chosen by user:10=1301
globalID=1020 localID=1301 

Available for user:20=1400 at Time=550.09
Available for user:20=1501 at Time=550.09
Available for user:20=1503 at Time=550.09
Chosen by user:20=1503
globalID=1030 localID=1503

2つの別々のファイルfile1とfile2から、すべての一致と不一致のためにすべての行のglobalIDを印刷したいと思います。利用可能なリストで、特定のユーザーが選択したIDのglobalIDが次のように格納されているfile1(大文字と小文字の一致)の出力:

1020
1030

特定のユーザーが利用可能なリストから選択していないIDのglobalIDが次のように格納されているfile2(不一致)の出力:

1000

頑張った

sed -nrf script.sed input.txt  | grep absent -A1 > file2

そして

sed -nrf script.sed input.txt | grep present -A1 > file1

ただし、ソースファイルの次の行は提供されませんが、sedスクリプト出力の次の行は提供されます。

ベストアンサー1

投稿したawkの回答を取ると前の質問「select」行に到達するのではなく、空行またはファイルの終わりに達したときに情報を印刷するように調整します(これを最初に実行する必要がありましたが、怠惰です)。

$ cat tst.awk
BEGIN { FS="[:= ]" }
NF {
    if ( $1 == "Chosen" ) {
        user = $4
        chosen = $5
    }
    else {
        avail[$5]
    }
    next
}
{ prt() }
END {
    prt()
    printf "%d users chose available %d times and not available %d times\n", userCnt, availCnt, notAvailCnt
}

function prt() {
    if ( chosen in avail ) {
        availCnt++
        str = ""
    }
    else {
        notAvailCnt++
        str = " not"
    }
    userCnt++
    printf "User %s chose %s which was%s available\n", user, chosen, str
    delete avail
}

その後、新しい入力に対して実行すると、次のものが生成されます。

$ awk -f tst.awk file
User 75 chose 1655 which was not available
User 10 chose 1301 which was available
User 20 chose 1503 which was available
3 users chose available 2 times and not available 1 times

その後、これを調整してglobalIDを印刷できます。

$ cat tst2.awk
BEGIN { FS="[:= ]" }
NF {
    if ( $1 == "Chosen" ) {
        user = $4
        chosen = $5
    }
    else if ( $1 == "globalID" ) {
        globalID = $2
    }
    else {
        avail[$5]
    }
    next
}
{ prt() }
END {
    prt()
    printf "%d users chose available %d times and not available %d times\n", userCnt, availCnt, notAvailCnt
}

function prt() {
    if ( chosen in avail ) {
        availCnt++
        str = ""
    }
    else {
        notAvailCnt++
        str = " not"
    }
    userCnt++
    printf "User %s chose %s which was%s available, and had globalID %s\n", user, chosen, str, globalID
    delete avail
}

出力:

$ awk -f tst2.awk file
User 75 chose 1655 which was not available, and had globalID 1000
User 10 chose 1301 which was available, and had globalID 1020
User 20 chose 1503 which was available, and had globalID 1030
3 users chose available 2 times and not available 1 times

実際に出力したい内容を印刷するためにステートメントを変更するのがどれほど簡単かを確認してください。print(そしてgrepや他のものにパイプするのではなく、awkで行うのは簡単です)。

$ cat tst3.awk
BEGIN { FS="[:= ]" }
NF {
    if ( $1 == "Chosen" ) {
        user = $4
        chosen = $5
    }
    else if ( $1 == "globalID" ) {
        globalID = $2
    }
    else {
        avail[$5]
    }
    next
}
{ prt() }
END { prt() }

function prt() {
    if ( chosen in avail ) {
        result = "match"
    }
    else {
        result = "nomatch"
    }
    if ( target == result ) {
        print globalID
    }
    delete avail
}

$ awk -v target='match' -f tst3.awk file
1020
1030

$ awk -v target='nomatch' -f tst3.awk file
1000

おすすめ記事