Linux

比較下一行中的可用列表列印編號

  • July 25, 2020

我能夠在模式(**可供使用者使用:使用者選擇:)**之間提取數字(id),並匹配特定使用者是否從 id 列表中可用的內容中選擇了一個 id

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 模式來回答我的問題,將可用列表與 shell 腳本中選擇的 ids 進行比較

使用的 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
}

現在我在“使用者選擇:”行之後添加了一個帶有 globalID 和 localID 的文件,即

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

現在我想在每個匹配的每一行中列印 globalID 並且在兩個單獨的文件 file1 和 file2 中不匹配。file1 的輸出(匹配情況),其中特定使用者從可用列表中選擇的 id 的 globalID 儲存為:

1020
1030

file2 的輸出(不匹配情況),其中特定使用者未從可用列表中選擇的 id 的 globalID 儲存為:

1000

我試過了

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

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

但它沒有給出下一行源文件,而是給出下一行 sed 腳本輸出。

如果我們採用我發佈到您上一個問題的 awk 答案並對其進行調整以在到達空行或文件末尾時列印資訊,而不是在到達“已選擇”行時列印資訊(我應該首先這樣做但我很懶):

$ 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

引用自:https://unix.stackexchange.com/questions/600052