linux - Putty 搜索 - 字符串后跟一个但不是两个符号

标签 linux shell unix

我正在路径/home/folder/中搜索包含多个 .txt 和 .xls 文件的字符串。存在定义变量 xyz 的实例。一个例子是: “xyz = 如果……否则……”

也存在将变量 xyz 用作条件的实例。一个例子是: “.... xyz==1 ...”

我想找到定义了 xyz 的所有实例,而不是将 xyz 用作条件的实例。我尝试了以下代码但没有任何效果......

grep --include=\*.{txt,xls} -rnw '/home/folder/' -e 'xyz\s*\=(?!=)'
grep --include=\*.{txt,xls} -rnw '/home/folder/' -e 'xyz\s*\=(?!\=)'
grep --include=\*.{txt,xls} -rnw '/home/folder/' -e 'xyz\s*\=[^=]'

我认为我的语法是正确的,但没有返回结果。我尝试使用不同的 shell ,但没有任何区别。在这种情况下,我将如何搜索字符串?

编辑:我知道目录中的文件中存在“xyz = ifelse”实例。当我使用以下命令搜索时会出现这些:

grep --include=\*.{txt,xls} -rnw '/home/folder/' -e 'xyz\s*\='

最佳答案

Marktink 的提示都是正确的:您需要添加 -P 选项并删除 -w,使用 \b 代替:

$ cat test.txt 
xyz = 14
xyz == 15
xyz=1
xyz==2
xyzz=4
zxyz=5

# No PCRE, no (correct) result
$ grep -e "xyz\s*\=(?!=)" test.txt 

# Missing instances without space between operator and value here
$ grep -P -w -e "xyz\s*\=(?!=)" test.txt 
xyz = 14

# Not checking for word boundary returns false positives
$ grep -P -e "xyz\s*\=(?!=)" test.txt 
xyz = 14
xyz=1
zxyz=5

# This is the result you want to see
$ grep -P -e "\bxyz\s*\=(?!=)" test.txt 
xyz = 14
xyz=1

# The same without PCRE
$ grep -e "\<xyz\s*\=[^=]" test.txt
xyz = 14
xyz=1

关于linux - Putty 搜索 - 字符串后跟一个但不是两个符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54542334/

相关文章:

linux - 替换一行libc.so

linux - 在 UNIX 时间戳 Shell/Bash 中使用时区转换日期

shell - 需要使用 Hive 将变量从 Shell Action 传递给 Oozie Shell

unix - 当您在进程上按 ctrl+z 时会发生什么?

c - 透明代理 - 如何在不修改的情况下将套接字传递到本地服务器?

linux - 使用 Qt 显示网络摄像头视频

linux - 语法错误: unexpected end of file when sourcing a file in bash

android - 是否可以通过 adb shell 启动 Activity 并接收其响应?

windows - perl 中的 fork 实现

linux - 命令管道在 *NIX 中如何工作?