How to insert a line using sed before a pattern and after a line number?

后端 未结 4 1381
我在风中等你
我在风中等你 2020-12-09 15:36

How to insert a line into a file using sed before a pattern and after a line number? And how to use the same in shell script?

This inserts a line before

4条回答
  •  忘掉有多难
    2020-12-09 16:30

    I assume you want to insert the line before a pattern only if the current line number is greater than some value (i.e. if the pattern occurs before the line number, do nothing)

    If you're not tied to sed:

    awk -v lineno=$line -v patt="$pattern" -v text="$line_to_insert" '
        NR > lineno && $0 ~ patt {print text}
        {print}
    ' input > output
    

提交回复
热议问题