简体   繁体   English

如何使用 sed 在文件的第一行插入文本?

[英]How do I insert text to the 1st line of a file using sed?

Hi I'm trying to add text to the 1st line of a file using sed so far iv'e tried嗨,到目前为止,我正在尝试使用 sed 将文本添加到文件的第一行

#!/bin/bash
touch test
sed -i -e '1i/etc/example/live/example.com/fullchain.pem;\' test

And this dosn't work also tried而且这行不通也试过了

#!/bin/bash
touch test
sed -i "1i ssl_certificate /etc/example/live/example.com/fullchain.pem;" test

this dosn't seem to work either oddly when I try当我尝试时,这似乎并不奇怪

#!/bin/bash
touch test
echo "ssl_certificate /etc/example/live/example.com/fullchain.pem;" > test

I get the 1st line of text to appear when i use cat test but as soon as i type sed -i "2i ssl_certificate_key /etc/example/live/example.com/privkey.pem;"当我使用cat test时,我会看到第一行文本,但是只要我输入sed -i "2i ssl_certificate_key /etc/example/live/example.com/privkey.pem;" I can't see the information that i sould do on line 2 this being ssl_certificate_key /etc/example/live/example.com/privkey.pem;我在第 2 行看不到我应该做的信息,这是 ssl_certificate_key /etc/example/live/example.com/privkey.pem;

so my question to summerise所以我要总结的问题

  1. Can text be inserted into the 1st line of a newly created file using sed?可以使用 sed 将文本插入到新创建文件的第一行吗?
  2. If yes whats the best way of inserting text after the 1st line of text?如果是,在第一行文本之后插入文本的最佳方式是什么?

Suppose you have a file like this:假设你有一个这样的file

one
two

Then to append to the first line:然后附加到第一行:

$ sed '1 s_$_/etc/example/live/example.com/fullchain.pem;_' file
one/etc/example/live/example.com/fullchain.pem;
two

To insert before the first line:在第一行之前插入:

$ sed '1 i /etc/example/live/example.com/fullchain.pem;' file
/etc/example/live/example.com/fullchain.pem;
one
two

Or, to append after the first line:或者,在第一行之后追加:

$ sed '1 a /etc/example/live/example.com/fullchain.pem;' file
one
/etc/example/live/example.com/fullchain.pem;
two

Note the number 1 in those sed expressions - that's called the address in sed terminology.请注意那些sed表达式中的数字1 - 这在sed术语中称为地址 It tells you on which line the command that follows is to operate.它告诉您后面的命令要在哪一进行操作。

If your file doesn't contain the line you're addressing, the sed command won't get executed.如果您的文件不包含您要寻址的行,则不会执行sed命令。 That's why you can't insert/append on line 1, if your file is empty.这就是为什么你不能在第 1 行插入/追加的原因,如果你的文件是空的。

Instead of using stream editor , to append (to empty files), just use a shell redirection >> :而不是使用流编辑器,追加(到空文件),只需使用外壳重定向>>

echo "content" >> file

Your command will work if you make sure the input file has at least one line:如果您确保输入文件至少有一行,您的命令将起作用:

[ "$(wc -l < test)" -gt 0 ] || printf '\n' >> test
sed -i -e '1 i/etc/example/live/example.com/fullchain.pem;\' test

Your problem stems from the fact that sed cannot locate the line you're telling it to write at, for example:您的问题源于sed无法找到您告诉它写入的行,例如:

touch test
sed -i -e '1i/etc/example/live/example.com/fullchain.pem;\' test

attempts to write to insert at the line 1 of test , but that line doesn't exist at this point.尝试在test的第 1 行写入插入,但此时该行不存在。 If you've created your file as:如果您已将文件创建为:

echo -en "\n" > test
sed -i '1i/etc/example/live/example.com/fullchain.pem;\' test

it would not complain, but you'd be having an extra line.它不会抱怨,但你会多出一条线。 Similarly, when you call:同样,当您调用:

sed -i "2i ssl_certificate_key /etc/example/live/example.com/privkey.pem;"

you're telling sed to insert the following data at the line 2 which doesn't exist at that point so sed doesn't get to edit the file.您告诉sed在第 2 行插入以下数据,该数据当时不存在,因此sed无法编辑文件。

So, for the initial line or the last line in the file, you should not use sed because simple > and >> stream redirects are more than enough.因此,对于文件中的首行或最后一行,您不应该使用sed因为简单的>>>流重定向已经绰绰有余。

First and Last第一和最后

I would assume that anyone who searched for how to insert/append text to the beginning/end of a file probably also needs to know how to do the other also.我假设任何搜索如何在文件的开头/结尾插入/附加文本的人可能也需要知道如何做另一个。

cal |                            \
  gsed -E                        \
       -e     '1i\{'             \
       -e     '1i\  "lines": ['  \
       -e 's/(.*)/    "\1",/'    \
       -e '$s/,$//'              \
       -e     '$a\  ]'           \
       -e     '$a\}'

Explanation说明

This is cal output piped to gnu- sed (called gsed on macOS installed via brew.sh ) with extended RegEx ( -E ) and 6 "scripts" applied ( -e ) and line breaks escaped with \\ for readability.这是cal输出管道输送到gnu- sed (称为gsed上经由安装MACOS brew.sh )与正则表达式扩展( -E )和6“脚本”应用于( -e )和换行与转义\\以提高可读性。 Scripts 1 & 2 use 1i\\ to "at line 1, insert".脚本 1 和 2 使用1i\\来“在第 1 行,插入”。 Scripts 5 & 6 use $a\\ to "at line <last>, append".脚本 5 和 6 使用$a\\来“在 <last> 行,追加”。 I vertically aligned the text outputs to make the code represent what is expected in the result.我垂直对齐文本输出以使代码代表结果中的预期内容。 Scripts 3 & 4 do substitutions (the latter applying only to "line <last>").脚本 3 和 4 进行替换(后者仅适用于“行 <last>”)。 The result is converting command output to valid JSON.结果是将命令输出转换为有效的 JSON。

output输出

{
  "lines": [
    "    October 2019      ",
    "Su Mo Tu We Th Fr Sa  ",
    "       1  2  3  4  5  ",
    " 6  7  8  9 10 11 12  ",
    "13 14 15 16 17 18 19  ",
    "20 21 22 23 24 25 26  ",
    "27 28 29 30 31        ",
    "                      "
  ]
}

For help getting this to work with the macos/BSD version of sed, see my answer here .如需帮助使其与 sed 的 macos/BSD 版本一起使用,请在此处查看我的回答

To insert text to the first line and put the rest on a new line using sed on macOS this worked for me在 macOS 上使用 sed 将文本插入第一行并将其余部分放在新行上,这对我有用

sed -i '' '1 i \
Insert
' ~/Downloads/File-path.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM