【问题标题】:sed insert line command to add text with new line OSXsed 插入行命令以添加带有新行 OSX 的文本
【发布时间】:2020-06-11 11:40:56
【问题描述】:

我正在尝试使用 sed 将文本插入文件的第 100 行,我在其他论坛上找到的语法是:

sed -i '' "100i\ text to insert" filename

当我使用它时,我可以在特定行上添加文本,但它会影响已经存在的其他文本并放错位置。

我想在添加到文件的文本之前和之后添加一个新行。

我试过这个sed -i '' "100i\ text to insert other text to insert" filename,但它并没有像例外那样工作。

这是我运行上述命令时的输出。顺序应为<key>,低于该顺序应为<string> 标签。

<key>NSBluetoothAlwaysUsageDescription</key>    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This will allow "App" to find and connect to Bluetooth accessories. This app may also use bluetooth to know when you are nearby.</string>   <string>This app requires constant access to your location in order to track your position, even when the screen is off or the app is in the background.</string>

我似乎不知道应该在命令中添加什么来添加新行。

我不想打乱我只想在第 100 行插入文本的顺序,如果已经有文本,那么该文本应该换行。

我使用的是 OSX,这就是为什么我有一个空的 ' ' 作为我的扩展名。

谢谢!

【问题讨论】:

  • I am able to add text on a particular line but it affects other text which is already there and misplace it. 您能否在您的问题中添加输入和预期输出示例,然后让我们知道?
  • 只需按 ENTER 即可引入换行符
  • @RavinderSingh13 我更新了问题。你能看看吗
  • @WiktorStribiżew 你的建议没有用。

标签: bash macos sed insert


【解决方案1】:

如果你想添加一个换行符,你应该使用:$'\n'ANSI quoting

More SO info on sed newline


回答问题;使用:

sed -i '' -e "3s/^//p; 3s/^.*/text to insert/" /tmp/so.txt
  1. 3s/^//p; 重复第 3 行
  2. 3s/^.*/text to insert/" 将新的第 3 行替换为您的文本

编辑;

sed -i '' -e "3s/^//p; 3s/^.*/<key>NSBluetoothAlwaysUsageDescription<\/key>/" /tmp/so.txt

记得转义任何 / 字符!

【讨论】:

  • 我的命令是sed -i '' -e "23s/^//p; 3s/^.*/&lt;key&gt;NSBluetoothAlwaysUsageDescription&lt;/key&gt;/" filename NSBluetoothAlwaysUsageDescription 这是要插入的文本
  • 尝试使用简单的test 字符串。该错误可能是由未转义的&lt;&gt;/字符引起的
  • 我也这么认为...但无论如何我也想添加&lt;key&gt;标签:(
  • @RageshPikalmunde 我已经用新的 comamnd/pic 更新了我的答案
【解决方案2】:

如果需要,请尝试 ed(1)。

printf '%s\n' 3a 'text to insert' . w | ed -s file

这里不需要转义。

printf '%s\n' '100a' '<key>NSBluetoothAlwaysUsageDescription</key>    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>' '<string>This will allow "App" to find and connect to Bluetooth accessories. This app may also use bluetooth to know when you are nearby.</string>   <string>This app requires constant access to your location in order to track your position, even when the screen is off or the app is in the background.</string>' . w | ed -s file.txt

... 或使用 Heredoc

ed -s file.txt <<'EOE'
100a
<key>NSBluetoothAlwaysUsageDescription</key>    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This will allow "App" to find and connect to Bluetooth accessories. This app may also use bluetooth to know when you are nearby.</string>   <string>This app requires constant access to your location in order to track your position, even when the screen is off or the app is in the background.</string>
.
w
EOE

只需将file.txt 替换为您的文件名即可。

【讨论】:

    猜你喜欢
    • 2014-10-27
    • 1970-01-01
    • 2014-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多