I have some output that I need to parse into an array that looks like the following. The number of entries can change.
interface : eth1
ip address : 1.1.1.1 [Active]
subnet mask: 255.255.255.0
router : 1.1.1.2
name server: 1.1.1.3
dhcp server: 1.1.1.4
lease time : 86400
last update: Fri Jul 5 00:11:12 UTC 2013
expiry : Sat Jul 06 00:11:08 UTC 2013
reason : BOUND
interface : eth2
ip address : 2.2.2.2 [Active]
subnet mask: 255.255.255.0
router : 2.2.2.3
name server: 2.2.2.4
dhcp server: 2.2.2.5
lease time : 86400
last update: Fri Jul 5 03:03:41 UTC 2013
expiry : Sat Jul 06 03:03:39 UTC 2013
reason : REBOOT
Each section begins with interface and ends with reason and the blank line after reason.
I'm pretty new to bash scripting and have tried just about everything I can think of to get each section into a variable and I just can't seem to get it to work. If this was any other language... I could do this in a heartbeat!
Basically what I want is an array that will have each section with all of the details in between (these details can also change and not have as many lines).
I've tried a number of different methods with awk, sed, grep, etc... None of them seem to get me where I want to be.
What it should ultimately look like:
$output_array[$1]=
interface : eth1
ip address : 1.1.1.1 [Active]
subnet mask: 255.255.255.0
router : 1.1.1.2
name server: 1.1.1.3
dhcp server: 1.1.1.4
lease time : 86400
last update: Fri Jul 5 00:11:12 UTC 2013
expiry : Sat Jul 06 00:11:08 UTC 2013
reason : BOUND
$output_array[$2]=
interface : eth1
ip address : 1.1.1.1 [Active]
subnet mask: 255.255.255.0
router : 1.1.1.2
name server: 1.1.1.3
dhcp server: 1.1.1.4
lease time : 86400
last update: Fri Jul 5 00:11:12 UTC 2013
expiry : Sat Jul 06 00:11:08 UTC 2013
reason : BOUND
Can anyone point me in the right direction? Thanks!
One example of something I've tried, info was not split, or I did something wrong!
output_array=echo $output | awk -v x="^$" -v n=1 '$0 ~ x {n++; next}{print}'
for items in $output_array; do
echo "ENTRY: $items"
done
have tried just about everything I can think of-- could you post what you attempted.If this was any other language... I could do this in a heartbeat!-- Very nice.while readloop to read it line by line, appending to some variable. When you find a blank line, add the variable to your array.