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

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

i need to get these output text into my output.sh file ... how can i do this ??
thank you... final thing can you tell me how to get following part Dec 10 03:39:13 cgnat2.dd.com 1 2015 Dec 9 14:39:11 01-g3-adsl - - NAT44 -
echo "... your long string ..." | grep -o '^[^\[]*'
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

1 Comment

how to write output.sh shell file to get splitted strings ??? i need to get splitted text into my output.sh file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.