2

我想从格式(主机名,,)的网络组中提取主机名。我使用了以下 sed 命令。似乎它们都不起作用。谁能指出我正确的正则表达式构造?谢谢。

bash-3.2# ypcat -k netgroup|grep '^gridprod2 '|sed 's/(\([a-zA-Z0-9]*\),,)/\1\n/g'
gridprod2 (sbpsvrso766,,) (sbpsvrso767,,) (sbpsvrso768,,) (sbpsvrso769,,) (sbpsvrso461,,) (sbpsvrso462,,) (sbpsvrso463,,) (sbpsvrso464,,) (sbpsvrso465,,) (sbpsvrso466,,) (sbpsvrso467,,)  (sbpsvrso468,,) (sbtorsvr569,,) (sbpsvrwm841,,) (sbpsvrwm842,,) (sbpsvrwm843,,) (sbpsvrwm844,,) (sbpsvrwm845,,) (sdpsvrso804,,)

bash-3.2# ypcat -k netgroup|grep '^gridprod2 '|sed 's/(\([:alnum:]*\),,)/\1\n/g'
gridprod2 (sbpsvrso766,,) (sbpsvrso767,,) (sbpsvrso768,,) (sbpsvrso769,,) (sbpsvrso461,,) (sbpsvrso462,,) (sbpsvrso463,,) (sbpsvrso464,,) (sbpsvrso465,,) (sbpsvrso466,,) (sbpsvrso467,,)  (sbpsvrso468,,) (sbtorsvr569,,) (sbpsvrwm841,,) (sbpsvrwm842,,) (sbpsvrwm843,,) (sbpsvrwm844,,) (sbpsvrwm845,,) (sdpsvrso804,,)
4

2 回答 2

0

grep -oP改为使用lookbehind

echo "gridprod2 (sbpsvrso766,,) (sbpsvrso767,,) (sbpsvrso768,,) (sbpsvrso769,,) (sbpsvrso461,,) (sbpsvrso462,,) (sbpsvrso463,,) (sbpsvrso464,,) (sbpsvrso465,,) (sbpsvrso466,,) (sbpsvrso467,,)  (sbpsvrso468,,) (sbtorsvr569,,) (sbpsvrwm841,,) (sbpsvrwm842,,) (sbpsvrwm843,,) (sbpsvrwm844,,) (sbpsvrwm845,,) (sdpsvrso804,,)" \
| grep ^gridprod2 | grep -oP '(?<=\()[^,\)]*'

输出:

sbpsvrso766
sbpsvrso767
sbpsvrso768
sbpsvrso769
...

也许更准确的是

grep -oP '(?<=\().*?(?=,,\))'
于 2013-09-20T14:51:44.213 回答
0

一个awk版本:

awk -F, 'NR>1 {print $1}' RS="(" file
sbpsvrso766
sbpsvrso767
sbpsvrso768
sbpsvrso769
.....
于 2013-09-21T07:45:26.047 回答