I am working on one shell script which search directory modified in last 24 hours and then compress them using tar.Folder Search :find /path to log directory/ -maxdepth 1 -type d -mtime +0 example output:/path to log directory/01_05_2016/path to log directory/02_05_2016/path to log directory/03_05_2016/path to log directory/04_05_2016Truncate unnecessary character:find /path to log directory/ -maxdepth 1 -type d -mtime +0 | tr -d _ | tr -d path to log directoryexample output:01052016 02052016 03052016 04052016Separate the truncated strings as individual array element using sed:# Folder searchdeclare -a array=($(find <0-24 hour directory serach> | <truncate uncecessary character> | sed 's/\([0-9]*\).*/[\1]=\1/'))# Array Printingecho Number of Array Elements: ${#array[@]}echo array[01052016]:  ${array[01052016]}echo array[02052016]:  ${array[02052016]}echo array[03052016]:  ${array[03052016]}echo array[04052016]:  ${array[04052016]}example outputNumber of Array Elements: 4array[01052016]: 01052016array[02052016]: 02052016array[03052016]: 03052016array[04052016]: 04052016Compress the output:LOG_DIR_NAME='<find folder>'xargs tar -cvzPf /ouput folder/logname.tar.gz  /path to log directory $LOG_DIR_NAMEProblem Statment:By the use of sed command, I can separate the search string into an individual array element. With the use of this command sed 's/\([0-9]*\).*/[\1]=\1/'where [\1] defines my array index and \1 defines my element. Desired OutputI am interested in getting the array element stored from index (0 to n)array[0]: 01052016array[1]: 02052016array[2]: 03052016array[3]: 04052016How can I customize the array indexing in this case or traverse thru the array without knowing the start index?Please helpThank you
Array indexing using sed
shell script;sed;array;associative array
First of all, you should simplify your command to:$ find /path to log directory/ -maxdepth 1 -type d -mtime +0 | sed 's#.*/##; s#_##g' 01052016030520160405201602052016Next, to save as an array, you could just do:array=( $(find /path to log directory/ -maxdepth 1 -type d -mtime +0 | sed 's#.*/##; s#_##g') )That saves it as you want, in an array with numerical, increasing indices starting from 0:$ for i in {0..3}; do echo $i : ${array[$i]}; done0 : 010520161 : 030520162 : 040520163 : 02052016That said, if you do find yourself needing to iterate over an associative array, you can do that too:$ declare -a array=( foo=>bar baz=>bad )$ for i in ${!array[*]}; do echo $i : ${array[$i]}; donebaz : badfoo : bar