我有一个内容为
的文件/a/b/c
/a/b/d/xyz
/a/b/c/nmxnxlcs
...
我想从文件中删除字符串/a/b/
。
我想用shell脚本来做。
答案 0 :(得分:4)
使用sed
:
sed 's#/a/b/##' file
如果你想更新文件,
sed -i.bak 's#/a/b/##' file
它将创建备份file.bak
并使用新值更新file
。
作为mbratch comments in his answer,您可能只想将开始替换为/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
答案 1 :(得分:0)
虽然在问题陈述中没有提到,但该示例表明您只想删除字符串的开头处的字符串:
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