我需要使用ripgrep来找到特定的模式。这将是一个描述化学反应的字符串。ripgrep的输出如下所示:
~ rg -U --only-matching --vimgrep --replace='$1' '```smiles\n(.+)\n```'
Testing Smiles.md:5:1:OC(=O)CCC(=O)O>CCO.[H+]>CCOC(=O)CCC(=O)OCC
Another Smiles.md:5:1:CO>BrP(Br)Br>CBr凉爽的!但是现在我需要使用Python脚本过滤掉这些结果。因此,我可以将这些结果通过管道传输到Python,然后从stdin中读取。但是有一个问题:我如何保证分隔符?如果我编写Python脚本,将第三个冒号之后的所有内容都作为输入字符串,如何保证文件本身的名称中没有冒号?当我通过管道连接到python时,如何正确地将文件名与匹配项分开?
谢谢,
发布于 2020-12-02 11:01:57
在ripgrep执行之前添加一个pre-check阶段怎么样:
dir="." # assign to your target directory
for f in "$dir"/*.md; do
if [[ $f = *:* ]]; then # if the file contains ":"
badlist+=("$f") # then add the filename to the badlist
fi
done
if (( ${#badlist[@]} > 0 )); then # if the badlist is not empty...
echo "These file(s) contain a colon character. Rename them and run again."
printf " %s\n" "${badlist[@]}"
exit
fi
rg -U --only-matching --vimgrep --replace='$1' '```smiles\n(.+)\n```' "$dir"/*.md | python-script如果文件名中包含:,上面的代码会在主ripgrep阶段之前立即停止执行。如果找到,则可以重命名文件名。
https://stackoverflow.com/questions/65100436
复制相似问题