【发布时间】:2022-01-23 14:16:48
【问题描述】:
我只是在 Linux 中使用awk 过滤文件中的一些内容,这很简单,我能够得到所需的内容,但我几乎无法理解逻辑。我希望有人会为我和像我这样的人说清楚。
1- 文件内容如下...
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
check_ntlm_password: authentication for user [cnf76628] -> [cnf76628] -> [cnf76628] succeeded
2- 我要做的是获取[] 内的用户 ID,如下所示。
# awk -F'[][]' '/authentication for user/{print $2}' test_file
所以,我无法理解的是,为什么 $2 是用户 ID 作为它退出的第五列,但它需要 $2?这是否意味着我看起来像 authentication for user 的字符串被视为 $1 作为起点?。
任何清晰都会有所帮助。
【问题讨论】:
-
问题在于
[][]将记录/行与单括号]或[拆分为字段,因此ID 不在第5 个字段中,而是在第二个字段中。如果您删除-F'[][]',则将使用默认分隔符、空格,然后 ID 将出现在字段 5 中。 -
请运行以下测试:如果使用
$1替换$2会得到什么?如果使用$3替换$2会得到什么? -
这是一个经典案例,说明你脑海中的第 5 列与代码中的第 5 列之间的差异。在这种情况下,您会失去理智:-)。将您的脚本更改为
awk -F'[][]' '/authentication for user/{for (i=1; i<=NF; ++) print i, "<" $i ">"; exit}' test_file,以便您可以查看代码中的列。/authentication for user/测试没有任何用处,因为您发布的示例输入顺便说一句,您可以将其删除。 -
为了将来参考,请考虑提供一组样本输入数据,其中一些行符合您的处理要求,而另一些则不符合;您当前的样本包含重复 29 次的相同行,因此您的测试 -
/authentication for user/- 似乎是不必要的,因为它发生在每一行;我猜你的 real 数据有一些不包含/authentication for user/的行。以及不同的用户 ID(cnf76628除外),在这种情况下,如果您提供 真实 数据的样本,您可能会得到更好的响应