使用Bash在特定行之前插入多行文本 [英] Insert multiple lines of text before specific line using Bash

查看:119
本文介绍了使用Bash在特定行之前插入多行文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在特定行之前插入几行文本,但是在尝试添加新行字符时会不断出现sed错误.我的命令如下:

I am trying to insert a few lines of text before a specific line, but keep getting sed errors when I try to add a new line character. My command looks like:

sed -r -i '/Line to insert after/ i Line one to insert \\
    second new line to insert \\
    third new line to insert' /etc/directory/somefile.txt

所报告的错误是:

sed: -e expression #1, char 77: unterminated `s' command

我尝试使用\n\\(如示例中所示),根本没有字符,只是将第二行移到了下一行.我也尝试过类似的事情:

I've tried, using \n, \\ (as in the example), no character at all, just moving the second line to the next line. I've also tried something like:

sed -r -i -e '/Line to insert after/ i Line one to insert'
    -e 'second new line to insert'
    -e 'third new line to insert' /etc/directory/somefile.txt

编辑!:抱歉,我希望在现有内容之前插入文本,而不是在之后!

EDIT!: Apologies, I wanted the text inserted BEFORE the existing, not after!

推荐答案

这应该有效:

sed -i '/Line to insert after/ i Line one to insert \
second new line to insert \
third new line to insert' file

这篇关于使用Bash在特定行之前插入多行文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