我有一个文件,其内容是
/a/b/c
/a/b/d/xyz
/a/b/c/nmxnxlcs
...
我想/a/b/
从文件中删除字符串。我想用shell脚本来做。
与sed
:
sed 's#/a/b/##' file
如果要更新文件,
sed -i.bak 's#/a/b/##' file
它将创建一个备份file.bak
并file
使用新值进行更新。
正如mbratch 在他的回答中评论的那样,您可能只想替换以 .开头的行/a/b/
。在这种情况下,您可以使用:
sed 's#^/a/b/##' file
其中^
代表行首。
$ cat a
/a/b/c
/a/b/d/xyz
/a/b/c/nmxnxlcs
hello/a/b/aa
$ sed 's#/a/b/##' a
c
d/xyz
c/nmxnxlcs
helloaa
虽然问题陈述中没有提到,但该示例表明您只想删除位于行首的字符串:
sed 's:^/a/b/::' myfile.txt
这将改变:
/a/b/c/foo.txt
到:
c/foo.txt
并且会改变:
/a/b/c/x/a/b/foo.txt
到:
c/x/a/b/foo.txt
而不是:
c/xfoo.txt