我试图在sed追加中使用一个变量,然后碰到一个问题。
以下命令按预期工作:
sed -i "\:#file = /mnt/var/log/hadoop-yarn/containers/application_1495965866386_0001/container_1495965866386_0001_01_000002/stderr:a file = /path/to/other/file" /etc/conf/service.conf但是,如果我用变量替换模式,就会碰到一个错误:
$ echo $item
#file = /mnt/var/log/hadoop-yarn/containers/application_1495965866386_0001/container_1495965866386_0001_01_000002/stdout
$ sed -i "\:$item:a file = /path/to/other/file" /etc/conf/service.conf
sed: -e expression #1, char 122: unterminated address regex编辑获取更多信息:因此“item”变量是从数组中填充的。该数组是从readarray和grep创建的:
$readarray LINES < <(grep "#file = /mnt/var/" /etc/conf/service.conf)
$item=${LINES[1]}
$echo $item
#file = /mnt/var/log/hadoop-yarn/containers/application_1495965866386_0001/container_1495965866386_0001_01_000002/stdout但是,我发现如果手动填充“item”,它就会工作,例如:
$item="#file = /mnt/var/log/hadoop-yarn/containers/application_1495965866386_0001/container_1495965866386_0001_01_000002/stdout"
$sed -i "\:$item:a file = /path/to/other/file" /etc/conf/service.conf
$因此,某种奇怪的事情似乎发生在readarray/grep上。
发布于 2017-05-30 09:23:07
所以这里的问题是换行符作为grep的一部分被拉进来了。这就是为什么手动填充$item -没有“\n”
感谢艾德·莫顿为我指明了正确的方向。而
echo "$item" | cat -v没有显示在readarray命令中添加“-t”以修剪换行符的任何内容:
$readarray -t LINES < <(grep "#file = /mnt/var/" /etc/conf/service.conf)在那之后,一切都如期而至。
https://stackoverflow.com/questions/44245956
复制相似问题