Insert a line at specific line number with sed or awk

后端 未结 9 1453
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 17:15

I have a script file which I need to modify with another script to insert a text at the 8th line.

String to insert: Project_Name=sowstest, into a file c

相关标签:
9条回答
  • 2020-11-30 17:52
    sed -i '8i8 This is Line 8' FILE
    

    inserts at line 8

    8 This is Line 8
    

    into file FILE

    -i does the modification directly to file FILE, no output to stdout, as mentioned in the comments by glenn jackman.

    0 讨论(0)
  • 2020-11-30 17:55

    OS X / macOS / FreeBSD sed

    The -i flag works differently on macOS sed than in GNU sed.

    Here's the way to use it on macOS / OS X:

    sed -i '' '8i\
    8 This is Line 8' FILE
    

    See man 1 sed for more info.

    0 讨论(0)
  • 2020-11-30 17:59

    the awk answer

    awk -v n=8 -v s="Project_Name=sowstest" 'NR == n {print s} {print}' file > file.new
    
    0 讨论(0)
提交回复
热议问题