【问题标题】:how to change a specific line in the linux command如何更改linux命令中的特定行
【发布时间】:2021-06-17 13:18:33
【问题描述】:

如何更改特定行的文本

假设我要更改第 50 行,并且要更改 "user" 后面的 8 个字母: 像下面我加粗的例子

第 50 行“用户”:“9077c266-8944-11eb-a9d1-fa163e4be1a2”

【问题讨论】:

    标签: shell command-line command-line-arguments


    【解决方案1】:

    使用sed中的s命令进行搜索和替换。

    sed -i 'Ns/9077c266/replacement-line/' file.txt
    

    N 应替换为您的目标行号。

    要将更改的文本保存在不同的文件中,请删除 -i 选项:

    sed  'Ns/9077c266/replacement-line/' file.txt > new_file.txt
    

    【讨论】:

      【解决方案2】:

      如果它是唯一的,我会按行号或仅按内容使用 sed:

      sed -i '50s/9077c266/replacement/' file.txt
      

      或:

      sed -i 's/9077c266/replacement/' file.txt
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-13
        • 1970-01-01
        • 2018-05-10
        • 1970-01-01
        • 2015-05-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多