【问题标题】:How to insert text assigned to variable into a file before a string如何在字符串之前将分配给变量的文本插入文件中
【发布时间】:2015-07-16 18:10:22
【问题描述】:

我有: 一个名为“fooBar”的文件。 一个名为“myVar”的变量,它分配了字符串“the lazy”。 我想在文本 '</dog>' 之前将 $myVar 插入到文件 fooBar 中

fooBar 之前:

The quick brown fox jumps over </dog>

fooBarAfter:

The quick brown fox jumps over the lazy </dog>

请问最好的方法是什么?

【问题讨论】:

  • 您是说您希望它在遇到&lt; 时插入the lazy 或在哪里?我只是想确保问题清楚,以便正确回答
  • bash 字符串插入参考此链接:

标签: linux bash unix


【解决方案1】:

你可以像这样使用sed

cat fooBar
The quick brown fox jumps over </dog>

myVar='the lazy'
sed -i.bak "s~</dog>~$myVar &~" fooBar

cat fooBar
The quick brown fox jumps over the lazy </dog>

【讨论】:

    猜你喜欢
    • 2013-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 2016-03-07
    • 1970-01-01
    • 2015-10-26
    • 2015-05-04
    相关资源
    最近更新 更多