Reputation: 83
I need to execute my bash script (output.sh) as piped script. see below.
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
how can i get echoing text in to my output.sh file and I need to split echoing text using [
output should be
[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 - - ]
please help me. i have no idea.. :(
Upvotes: 1
Views: 50
Reputation: 88776
With grep:
| grep -o '\[[^]]*\]'
or with GNU grep:
| grep -oP '\[.*?\]'
Output:
[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 - - ]
With a bash script (e.g. output.sh):
#!/bin/bash
grep -o '\[[^]]*\]'
Usage:
echo "... your string ..." | ./output.sh
See: The Stack Overflow Regular Expressions FAQ
Upvotes: 1
Reputation: 10039
If it is removing header before first [
, use a sed between before your pipe (assuming your echo is a sample of other source)
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
Upvotes: 0