运行管道bash脚本

时间:2016-01-06 10:21:51

标签: bash shell awk grep sh

我需要执行我的bash脚本(output.sh)作为管道脚本。 见下文。

echo "Dec 10 03:39:13 cgnat2.dd.com 1 2015 Dec 9 14:39:11 01-g3-adsl - - NAT44 - [UserbasedW - 100.70.24.236 vrf-testnet - 222.222.34.65 - 3072 4095 - - ][UserbasedW - 100.70.25.9 vrf-testnet - 222.222.34.65 - 16384 17407 - - ][UserbasedW - 100.70.25.142 vrf-testnet - 222.222.34.69 - 9216 10239 - - ]" | ./output.sh

如何将文本回显到我的output.sh文件中,我需要使用[分割回显文本

输出应该是

[UserbasedW - 100.70.24.236 vrf-testnet - 222.222.34.65 - 3072 4095 - - ] [UserbasedW - 100.70.25.9 vrf-testnet - 222.222.34.65 - 16384 17407 - - ] [UserbasedW - 100.70.25.142 vrf-testnet - 222.222.34.69 - 9216 10239 - - ]

请帮帮我。我不知道.. :(

2 个答案:

答案 0 :(得分:1)

使用grep:

| grep -o '\[[^]]*\]'

或使用GNU grep:

| grep -oP '\[.*?\]'

输出:

[UserbasedW - 100.70.24.236 vrf-testnet - 222.222.34.65 - 3072 4095 - - ]
[UserbasedW - 100.70.25.9 vrf-testnet - 222.222.34.65 - 16384 17407 - - ]
[UserbasedW - 100.70.25.142 vrf-testnet - 222.222.34.69 - 9216 10239 - - ]

使用bash脚本(例如output.sh):

#!/bin/bash
grep -o '\[[^]]*\]'

用法:

echo "... your string ..." | ./output.sh

请参阅:The Stack Overflow Regular Expressions FAQ

答案 1 :(得分:0)

如果在第一个[之前删除标题,请在管道之前使用sed(假设您的echo是其他来源的样本)

echo "Dec 10 03:39:13 cgnat2.dd.com 1 2015 Dec 9 14:39:11 01-g3-adsl - - NAT44 - [UserbasedW - 100.70.24.236 vrf-testnet - 222.222.34.65 - 3072 4095 - - ][UserbasedW - 100.70.25.9 vrf-testnet - 222.222.34.65 - 16384 17407 - - ][UserbasedW - 100.70.25.142 vrf-testnet - 222.222.34.69 - 9216 10239 - - ]" \
 | sed 's/^[^[]*//' \
 | ./output.sh