read in bash on whitespace-delimited file without empty fields collapsing

前端 未结 5 1959
无人共我
无人共我 2020-11-28 14:52

I\'m trying to read a multi-line tab-separated file in bash. The format is such that empty fields are expected. Unfortunately, the shell is collapsing together field separat

5条回答
  •  借酒劲吻你
    2020-11-28 15:12

    I've written a function which works around this issue. This particular implementation is particular about tab-separated columns and newline-separated rows, but that limitation could be removed as a straightforward exercise:

    read_tdf_line() {
        local default_ifs=$' \t\n'
        local n line element at_end old_ifs
        old_ifs="${IFS:-${default_ifs}}"
        IFS=$'\n'
    
        if ! read -r line ; then
            return 1
        fi
        at_end=0
        while read -r element; do
            if (( $# > 1 )); then
                printf -v "$1" '%s' "$element"
                shift
            else
                if (( at_end )) ; then
                    # replicate read behavior of assigning all excess content
                    # to the last variable given on the command line
                    printf -v "$1" '%s\t%s' "${!1}" "$element"
                else
                    printf -v "$1" '%s' "$element"
                    at_end=1
                fi
            fi
        done < <(tr '\t' '\n' <<<"$line")
    
        # if other arguments exist on the end of the line after all
        # input has been eaten, they need to be blanked
        if ! (( at_end )) ; then
            while (( $# )) ; do
                printf -v "$1" '%s' ''
                shift
            done
        fi
    
        # reset IFS to its original value (or the default, if it was
        # formerly unset)
        IFS="$old_ifs"
    }
    

    Usage as follows:

    # read_tdf_line one two three rest <<<$'one\t\tthree\tfour\tfive'
    # printf '<%s> ' "$one" "$two" "$three" "$rest"; printf '\n'
     <>  
    

提交回复
热议问题