Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.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
Bash 使用“grep”提取精确匹配的字符串_Bash_Shell - Fatal编程技术网

Bash 使用“grep”提取精确匹配的字符串

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

我尝试用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.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