Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 每行只打印一个匹配项_Regex_Awk_Grep_Sh - Fatal编程技术网

Regex 每行只打印一个匹配项

Regex 每行只打印一个匹配项,regex,awk,grep,sh,Regex,Awk,Grep,Sh,我有一个这样的日志 3>DirectMicrophone.obj : error LNK2019: unresolved external symbol _DirectSoundCaptureEnumerateW@8 referenced in function "private: void __thiscall DirectMicrophoneManager::getDevices(void)" (?getDevices@DirectMicrophoneManager@@AAEXXZ)

我有一个这样的日志

3>DirectMicrophone.obj : error LNK2019: unresolved external symbol _DirectSoundCaptureEnumerateW@8 referenced in function "private: void __thiscall DirectMicrophoneManager::getDevices(void)" (?getDevices@DirectMicrophoneManager@@AAEXXZ)
3>DirectMicrophone.obj : error LNK2001: unresolved external symbol _DSDEVID_DefaultVoiceCapture
3>DirectMicrophone.obj : error LNK2001: unresolved external symbol _IID_IDirectSoundCapture
3>DirectSoundPlayer.obj : error LNK2019: unresolved external symbol _DirectSoundCreate@12 referenced in function "private: bool __thiscall DirectSoer::CreateDirBuffers(void)" (?CreateDirBuffers@DirPlayer@@AAE_NXZ)
libmodule-text.lib(CTS_Support.obj) : error LNK2001: unresolved external symbol _delete "void __cdecl operator delete(void *)" (??3@YAXPAX@Z)
3>rtmfp_interface.obj : error LNK2001: unresolved external symbol __CIcos
我只想提取日志中突出显示的符号。对于同一个问题,有两种方法

打印外部符号后每行的第一个字 以开头的每行打印第一个单词_ 我使用脚本尝试了第二种方法

egrep -o "(\s(_\S+))" <log_file> 
您可以使用grep-oP命令:

或使用awk:


如果您的grep支持-p,那么您可以使用下面的正则表达式

grep -oP 'external symbol\K\h_\S+' file
使用呆呆

awk 'match($0,/_([^*]+)/,a){print a[1]}' file
如果它必须是外部符号后的下一个单词,那么这将起作用

awk 'match($0,/external symbol[^[:alnum:]]+([[:alnum:]]+)/,a){print a[1]}' file
同样,正如您已经指定了一个单词的组成,您可以这样做来包括@

由于RS中有多个字符,另一个gnu awk

好的,试试这个:

sed的/[^\u]*\\\[^\b\t\s]*\[^\u]*/\1XXX/;s/\.*\XXX.*/\1/;s/*\\\/\1/'记录数据


你的预期产出是多少?是否要输出包含星号和@?是否要在正则表达式中提及字符串外部符号?它可以工作,但不适用于:在外部符号后每行打印第一个单词。在本例中,它正好是在后面。它在第二位说,这只是试图在u后面找到单词的一种方式。我将在一分钟内添加一个:我在OSX awk和gnu awk上进行了测试,以生成此输出。我在gnu awk 3.1.5上,它只打印空行,非常确定它不喜欢多字符FS。事实上,使用multichar FS没问题,使用该FS时似乎只需3美元。我有GNU Awk 4.1.0和OSX Awk版本20070501,Awk脚本将以与所有现代Awk完全相同的方式运行,因为它使用完全可移植和POSIX定义的结构。据我所见,它将根据给定的示例输入生成发布的输出,除了2个尾随*s已消失,但可能OP在发布此答案后编辑了问题,因此如果它没有生成预期的输出,则您的输入文件与发布的示例有所不同。@anubhava awk脚本工作正常很好,除了符号前面有其他单词的情况。例如,在log abcj.df.Ob:错误未解决的符号XXXX中考虑这一行。在这种情况下,脚本将打印Def.obj。此外,它还删除了3;。这不起作用,因为head-1只返回第一个结果。
grep -oP 'external symbol\K\h_\S+' file
awk 'match($0,/_([^*]+)/,a){print a[1]}' file
awk 'match($0,/external symbol[^[:alnum:]]+([[:alnum:]]+)/,a){print a[1]}' file
awk 'match($0,/external symbol[^[:alnum:]]+([[:alnum:]@]+)/,a){print a[1]}' file
awk -v RS='external symbol \\*\\*_' -F'\\*\\*' 'NR>1{print $1}' file
DirectSoundCaptureEnumerateW@8
DSDEVID_DefaultVoiceCapture
IID_IDirectSoundCapture
DirectSoundCreate@12
It says 
[^_]* don't match _ any amount 
\( start a hold pattern 
_ start with underbar and terminate at any boundary \b or tab \t or space \s  
end hold \) 
[^_]* don't match _ any amount 
then replace the previous with a boundary marker XXX 
and delete everything else - keeping only the first match.