.
的是在sed
意义 '的任何字符' 元字符。为了抑制它的特殊含义,用反斜杠转义:
sed -e 's%/\.\./%/%g' $src_file > $temp_file
注意,你指的是不同的文件,你消除/../
这样了。为了引用与以前相同的名称(在没有符号链接的情况下,这会使事情变得复杂),您需要在/../
之前删除目录组件。因此:
some/../path/to/file
path/to/file
指的是同一个文件,假设some
是一个目录,而不是一个符号链接其他地方,但总的来说,some/path/to/file
是一个不同的文件(虽然符号链接可以用来混淆这一说法)。
$ x="some/../path/to/file
> some/ab/path/to/file
> /some/path/../to/another/../file"
$ echo "$x"
some/../path/to/file
some/ab/path/to/file
/some/path/../to/another/../file
$ echo "$x" | sed -e 's%/\.\./%/%g'
some/path/to/file
some/ab/path/to/file
/some/path/to/another/file
$ echo "$x" | sed -e "s%/\.\./%/%g"
some/path/to/file
some/ab/path/to/file
/some/path/to/another/file
$ echo "$x" | sed -e s%/\.\./%/%g
some/path/file
some/path/file
/some/path/to/another/file
$ echo "$x" | sed -e s%/\\.\\./%/%g
some/path/to/file
some/ab/path/to/file
/some/path/to/another/file
$
注意在echo
命令谨慎使用双引号的变量"$x"
左右。我可以在作业中使用单引号或双引号,并得到相同的结果。
在标准的sed
(外壳为/bin/sh
,又名bash
3.2.x)的Mac OS X 10.7.4上测试,但结果在任何系统上都是一样的。
这不符合要求。它也删除了我不想要的“/ tx /”。我只想要该序列“/../”应该替换为“/”。 – Niranjan
@Niranjan:你在'sed'脚本中使用单引号还是双引号?它们不可互换 - 至少,不是在这方面。或者你还没有使用任何引号?如果你不使用单引号,你必须使用更多的反斜杠。根据经验,除非需要插入shell变量,否则将'sed'脚本括在单引号中。 –
得到了点乔纳森。我根本没有在SED命令中使用任何引号。你的建议是建议使用单引号。单引号'sed -e'%/ \。\ ./%/% g'$ {SRC_FILE}> $ {TEMP_FILE}'解决了这个问题。感谢你的帮助。非常感谢你。 – Niranjan