Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash将条目添加到映射_Bash_Sed - Fatal编程技术网

Bash将条目添加到映射

Bash将条目添加到映射,bash,sed,Bash,Sed,我有一个(Nginx)配置文件,比如 types { text/html html htm shtml; text/css css; text/xml xml; } 我想在其中附加一个条目(文件中的任何位置),例如 types { text/html html htm shtml; text/css css; text/xml xml; image/gif gi

我有一个(Nginx)配置文件,比如

types {
    text/html      html htm shtml;
    text/css       css;
    text/xml       xml;
}
我想在其中附加一个条目(文件中的任何位置),例如

types {
    text/html      html htm shtml;
    text/css       css;
    text/xml       xml;
    image/gif      gif;
}
我试着用sed作为例子

我正在用fish在MacOS上测试,但是在bash上得到了相同的错误

我可以在这里使用哪个命令?
非常感谢

这将执行就地编辑,在包含
}
和可选空白的任何行之前插入一行
hello

perl -p -i -l -e 'print "hello" if /^\s*}\s*$/' mimes.types
上述限制:如果有多个
}
行,那么您将得到多个
hello

即使有多行
}
行,以下内容也只打印hello一次:

perl -p -i -l -e 'print("hello"), $done=1 if /^\s*}\s*$/ && !$done' mimes.types 
说明:

  • -p
    循环输入,在执行perl代码后打印每个输入行
  • -i
    就地编辑文件(删除此项以写入标准输出)
  • -l
    从输入中去除换行符并将其附加到输出中
  • -e'print“hello”if/^\s*}\s*$/”
    如果包含读入的行的默认变量(
    $\u
    )与正则表达式匹配,则执行
    print

在第二个示例中,变量
$done
没有显式初始化,而是
$done
测试使用
undef
值与显式
0

时使用的值相同,我认为错误源于GNU
sed
(在大多数Linux系统上使用)和BSD
sed
(在MacOS上使用)之间的一个微小差异。后者期待一个新的机会。试试下面的方法

sed -i .bak "\$i hello" mimes.types

谢谢工作正常,但使用的是
perl
。我将检查我是否可以在我的案例中使用perl。
sed -i .bak "\$i hello" mimes.types