重复从一行中提取组名?
Extract group name from one line repeatedly?
我得到了如下命令的输出。需要提取组名。
dsAttrTypeNative:memberOf: CN=Grupa_test,OU=Groups,DC=yellow,DC=com CN=Firefox_Install,OU=Groups,DC=yellow,DC=com CN=Network_Admin,OU=Groups,DC=yellow,DC=com
所以我想要这样的东西:
Grupa_test
Firefox_Install
Network_Admin
每次组的数量都会不同,所以我不确定如何实现。
您可以使用 GNU grep 来完成:
grep -oP '(?<=CN=)[^,]*' file
尝试使用以下 awk 一次。
awk -v RS='[ ,]' -v FS="=" '/CN=/{print }' Input_file
$ awk -v FPAT="CN=[^,]+" '{for(i=1;i<=NF;i++)print substr($i,4)}' Input_file
将每个匹配的 CN=[^,]+
个案例视为一个字段。对于每个匹配的字段,使用 substr($i,4)
过滤掉 CN=
,以打印所需的字符串。
$ awk -v RS=' ' -F'[=,]' 'NR>1{print }' file
Grupa_test
Firefox_Install
Network_Admin
以上内容适用于任何 awk。
我得到了如下命令的输出。需要提取组名。
dsAttrTypeNative:memberOf: CN=Grupa_test,OU=Groups,DC=yellow,DC=com CN=Firefox_Install,OU=Groups,DC=yellow,DC=com CN=Network_Admin,OU=Groups,DC=yellow,DC=com
所以我想要这样的东西:
Grupa_test
Firefox_Install
Network_Admin
每次组的数量都会不同,所以我不确定如何实现。
您可以使用 GNU grep 来完成:
grep -oP '(?<=CN=)[^,]*' file
尝试使用以下 awk 一次。
awk -v RS='[ ,]' -v FS="=" '/CN=/{print }' Input_file
$ awk -v FPAT="CN=[^,]+" '{for(i=1;i<=NF;i++)print substr($i,4)}' Input_file
将每个匹配的 CN=[^,]+
个案例视为一个字段。对于每个匹配的字段,使用 substr($i,4)
过滤掉 CN=
,以打印所需的字符串。
$ awk -v RS=' ' -F'[=,]' 'NR>1{print }' file
Grupa_test
Firefox_Install
Network_Admin
以上内容适用于任何 awk。