使用 grep/sed 提取字符串的一部分 [英] Extracting a part of String using grep/sed
本文介绍了使用 grep/sed 提取字符串的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 linux 中有一个文件,其条目与下面类似
I have a file in linux with similar entries as below
dn: CN=HP_NetworkSupport,OU=groups,DC=HDFCSLDM,DC=COM
dn: CN=Review users,OU=groups,DC=HDFCSLDM,DC=COM
我只想提取CN信息,直到第一个,例如:
I would like to extract only the CN information, till the first , for ex:
> HP_NetworkSupport
> Review users
在上述情况下到另一个文件.
in the above case to another file.
执行相同操作的命令是什么.
What would be command for doing the same.
推荐答案
Using awk
awk -F"=|," '{print $2}' file
HP_NetworkSupport
Review users
或
awk -F[=,] '{print $2}' file
HP_NetworkSupport
Review users
将分隔符设置为 、
或 =
,然后打印第二个字段.
Set the delimiter to ,
or =
, then print second field.
要处理带有逗号的字段,您应该使用 LDAP 解析器,但这应该可以工作.
To handel field with comma within, you should use a parser for LDAP, but this should work.
echo file
dn: CN=HP_NetworkSupport,OU=groups,DC=HDFCSLDM,DC=COM
dn: CN="Review, users",OU=groups,DC=HDFCSLDM,DC=COM
awk -F"CN=|,OU" '{print $2}' file
HP_NetworkSupport
Review, users
这篇关于使用 grep/sed 提取字符串的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文