【问题标题】:How to insert a string in to a string where i find a substring , using SED如何使用 SED 将字符串插入到我找到子字符串的字符串中
【发布时间】:2016-10-16 21:48:17
【问题描述】:
$ cat f1
My name is Bruce and my surname is

我想使用 SED 查找“Bruce”,然后将“Lee”附加到找到“Bruce”的行的末尾

然后是一个更棘手的......。 我想插入……一个字符串……到我发现某些东西的行中。 示例

$ cat f2
My name is Yacov and I am from Israel.
I used to be a programmer but then I found love for a woman who told me to become a teacher.

我想使用 SED 在我找到字符串“programmer”的地方插入字符串“excellent”。 我想在子字符串“programmer”的开头插入这个字符串

【问题讨论】:

  • 我们不是来当你的导师,也不是为你写代码。您展示您所写的内容,我们(也许)会尝试帮助修复它。
  • 手册页不好学,但您查看过this tutorial 吗?也推荐:this book.
  • Benjamin W,我看过那一页以及关于 AWK 的一页,哇,它真的很强大,但你需要知道如何理解他们在说什么。我不打算遵循他们展示的所有使用示例。例如,他们向您展示了 append for 的用法,但他们编写它时就像您在 bash 文件中使用它一样。我已经成功地实现了这些例子,所以我显然没有做得那么差。不过,我只是很难在命令提示符的一行中执行其中的一些操作。
  • Benjamin W,我也遵循使用 SED 的附加示例,但它会将字符串附加到您正在编辑的文件中的以下行。所以在我的情况下,我正在找到我想要的字符串,然后在该行的末尾我想附加一个字符串。它将字符串附加到新行。我认为使用“a”选项是绝对正确的。但是,我想知道如何在找到要搜索的字符串的同一行上追加内容。
  • Benjamin W,我的下一个挑战是在我在文本文件中找到的字符串的开头或结尾插入一个字符串(我已经找到并尝试了 I 选项,但它正在替换和写完我不想要的其余行)。

标签: string sed insert append substring


【解决方案1】:

你试过了吗?

$ cat f1
My name is Bruce and my surname is
$ sed 's/^.*Bruce.*$/& Lee/' f1
My name is Bruce and my surname is Lee
$ cat f2
My name is Yacov and I am from Israel.
I used to be a programmer but then I found love for a woman who told me
to become a teacher.
$ sed 's/programmer/excellent programmer/' f2
My name is Yacov and I am from Israel.
I used to be a excellent programmer but then I found love for a woman 
who told me to become a teacher.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-25
    相关资源
    最近更新 更多