我能够提取模式之间的数字(id)(可供用户使用:和由用户选择:)并且如果特定用户从 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 脚本中选定的 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
}
现在我有一个在“由用户选择:”行后添加了全局 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
现在我想在每个匹配的每一行中打印 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 脚本输出的下一行。
答案1
如果我们采用我发布的 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