【问题标题】:Insert lines in a file starting from a specific line从特定行开始在文件中插入行
【发布时间】:2012-10-30 06:23:57
【问题描述】:

我想在 bash 中的文件中从特定行开始插入行。

每一行都是一个字符串,是数组的一个元素

line[0]="foo"
line[1]="bar"
...

具体的行是'fields'

file="$(cat $myfile)"
for p in $file; do
    if [ "$p" = 'fields' ]
        then insertlines()     #<- here
    fi
done

【问题讨论】:

    标签: bash sed


    【解决方案1】:

    sed 是你的朋友:

    :~$ cat text.txt 
    foo
    bar
    baz
    ~$ 
    
    ~$ sed '/^bar/\na this is the new line/' text.txt > new_text.txt
    ~$ cat new_text.txt 
    foo
    bar
    this is the new line
    baz
    ~$ 
    

    【讨论】:

    • 那行不通;您需要在a 之后的 sed 命令字符串中使用反斜杠和换行符,而不是空格。
    【解决方案2】:

    这可以通过 sed 完成:sed 's/fields/fields\nNew Inserted Line/'

    $ cat file.txt 
    line 1
    line 2 
    fields
    line 3
    another line 
    fields
    dkhs
    
    $ sed 's/fields/fields\nNew Inserted Line/' file.txt 
    line 1
    line 2 
    fields
    New Inserted Line
    line 3
    another line 
    fields
    New Inserted Line
    dkhs
    

    使用-i就地保存而不是打印到stdout

    sed -i 's/fields/fields\nNew Inserted Line/'

    作为 bash 脚本:

    #!/bin/bash
    
    match='fields'
    insert='New Inserted Line'
    file='file.txt'
    
    sed -i "s/$match/$match\n$insert/" $file
    

    【讨论】:

    • 当使用sed -i 时,这是否会重写整个文件,但使用sed 的替换,或者这是否类似于在文本编辑器中打开文件并手动插入字符串?我问是因为我需要用一个大文件来做这件事,而且不得不一遍又一遍地写这么多,这将花费很长时间,并且会更快地耗尽我的 SSD 和 HDD。
    • 如果您打开一个文件,手动插入字符串并保存它,您希望文本编辑器在物理上做什么?
    • sed -i 不再工作。在 Solaris11.4 上,它抛出“非法选项 -i”
    【解决方案3】:

    这绝对是您想要使用类似sed(或awkperl)而不是在shell 循环中一次读取一行的情况。这不是 shell 做得好的或有效的事情。

    您可能会发现编写可重用函数很方便。这是一个简单的,虽然它不适用于完全任意的文本(斜线或正则表达式元字符会混淆事物):

    function insertAfter # file line newText
    {
       local file="$1" line="$2" newText="$3"
       sed -i -e "/^$line$/a"$'\\\n'"$newText"$'\n' "$file"
    }
    

    例子:

    $ cat foo.txt
    Now is the time for all good men to come to the aid of their party.
    The quick brown fox jumps over a lazy dog.
    $ insertAfter foo.txt \
       "Now is the time for all good men to come to the aid of their party." \
       "The previous line is missing 'bjkquvxz.'"
    $ cat foo.txt
    Now is the time for all good men to come to the aid of their party.
    The previous line is missing 'bjkquvxz.'
    The quick brown fox jumps over a lazy dog.
    $ 
    

    【讨论】:

      【解决方案4】:

      或者使用sed 的另一个例子:

      准备一个test.txt 文件:

      echo -e "line 1\nline 2\nline 3\nline 4" > /tmp/test.txt
      
      cat /tmp/test.txt
      line 1
      line 2
      line 3
      line 4
      

      test.txt 文件中添加新行:

      sed -i '2 a line 2.5' /tmp/test.txt
      # sed for in-place editing (-i) of the file: 'LINE_NUMBER a-ppend TEXT_TO_ADD'
      
      cat /tmp/test.txt
      line 1
      line 2
      line 2.5
      line 3
      line 4
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-04-11
        • 2020-11-05
        • 1970-01-01
        • 2019-05-31
        • 1970-01-01
        • 2013-06-02
        相关资源
        最近更新 更多