inserting text into a specific line

前端 未结 3 1523
北海茫月
北海茫月 2021-02-06 00:00

I\'ve got a text file, and using Bash I wish to insert text into into a specific line.

Text to be inserted for example is !comment: http://www.test.com into

相关标签:
3条回答
  • 2021-02-06 00:45

    Using man 1 ed (which reads entire file into memory and performs in-place file editing without previous backup):

    # cf. http://wiki.bash-hackers.org/doku.php?id=howto:edit-ed
    line='!comment: http://www.test.com'
    #printf '%s\n' H '/!eeee/i' "$line" . wq | ed -s file
    printf '%s\n' H 5i "$line" . wq | ed -s file
    
    0 讨论(0)
  • 2021-02-06 00:49
    sed '4a\
    !comment: http://www.test.com' file.txt > result.txt
    

    i inserts before the current line, a appends after the line.

    0 讨论(0)
  • 2021-02-06 00:52

    you can use awk as well

    $ awk 'NR==5{$0="!comment: http://www.test.com\n"$0}1' file
    !aaaa
    !bbbb
    !cccc
    !ffffdd
    !comment: http://www.test.com
    !eeee
    !ffff
    
    0 讨论(0)
提交回复
热议问题