我有一些日志文件,我将它们用于特定的字符串:
grep UncategorizedLdap *
我得到~33个结果:
2009-01-01:org.springframework.ldap.UncategorizedLdapException: Uncategorized exception occured during LDAP processing; nested exception is javax.naming.NamingException: [LDAP: error code 1 - Operations Error]; remaining name 'ou=ABC'
2009-01-02:org.springframework.ldap.UncategorizedLdapException: Uncategorized exception occured during LDAP processing; nested exception is javax.naming.NamingException: [LDAP: error code 1 - Operations Error]; remaining name 'ou='
2009-01-02:org.springframework.ldap.UncategorizedLdapException: Uncategorized exception occured during LDAP processing; nested exception is javax.naming.NamingException: [LDAP: error code 1 - Operations Error]; remaining name 'ou=ABD'
2009-01-03:org.springframework.ldap.UncategorizedLdapException: Uncategorized exception occured during LDAP processing; nested exception is javax.naming.NamingException: [LDAP: error code 1 - Operations Error]; remaining name 'ou=ABE'
...
如何修改grep调用以仅返回
ou=ABC
ou=
ou=ABD
ou=ABE
...
答案 0 :(得分:1)
您可以将输出传递给perl:
grep UncategorizedLdap * | perl -lpe '($_) = /(ou=\w*)/'
如果您没有安装Perl,这是一个仅限bash的解决方案:
grep UncategorizedLdap * |
while read line; do
line=${line#*\'}
line=${line%\'*}
echo $line
done
答案 1 :(得分:1)
如果您有GNU grep
(在Cygwin中就是这种情况):
grep -Po "UncategorizedLdap.*'\Kou.*?(?=')" *
将选择行并在一个命令中选择字符串。它使用支持外观的Perl兼容正则表达式。 \K
之前的部分是一个lookbehind,用于进行匹配,但不包含在输出中。 (?=)
中的字符串在这种情况下是单引号是前瞻,它也不包含在输出中。 -o
选项仅打印行的匹配部分(不从输出中排除)。
答案 2 :(得分:1)
如果您使用bash,那么有时您可以使用bash
for file in *
while read -r line
do
case "$line" in
*UncategorizedLdap* )
line=${line#*\'}
line=${line%\'*}
echo $line ;;
esac
done < $file
done