string - bash: combine all elements of two arrays (and add something to each) -


i have 2 bash arrays , want combine elements of both, plus add string each resulting element.

specifically, have array containing years , months, , want date string of first day of each month in each year:

# define arrays containing years , months (zero-padded) yyyys=(2000 2001 2002) mms=(12 01 02)  # want achieve following using arrays defined above echo {2000..2002}{12,01,02}01 # 20001201 20000101 20000201 20011201 ...  # hard-coded months, following want echo ${yyyys[@]/%/0101} # 20000101 20010101 20020101  # how can achieve arbitrary months, using $mms? 

how can achieve little code possible?

note: need (dirty enough) bash run script, i'm not looking clean, portable solution condensed bash solution using string expansion, piping, or whatever else necessary. (i write function achieve in few lines of code without problem, that's not point).

i've found partial solution based on brace expansion:

eval echo $(echo "{${yyyys[@]}}{${mms[@]}}01" | sed 's/ /,/g') # 20001201 20000101 20000201 20011201 20010101 20010201 20021201 20020101 20020201 

however, works if both arrays contain more 1 element, otherwise braces not resolved.

given one-element arrays real use case, i've nevertheless written function:

# create timesteps combinations of years, days, months, hours # - $1 year(s), e.g., 2001, 2004-2006+2008 # - $2 month(s), e.g., 06, 12+01-02 # - $3 day(s), e.g., 01, 01-28 # - $4 hour(s), e.g., 00, 00+06+12+18 create_timesteps() {     local ys=($(expand_list_range ${1}))     local ms=($(expand_list_range ${2}))     local ds=($(expand_list_range ${3}))     local hs=($(expand_list_range ${4}))      local expr=""     [ ${#ys[@]} -eq 1 ] && expr+="${ys}" || expr+="{${ys[@]}}"     [ ${#ms[@]} -eq 1 ] && expr+="${ms}" || expr+="{${ms[@]}}"     [ ${#ds[@]} -eq 1 ] && expr+="${ds}" || expr+="{${ds[@]}}"     [ ${#hs[@]} -eq 1 ] && expr+="${hs}" || expr+="{${hs[@]}}"      expr="$(echo "${expr}" | sed 's/ /,/g')"     eval echo "${expr}" } 

i'll refrain accepting answer now, though, in hopes of different answer more along lines of original question.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -