我正在使用 CLI 和确定的库,我想查找某个用户名是否存在。 我尝试了下面的命令,但尽管我确定用户名存在,但我没有打印该行。 假设用户名是
xyz
,列名是Username
(其他列是User Id
等)
det user ls | grep -E "^Username: (${xyz})$"
det user ls | awk "/Username: ${xyz}$/ {print $0}"
det user ls | awk '$3 == "xyz"'
$3
因为它是第三列。
澄清一下,
det user ls
确实有效!
我希望它是一个单行代码而不是一个脚本(以避免上传到系统)。
我觉得逻辑有问题?还是我搜索错误?
提前致谢
编辑1: 以下是
det user ls
的示例:
det user ls
User Id | Name | Username | Active
-----------+-----------+----------------+----------
325 | abc | xyz | False
在您提供的示例输入中:
$ cat file
User Id | Name | Username | Active
-----------+-----------+----------------+----------
325 | abc | xyz | False
要输出的值,
xyz
是第5个空格分隔字段,而不是第3个,所以:
$ cat file | awk 'NR>2{print $5}'
xyz
或者如果您想使用由可选空格包围的
|
作为字段分隔符:
$ cat file | awk -F'[[:space:]]*[|][[:space:]]*' 'NR>2{print $3}'
xyz
当然将
cat file
替换为 det user ls
。上面的代码可以使用任何 POSIX awk 来工作。