1

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.. :(

Cyrus
  • 69,405
  • 13
  • 65
  • 117
etution lanka
  • 73
  • 1
  • 11

2 Answers2

1

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

Community
  • 1
  • 1
Cyrus
  • 69,405
  • 13
  • 65
  • 117
0

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
NeronLeVelu
  • 9,372
  • 1
  • 21
  • 41