removing lines between two patterns (not inclusive) with sed

后端 未结 6 482
遇见更好的自我
遇见更好的自我 2020-12-09 20:08

Ok

I know that this is trivial question but: How can i remove lines from files that are between two known patterns/words:

pattern1
garba

6条回答
  •  猫巷女王i
    2020-12-09 21:00

    You may also use the Unix text editor ed:

    echo '
    pattern1
    garbage
    pattern2
    ' > test.txt
    
    cat <<-'EOF' | sed -e 's/^ *//' -e 's/ *$//' | ed -s test.txt &>/dev/null
      H
      /pattern1/+1,/pattern2/-1d
      wq
    EOF
    

    For more information see: Editing files with the ed text editor from scripts

提交回复
热议问题