Comparison of three different solutions
Less using sed :
sed -e ':a;N;/\n#MyUniqueString/{s/\n/ domain4.com\n/};H;s/\n.*$//;p;g;s/^.*\n//;ta;' config.file
This can be run as:
sed -e ' :a; N; /\n#MyUniqueString/{ s/\n/ domain4.com\n/ }; H; s/\n.*$//; p; g; s/^.*\n//; ta; ' config.file
profiling:
time sed ':a;N;/\n#MyUniqueString/{s/\n/ domain4.com\n/};H; s/\n.*$//;p;g;s/^.*\n//;ta;' config.file define hostgroup{ hostgroup_name http-urls ; The name of the hostgroup alias HTTP URLs ; Long name of the group members domain1.com, domain2.com, domain3.com, domain4.com
Why not clean bash ?
Since there is no fork , this can be very fast (if from bash and for not too long configuration files):
readarray configFile < ./config.file for ((i=${#configFile};i--;));do [[ "${configFile[i]}" =~ "#MyUniqueString" ]] && break done configFile[i-1]+=" domain4.com" printf "%s\n" "${configFile[@]//$'\n'/}"
Profiling is performed:
time { readarray configFile < ./config.file for ((i=${#configFile};i--;));do [[ "${configFile[i]}" =~ "#MyUniqueString" ]] && break done configFile[i-1]+=" domain4.com" printf "%s\n" "${configFile[@]//$'\n'/}" }
It gives:
define hostgroup{ hostgroup_name http-urls ; The name of the hostgroup alias HTTP URLs ; Long name of the group members domain1.com, domain2.com, domain3.com, domain4.com
Test with awk
time awk '{a[NR]=$0}/
source share