fork(1) download
  1. #!/usr/bin/env bash
  2. case $BASH_VERSION in ''|[1-3].*) echo "ERROR: Bash 4.0 or newer required" >&2; exit 1;; esac
  3. PS4=':$LINENO+'; set -x # enable trace logging w/ line numbers
  4.  
  5. start_hostgroup_re='^define[[:space:]]+hostgroup[[:space:]]*[{]'
  6. kv_re='^[[:space:]]*([^[:space:];]+)[[:space:]]+([^;]+)(;.*)?'
  7. end_re='^[[:space:]]*}'
  8.  
  9. declare -A keys=( ) comments=( )
  10.  
  11. build_new_members() {
  12. local hostgroup_name=$1 old_members=$2
  13. echo "New member list for $hostgroup_name"
  14. }
  15.  
  16. in_hostgroup=0
  17. while IFS= read -r line; do : "line=$line"
  18. if (( in_hostgroup )); then
  19. if [[ $line =~ $kv_re ]]; then
  20. keys[${BASH_REMATCH[1]}]=${BASH_REMATCH[2]}
  21. comments[${BASH_REMATCH[1]}]=${BASH_REMATCH[3]}
  22. elif [[ $line =~ $end_re ]]; then
  23. keys["members"]=$(build_new_members "${keys["hostgroup_name"]}" "${keys["members"]}")
  24. printf '%s\n' 'define hostgroup {'
  25. for key in "${!keys[@]}"; do : key="$key"
  26. value=${keys[$key]}
  27. comment=${comments[$key]}
  28. if [[ $comment ]]; then
  29. printf ' %-16s %s %s\n' "$key" "$value" "$comment"
  30. else
  31. printf ' %-16s %s\n' "$key" "$value"
  32. fi
  33. done
  34. printf '%s\n' '}'
  35. keys=( ); comments=( ); in_hostgroup=0
  36. elif [[ $line ]]; then # warn about non-empty non-assignment lines
  37. printf 'WARNING: Unrecognized line in hostgroup: %s\n' "$line" >&2
  38. fi
  39. else
  40. if [[ $line =~ $start_hostgroup_re ]]; then
  41. in_hostgroup=1
  42. else
  43. printf '%s\n' "$line"
  44. fi
  45. fi
  46. done
Success #stdin #stdout #stderr 0.01s 4384KB
stdin
define hostgroup {
  hostgroup_name  NA-servers ; The name of the hostgroup
  alias           NA region ; Long name of the group
  members         sample.com ; hosts belonging to this group

}

define hostgroup{
  hostgroup_name  FTP-server ; The name of the hostgroup
  alias           FTP NA region ; Long name of the group
  members         example.com
}
stdout
define hostgroup {
  members          New member list for NA-servers  ; hosts belonging to this group
  alias            NA region  ; Long name of the group
  hostgroup_name   NA-servers  ; The name of the hostgroup
}

