use*_*919 8 linux bash shell scripting sed
嗨,到目前为止,我正在尝试使用 sed 将文本添加到文件的第一行
#!/bin/bash
touch test
sed -i -e '1i/etc/example/live/example.com/fullchain.pem;\' test
Run Code Online (Sandbox Code Playgroud)
而且这行不通也试过了
#!/bin/bash
touch test
sed -i "1i ssl_certificate /etc/example/live/example.com/fullchain.pem;" test
Run Code Online (Sandbox Code Playgroud)
当我尝试时,这似乎并不奇怪
#!/bin/bash
touch test
echo "ssl_certificate /etc/example/live/example.com/fullchain.pem;" > test
Run Code Online (Sandbox Code Playgroud)
我在使用时显示第一行文本,cat test
但是一旦我输入,sed -i "2i ssl_certificate_key /etc/example/live/example.com/privkey.pem;"
我就看不到我应该在第 2 行执行的信息,这是 ssl_certificate_key /etc/example/live/example.com/privkey.pem;
所以我要总结的问题
ran*_*mir 10
假设你有一个file这样的:
one
two
Run Code Online (Sandbox Code Playgroud)
然后附加到第一行:
$ sed '1 s_$_/etc/example/live/example.com/fullchain.pem;_' file
one/etc/example/live/example.com/fullchain.pem;
two
Run Code Online (Sandbox Code Playgroud)
在第一行之前插入:
$ sed '1 i /etc/example/live/example.com/fullchain.pem;' file
/etc/example/live/example.com/fullchain.pem;
one
two
Run Code Online (Sandbox Code Playgroud)
或者,在第一行之后追加:
$ sed '1 a /etc/example/live/example.com/fullchain.pem;' file
one
/etc/example/live/example.com/fullchain.pem;
two
Run Code Online (Sandbox Code Playgroud)
请注意1这些sed表达式中的数字-在术语中称为地址sed。它告诉您后面的命令要在哪一行 进行操作。
如果您的文件不包含您要寻址的行,sed则不会执行该命令。这就是为什么你不能在第 1 行插入/追加的原因,如果你的文件是空的。
而不是使用流编辑器,追加(到空文件),只需使用外壳重定向>>:
echo "content" >> file
Run Code Online (Sandbox Code Playgroud)