Bash 使用“grep”提取精确匹配的字符串
我尝试用grep从mkv.md中提取扩展名为.mkv的文件 输出中有一个前导周期,应该手动删除。 我想要的结果是没有周期符号Bash 使用“grep”提取精确匹配的字符串,bash,shell,Bash,Shell,我尝试用grep从mkv.md中提取扩展名为.mkv的文件 输出中有一个前导周期,应该手动删除。 我想要的结果是没有周期符号 /Volumes/Transcend/Downloads/The.Adventure.of.English.Ep4.mkv /Volumes/Transcend/Downloads/The.Adventure.of.English.Ep5.mkv /Volumes/Transcend/Downloads/The.Adventure.of.Engli
/Volumes/Transcend/Downloads/The.Adventure.of.English.Ep4.mkv
/Volumes/Transcend/Downloads/The.Adventure.of.English.Ep5.mkv
/Volumes/Transcend/Downloads/The.Adventure.of.English.Ep6.mkv
我试着得到了同样的结果
grep -i '\/.*\.mkv' mkv.md
./Volumes/Transcend/Downloads/The.Adventure.of.English.Ep4.mkv
./Volumes/Transcend/Downloads/The.Adventure.of.English.Ep5.mkv
./Volumes/Transcend/Downloads/The.Adventure.of.English.Ep6.mkv
...
如何解决此问题?如果可以使用其他命令,cut可以帮助:
$ grep -i mkv mkv.md | cut -c 2-
/Volumes/Transcend/Downloads/The.Adventure.of.English.Ep4.mkv
/Volumes/Transcend/Downloads/The.Adventure.of.English.Ep5.mkv
/Volumes/Transcend/Downloads/The.Adventure.of.English.Ep6.mkv
您可以使用剪切:
cut-c2--从第二个字符到字符串的末尾进行剪切
在你的例子中:
grep -i 'mkv' mkv.md | cut -c 2-
更多示例如下:
您可以使用-o
-o、 -仅匹配:仅打印行的匹配部分
在您的示例中:
grep -io '\/.*\.mkv' mkv.md
#please note that you can combine multiple options as the above, or
grep -i -o '\/.*\.mkv' mkv.md
mkv.md的内容是什么?
grep -io '\/.*\.mkv' mkv.md
#please note that you can combine multiple options as the above, or
grep -i -o '\/.*\.mkv' mkv.md