define hostgroup {
  members          New member list for FTP-server 
  alias            FTP NA region  ; Long name of the group
  hostgroup_name   FTP-server  ; The name of the hostgroup
}
stderr
:5+start_hostgroup_re='^define[[:space:]]+hostgroup[[:space:]]*[{]'
:6+kv_re='^[[:space:]]*([^[:space:];]+)[[:space:]]+([^;]+)(;.*)?'
:7+end_re='^[[:space:]]*}'
:9+keys=()
:9+comments=()
:9+declare -A keys comments
:16+in_hostgroup=0
:17+IFS=
:17+read -r line
:17+: 'line=define hostgroup {'
:18+((  in_hostgroup  ))
:40+[[ define hostgroup { =~ ^define[[:space:]]+hostgroup[[:space:]]*[{] ]]
:41+in_hostgroup=1
:17+IFS=
:17+read -r line
:17+: 'line=  hostgroup_name  NA-servers ; The name of the hostgroup'
:18+((  in_hostgroup  ))
:19+[[   hostgroup_name  NA-servers ; The name of the hostgroup =~ ^[[:space:]]*([^[:space:];]+)[[:space:]]+([^;]+)(;.*)? ]]
:20+keys[${BASH_REMATCH[1]}]='NA-servers '
:21+comments[${BASH_REMATCH[1]}]='; The name of the hostgroup'
:17+IFS=
:17+read -r line
:17+: 'line=  alias           NA region ; Long name of the group'
:18+((  in_hostgroup  ))
:19+[[   alias           NA region ; Long name of the group =~ ^[[:space:]]*([^[:space:];]+)[[:space:]]+([^;]+)(;.*)? ]]
:20+keys[${BASH_REMATCH[1]}]='NA region '
:21+comments[${BASH_REMATCH[1]}]='; Long name of the group'
:17+IFS=
:17+read -r line
:17+: 'line=  members         sample.com ; hosts belonging to this group'
:18+((  in_hostgroup  ))
:19+[[   members         sample.com ; hosts belonging to this group =~ ^[[:space:]]*([^[:space:];]+)[[:space:]]+([^;]+)(;.*)? ]]
:20+keys[${BASH_REMATCH[1]}]='sample.com '
:21+comments[${BASH_REMATCH[1]}]='; hosts belonging to this group'
:17+IFS=
:17+read -r line
:17+: line=
:18+((  in_hostgroup  ))
:19+[[ '' =~ ^[[:space:]]*([^[:space:];]+)[[:space:]]+([^;]+)(;.*)? ]]
:22+[[ '' =~ ^[[:space:]]*} ]]
:36+[[ -n '' ]]
:17+IFS=
:17+read -r line
:17+: 'line=}'
:18+((  in_hostgroup  ))
:19+[[ } =~ ^[[:space:]]*([^[:space:];]+)[[:space:]]+([^;]+)(;.*)? ]]
:22+[[ } =~ ^[[:space:]]*} ]]
::23+build_new_members 'NA-servers ' 'sample.com '
::12+local 'hostgroup_name=NA-servers ' 'old_members=sample.com '
::13+echo 'New member list for NA-servers '
:23+keys["members"]='New member list for NA-servers '
:24+printf '%s\n' 'define hostgroup {'
:25+for key in "${!keys[@]}"
:25+: key=members
:26+value='New member list for NA-servers '
:27+comment='; hosts belonging to this group'
:28+[[ -n ; hosts belonging to this group ]]
:29+printf '  %-16s %s %s\n' members 'New member list for NA-servers ' '; hosts belonging to this group'
:25+for key in "${!keys[@]}"
:25+: key=alias
:26+value='NA region '
:27+comment='; Long name of the group'
:28+[[ -n ; Long name of the group ]]
:29+printf '  %-16s %s %s\n' alias 'NA region ' '; Long name of the group'
:25+for key in "${!keys[@]}"
:25+: key=hostgroup_name
:26+value='NA-servers '
:27+comment='; The name of the hostgroup'
:28+[[ -n ; The name of the hostgroup ]]
:29+printf '  %-16s %s %s\n' hostgroup_name 'NA-servers ' '; The name of the hostgroup'
:34+printf '%s\n' '}'
:35+keys=()
:35+comments=()
:35+in_hostgroup=0
:17+IFS=
:17+read -r line
:17+: line=
:18+((  in_hostgroup  ))
:40+[[ '' =~ ^define[[:space:]]+hostgroup[[:space:]]*[{] ]]
:43+printf '%s\n' ''
:17+IFS=
:17+read -r line
:17+: 'line=define hostgroup{'
:18+((  in_hostgroup  ))
:40+[[ define hostgroup{ =~ ^define[[:space:]]+hostgroup[[:space:]]*[{] ]]
:41+in_hostgroup=1
:17+IFS=
:17+read -r line
:17+: 'line=  hostgroup_name  FTP-server ; The name of the hostgroup'
:18+((  in_hostgroup  ))
:19+[[   hostgroup_name  FTP-server ; The name of the hostgroup =~ ^[[:space:]]*([^[:space:];]+)[[:space:]]+([^;]+)(;.*)? ]]
:20+keys[${BASH_REMATCH[1]}]='FTP-server '
:21+comments[${BASH_REMATCH[1]}]='; The name of the hostgroup'
:17+IFS=
:17+read -r line
:17+: 'line=  alias           FTP NA region ; Long name of the group'
:18+((  in_hostgroup  ))
:19+[[   alias           FTP NA region ; Long name of the group =~ ^[[:space:]]*([^[:space:];]+)[[:space:]]+([^;]+)(;.*)? ]]
:20+keys[${BASH_REMATCH[1]}]='FTP NA region '
:21+comments[${BASH_REMATCH[1]}]='; Long name of the group'
:17+IFS=
:17+read -r line
:17+: 'line=  members         example.com'
:18+((  in_hostgroup  ))
:19+[[   members         example.com =~ ^[[:space:]]*([^[:space:];]+)[[:space:]]+([^;]+)(;.*)? ]]
:20+keys[${BASH_REMATCH[1]}]=example.com
:21+comments[${BASH_REMATCH[1]}]=
:17+IFS=
:17+read -r line
:17+: 'line=}'
:18+((  in_hostgroup  ))
:19+[[ } =~ ^[[:space:]]*([^[:space:];]+)[[:space:]]+([^;]+)(;.*)? ]]
:22+[[ } =~ ^[[:space:]]*} ]]
::23+build_new_members 'FTP-server ' example.com
::12+local 'hostgroup_name=FTP-server ' old_members=example.com
::13+echo 'New member list for FTP-server '
:23+keys["members"]='New member list for FTP-server '
:24+printf '%s\n' 'define hostgroup {'
:25+for key in "${!keys[@]}"
:25+: key=members
:26+value='New member list for FTP-server '
:27+comment=
:28+[[ -n '' ]]
:31+printf '  %-16s %s\n' members 'New member list for FTP-server '
:25+for key in "${!keys[@]}"
:25+: key=alias
:26+value='FTP NA region '
:27+comment='; Long name of the group'
:28+[[ -n ; Long name of the group ]]
:29+printf '  %-16s %s %s\n' alias 'FTP NA region ' '; Long name of the group'
:25+for key in "${!keys[@]}"
:25+: key=hostgroup_name
:26+value='FTP-server '
:27+comment='; The name of the hostgroup'
:28+[[ -n ; The name of the hostgroup ]]
:29+printf '  %-16s %s %s\n' hostgroup_name 'FTP-server ' '; The name of the hostgroup'
:34+printf '%s\n' '}'
:35+keys=()
:35+comments=()
:35+in_hostgroup=0
:17+IFS=
:17+read -r line