Dynamic indirect Bash array

后端 未结 3 743
挽巷
挽巷 2021-01-29 00:03

I have logs in this format:

log1,john,time,etc
log2,peter,time,etc
log3,jack,time,etc
log4,peter,time,etc

I want to create a list for every per

3条回答
  •  忘掉有多难
    2021-01-29 00:19

    Be warned: The below uses namevars, a new bash 4.3 feature.


    First: I would strongly suggest namespacing your arrays with a prefix to avoid collisions with unrelated variables. Thus, using content_ as that prefix:

    read_arrays() {
      while IFS= read -r line && IFS=, read -r -a fields <<<"$line"; do
        name=${fields[1]}
        declare -g -a "content_${fields[1]}"
        declare -n cur_array="content_${fields[1]}"
        cur_array+=( "$line" )
        unset -n cur_array
      done
    }
    

    Then:

    lines_for() {
      declare -n cur_array="content_$1"
      printf '%s\n' "${#cur_array[@]}" ## emit length of array for given person
    }
    

    ...or...

    for_each_line() {
      declare -n cur_array="content_$1"; shift
      for line in "${cur_array[@]}"; do
        "$@" "$line"
      done
    }
    

    Tying all this together:

    $ read_arrays <<'EOF'
    log1,john,time,etc
    log2,peter,time,etc
    log3,jack,time,etc
    log4,peter,time,etc
    EOF
    $ lines_for peter
    2
    $ for_each_line peter echo
    log2,peter,time,etc
    log4,peter,time,etc
    

    ...and, if you really want the format you asked for, with the number of columns as explicit data, and variable names that aren't safely namespaced, it's easy to convert from one to the other:

    # this should probably be run in a subshell to avoid namespace pollution
    # thus, (generate_stupid_format) >output
    generate_stupid_format() {
      for scoped_varname in "${!content_@}"; do
        unscoped_varname="${scoped_varname#content_}"
        declare -n unscoped_var=$unscoped_varname
        declare -n scoped_var=$scoped_varname
        unscoped_var=( "${#scoped_var[@]}" "${scoped_var[@]}" )
        declare -p "$unscoped_varname"
      done
    }
    

提交回复
热议问题