Thursday, March 31, 2016

Bash Dynamic Loops -- Read file daemon

#!/bin/bash 

COND1="break"
COND2="sleep 1"
 ## Dynamically change the loop logic from continue reading, to read 
 ## all data and break. 
[[ -z "${1}" ]] && COND="${COND1}" || COND="${COND2}" 
while IFS= read -d $'\n' -r a || ${COND}; do 
 [[ -n "$a" ]] && echo "curl -s http://foo.bar/some.php?id=${a}"
done < id_list.txt

exit 0 

Monday, March 28, 2016

Replacing Nth Occurrence from End of Line via Sed

SEARCH="one" 
SEARCHNEG=$(sed 's/./[^&]*/g' <<< "${SEARCH}")
OCCURRENCE=3 
REPLACE="FOUR" 
SED_DELIM=$'\001'  
sed -r "s${SED_DELIM}${SEARCH}((${SEARCHNEG}${SEARCH}){$((${OCCURRENCE}-1))}${SEARCHNEG})\$${SED_DELIM}${REPLACE}\1${SED_DELIM}"  <<< "one one two two one one three three one one"