matching text in quotes (newbie)

前端 未结 5 1207
礼貌的吻别
礼貌的吻别 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:41

    As long as someone is pointing out sed/awk, I'm going to point out that grep is redundant.

    sed -ne '/^zone/{s/.*"\([^"]*\)".*/\1/;p}' /etc/bind/named.conf
    

    This gives you what you're looking for without the quotes (move the quotes inside the parenthesis to keep them). In awk, it's even simpler with the quotes:

    awk '/^zone/{print $2}' /etc/bind/named.conf
    

    I try to avoid pipelines as much as possible (but not more). Remember, Don't pipe cat. It's not needed. And, insomuch as awk and sed duplicating grep's work, don't pipe grep, either. At least, not into sed or awk.

    Personally, I'd probably have used perl. But that's because I probably would have done the rest of whatever you're doing in perl, making it a minor detail (and being able to slurp the whole file in and regex against everything simultaneously, ignoring \n's would be a bonus for cases where I don't control /etc/bind, such as on a shared webhost). But, if I were to do it in shell, one of the above two would be the way I'd approach it.

提交回复
热议问题