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

前端 未结 5 1956
无人共我
无人共我 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:13

    It's not necessary to use tr, but it is necessary that IFS is a non-whitespace character (otherwise multiples get collapsed to singles as you've seen).

    $ IFS=, read -r one two three <<<'one,,three'
    $ printf '<%s> ' "$one" "$two" "$three"; printf '\n'
     <> 
    
    $ var=$'one\t\tthree'
    $ var=${var//$'\t'/,}
    $ IFS=, read -r one two three <<< "$var"
    $ printf '<%s> ' "$one" "$two" "$three"; printf '\n'
     <> 
    
    $ idel=$'\t' odel=','
    $ var=$'one\t\tthree'
    $ var=${var//$idel/$odel}
    $ IFS=$odel read -r one two three <<< "$var"
    $ printf '<%s> ' "$one" "$two" "$three"; printf '\n'
     <> 
    

提交回复
热议问题