matching text in quotes (newbie)

前端 未结 5 1210
礼貌的吻别
礼貌的吻别 2021-02-09 21:22

I\'m getting totally lost in shell programming, mainly because every site I use offers different tool to do pattern matching. So my question is what tool to use to do simple pat

5条回答
  •  爱一瞬间的悲伤
    2021-02-09 21:30

    1.

    zoul@naima:etc$ cat named.conf | grep zone
    zone "." IN {
    zone "localhost" IN {
        file "localhost.zone";
    zone "0.0.127.in-addr.arpa" IN {
    

    2.

    zoul@naima:etc$ cat named.conf | grep ^zone
    zone "." IN {
    zone "localhost" IN {
    zone "0.0.127.in-addr.arpa" IN {
    

    3.

    zoul@naima:etc$ cat named.conf | grep ^zone | sed 's/.*"\([^"]*\)".*/\1/'
    .
    localhost
    0.0.127.in-addr.arpa
    

    The regexp is .*"\([^"]*\)".*, which matches:

    1. any number of any characters: .*
      • a quote: "
      • starts to remember for later: \(
      • any characters except quote: [^"]*
      • ends group to remember: \)
      • closing quote: "
      • and any number of characters: .*

    When calling sed, the syntax is 's/what_to_match/what_to_replace_it_with/'. The single quotes are there to keep your regexp from being expanded by bash. When you “remember” something in the regexp using parens, you can recall it as \1, \2 etc. Fiddle with it for a while.

提交回复
热议问题