SED delete lines between two pattern matches

后端 未结 5 1664
花落未央
花落未央 2020-12-12 02:12

I am trying to replace lines between two pattern/string matches using SED see below. I need to delete lines below interface GigabitEthernet0/3 up to inter

5条回答
  •  伪装坚强ぢ
    2020-12-12 03:00

    The basic answer seems simple to me:

    sed -e "/interface GigabitEthernet0\/3/p" \
        -e "/interface GigabitEthernet0\/4/p" \
        -e "/interface GigabitEthernet0\/3/,/interface GigabitEthernet0\/4/d" "$@"
    

    Print lines matching 'interface GigabitEthernet0/3', and lines containing 'interface GigabitEthernet0/4'; delete the lines between and including the two.

    The only possible issue is that if there is 'interface GigabitEthernet0/4' and no preceding 'interface GigabitEthernet0/3', then that line will be doubled up. If that's an actual problem, then go for a more complex script:

    sed -e "/interface GigabitEthernet0\/3/,/interface GigabitEthernet0\/4/{
            /interface GigabitEthernet0\/3/p
            /interface GigabitEthernet0\/4/p
            d
            }" "$@"
    

    If you're adamant it must be one line, use semicolons, but the result is illegible.

提交回复
热议问题