【问题标题】:On AIX system, how to insert data to file at certain line在 AIX 系统上,如何在某行向文件中插入数据
【发布时间】:2019-07-19 18:10:03
【问题描述】:

在 AIX 测试系统上,我想在文件的某一行插入一些数据。我曾尝试使用 sed 命令 sed "5i some_data" somefile.txt 但失败了。有什么建议吗?

【问题讨论】:

  • “失败”时发生了什么?只是原文?错误信息?只是一些数据?你确定 somefile.txt 有超过五行吗?
  • @mpez0 error is "sed: 0602-404 Function 5i some data cannot be parsed."

标签: sed aix


【解决方案1】:

AIX sed 在requiring a newline with the i function 中是严格的:

cp somefile.txt somefile.txt.orig
sed '5i\
some_data' somefile.txt.orig > somefile.txt

标准指明的地方:

[1addr]i\
text
Write text to standard output.

【讨论】:

    【解决方案2】:

    sed 更适合编辑流;您可以对文件使用可编写脚本的文本编辑器。以ed 或ex 为例:

    ed somefile.txt <<EOE
    5i
    some data
    .
    wq
    EOE
    

    这更明确地分解了正在发生的事情。

    【讨论】:

    • 您可以使用 ed -s 使 ed 的某些输出静音
    • @BlackForest - 你的系统上没有“ed”吗?试试“前”。
    • ed 和 ex 都是 bos.rte.edit 包的一部分,我相信它是默认安装的。应该澄清“不起作用”以表明尝试了什么以及实际发生了什么。
    猜你喜欢
    • 2021-10-15
    • 2015-12-03
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 2012-08-14
    • 2014-04-06
    • 1970-01-01
    相关资源
    最近更新 更多