我需要找到包含特定字符串模式的所有文件.想到的第一个解决方案是使用带有xargs grep的find管道:
find . -iname '*.py' | xargs grep -e 'YOUR_PATTERN'
Run Code Online (Sandbox Code Playgroud)
但是如果我需要找到跨越多行的模式,我就会被卡住,因为vanilla grep找不到多行模式.
我需要通过bash脚本检查一个文件是否在另一个文件中.对于给定的多行模式和输入文件.
返回值:
我想接收状态(在grep命令中如何)0如果找到任何匹配,如果没有找到匹配则为1.
图案:
说明
只有以下示例才能找到匹配项:
pattern file1 file2 file3 file4
222 111 111 222 222
333 222 222 333 333
333 333 444
444
Run Code Online (Sandbox Code Playgroud)
以下不应该:
pattern file1 file2 file3 file4 file5 file6 file7
222 111 111 333 *222 111 111 222
333 *222 222 222 *333 222 222
333 333* 444 111 333
444 333 333
Run Code Online (Sandbox Code Playgroud)
这是我的脚本:
#!/bin/bash
function writeToFile {
if [ -w "$1" ] ; then
echo "$2" >> "$1"
else
echo -e "$2" …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用在Cygwin上对此问题的最佳答案中指定的pcregrep 。我的环境是运行Cygwin V 1.7.20(0.266 / 5/3)的Win7 64bit。
使用cygcheck -p pcregrep
我得到:
Found 6 matches for pcregrep
libpcre-devel-8.37-1 - libpcre-devel: Perl Compatible Regular Expressions library development (installed binaries and support files)
libpcre-devel-8.37-2 - libpcre-devel: Perl Compatible Regular Expressions library development (installed binaries and support files)
pcre-debuginfo-8.37-1 - pcre-debuginfo: Debug info for pcre (installed binaries and support files)
pcre-debuginfo-8.37-2 - pcre-debuginfo: Debug info for pcre (installed binaries and support files)
pcre-8.37-1 - pcre: Perl Compatible Regular Expressions utilities (installed …
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个脚本来帮助在与正则表达式匹配时突出显示。以下是我现在所做的示例。
var text = "SOMETHING ~WEIRDs~ AND Not WEIRD";
var regexp = /\b(WEIRD)\b/
var matches = text.match(regexp);
for (i = 0; i < matches.length; i++) {
var replace_all = RegExp(matches[i] + "(?![^<]*>|[^<>]*<\/)", "ig");
text = text.replace(eval(replace_all), "<span style='background- color:yellow'>" + matches[i] + "</span>");
}
console.log(text);
Run Code Online (Sandbox Code Playgroud)
上面代码的输出是
SOMETHING ~<span style='background- color:yellow'>WEIRD</span>s~ AND Not <span style='background- color:yellow'>WEIRD</span>
Run Code Online (Sandbox Code Playgroud)
我想要的输出是
SOMETHING ~WEIRDs~ AND Not <span style='background- color:yellow'>WEIRD</span>
Run Code Online (Sandbox Code Playgroud)
我想知道的是,无论如何写一个正则表达式包含正则表达式和提供的单词?或者有任何其他方法可以解决这个不正确的替换问题。
我正在使用'pcregrep'在UNIX中实现多行搜索.我希望匹配在第一次出现时停止.
例如,如果我在文件中有下面的文字:
mynameishin
hahahain
internetin
fdhsufsdhuiain
djbssdvbsbsduiain
sduigsyubsuvasdyivsyifvasin
Run Code Online (Sandbox Code Playgroud)
我想打印到'name'之后第一次出现'ain'.所以我只需要打印:
mynameishin
hahahain
Run Code Online (Sandbox Code Playgroud)
我正在执行:
pcregrep -M 'name.*ain' *.txt
Run Code Online (Sandbox Code Playgroud)
但是我得到的输出为:
mynameishin
hahahain
internetin
fdhsufsdhuiain
djbssdvbsbsduiain
Run Code Online (Sandbox Code Playgroud) 我有一个多线匹配pcregrep命令,我想将其转换为awk或sed命令,因为我需要它在pcregrep不可用的机器上(OS X).
原始命令:
ifconfig -a | pcregrep -M '^[a-z].*\n(\t[^\n]+\n)+\t[^\n]+baseTX' | grep -oE "^([a-z]+[^:]+)"
Run Code Online (Sandbox Code Playgroud)
它输出包含字符串"baseTX"的接口的名称(我发现的唯一方法是可靠地找到MacBook上的以太网接口的名称).就我而言,"en4".
输入文本如下所示:
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
options=3<RXCSUM,TXCSUM>
inet6 ::1 prefixlen 128
inet 127.0.0.1 netmask 0xff000000
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
nd6 options=1<PERFORMNUD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether xx:xx:xx:xx:xx:xx
inet6 xxxx::xxxx:xxxx:xxxx:xxx%en0 prefixlen 64 scopeid 0x4
inet 10.xxx.xxx.xx netmask 0xffffff00 broadcast 10.xxx.xxx.255
inet6 xxxx:xxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx prefixlen 128
nd6 options=1<PERFORMNUD>
media: autoselect
status: active
en5: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
options=60<TSO4,TSO6>
ether xx:xx:xx:xx:xx:xx
media: …
Run Code Online (Sandbox Code Playgroud)