代码之家  ›  专栏  ›  技术社区  ›  justaguy

sed从文件中就地删除url

sed
  •  0
  • justaguy  · 技术社区  · 9 年前

    我正在尝试使用从文件中就地删除特定url sed 当前,下面的命令正在运行,但url仍在文件中。谢谢:)

    列表

    xxxx://www.xxx.com/xxx/xx/xxx/file.html
    xxxx://www.xxx.com/xxx/xx/xxx/file1.bam
    xxxx://www.xxx.com/xxx/xx/xxx/file2.bam
    xxxx://www.xxx.com/xxx/xx/xxx/file1.vcf.gz
    xxxx://www.xxx.com/xxx/xx/xxx/file2.vcf.gz
    

    期望输出

    file.html
    file1.bam
    file2.bam
    file1.vcf.gz
    file2.vcf.gz
    

    sed命令

    sed -i -e 's|xxxx://www.xxx.com/xxx/xx/xxx/.*/||' /home/cmccabe/list  
    
    2 回复  |  直到 9 年前
        1
  •  1
  •   ghoti    9 年前

    sed脚本中列出了一个额外的目录。观察事物如何排列:

                 xxxx://www.xxx.com/xxx/xx/xxx/file.html
                                               ↓
    sed -i -e 's|xxxx://www.xxx.com/xxx/xx/xxx/.*/||' /home/cmccabe/list  
    

    您的替换似乎要求文件存在于 xxxx://www.xxx.com/xxx/xx/xxx/ 而不是在该目录本身内。删除额外的目录要求,或者使其成为可选的,那么sed替换就按原样工作。

    您正在寻找的可能是:

    sed -i -e 's|xxxx://www\.xxx\.com/xxx/xx/xxx/||'  /home/cmccabe/list
    

    sed -i -r -e 's|xxxx://www\.xxx\.com/xxx/xx/xxx/(.+/)?||'  /home/cmccabe/list
    

    或者只允许一级子目录:

    sed -i -r -e 's|xxxx://www\.xxx\.com/xxx/xx/xxx/([^/]+/)?||'  /home/cmccabe/list
    
        2
  •  0
  •   hek2mgl    9 年前

    它应该是:

    sed 's|.*/||' /home/cmccabe/list
    

    .*/ 从字符串的开始直到最后一次出现 / 并且没有任何东西代替它。

    顺便说一下,在 xargs 您还可以使用 basename 命令:

    xargs -a /home/cmccabe/list -n1 basename