Loop over array, preventing wildcard expansion (*)

Your problem is that you want an array, but you wrote a single string that contains the elements with spaces between them. Use an array instead. WHITELIST_DOMAINS=(‘*’ ‘*.foo.com’ ‘*.bar.com’) Always use double quotes around variable substitutions (i.e. “$foo”), otherwise the shell splits the the value of the variable into separate words and treats each word … Read more

Sum duplicate row values with awk

Use an Awk as below, awk ‘{ seen[$1] += $2 } END { for (i in seen) print i, seen[i] }’ file1 1486113768 9936 1486113769 6160736 1486113770 5122176 1486113772 4096832 1486113773 9229920 1486113774 8568888 {seen[$1]+=$2} creates a hash-map with the $1 being treated as the index value and the sum is incremented only for those … Read more

Do I need to quote command substitutions?

$(echo foo bar) is indeed a command substitution. In this specific example, you don’t need double quotes because a variable assignment creates a “double quote context” for its right-hand side, so VAR=$(…) is equivalent to VAR=”$(…)”. In bash, you don’t need double quotes in export VAR=$(…) or declare VAR=$(…). But you do need the double … Read more

Howto split a string on a multi-character delimiter in bash?

Since you’re expecting newlines, you can simply replace all instances of mm in your string with a newline. In pure native bash: in=’emmbbmmaaddsb’ sep=’mm’ printf ‘%s\n’ “${in//$sep/$’\n’}” If you wanted to do such a replacement on a longer input stream, you might be better off using awk, as bash’s built-in string manipulation doesn’t scale well … Read more