Читать в bash файл с разделителями табуляции без свертывания пустых полей

Я пытаюсь прочитать многострочный файл, разделенный табуляцией, в bash. Формат таков, что ожидаются пустые поля. К сожалению, оболочка объединяет разделители полей, которые расположены рядом друг с другом, как показано ниже:

# IFS=$'\t'
# read one two three <<<$'one\t\tthree'
# printf '<%s> ' "$one" "$two" "$three"; printf '\n'
<one> <three> <>

... в отличие от желаемого результата <one> <> <three>,

Можно ли решить эту проблему, не прибегая к отдельному языку (например, к awk)?

7 ответов

Решение

Вот подход с некоторыми тонкостями:

  • входные данные, где бы они ни стали псевдо-2D массивом в основном коде (избегая распространенной проблемы, когда данные доступны только в пределах одной стадии конвейера).
  • не использовать awk, tr или другие внешние проги
  • пара аксессоров get/put, чтобы скрыть синтаксис hairier
  • работает со строками, разделенными табуляцией, используя сопоставление параметров вместо IFS=

Код. file_data а также file_input только для генерации ввода, как будто из внешней команды, вызываемой из сценария. data а также cols может быть параметризован для get а также put звонки и т. д., но этот сценарий не заходит так далеко.

#!/bin/bash

file_data=( $'\t\t'       $'\t\tbC'     $'\tcB\t'     $'\tdB\tdC'   \
            $'eA\t\t'     $'fA\t\tfC'   $'gA\tgB\t'   $'hA\thB\thC' )
file_input () { printf '%s\n' "${file_data[@]}" ; }  # simulated input file
delim=$'\t'

# the IFS=$'\n' has a side-effect of skipping blank lines; acceptable:
OIFS="$IFS" ; IFS=$'\n' ; oset="$-" ; set -f
lines=($(file_input))                    # read the "file"
set -"$oset" ; IFS="$OIFS" ; unset oset  # cleanup the environment mods.

# the read-in data has (rows * cols) fields, with cols as the stride:
data=()
cols=0
get () { local r=$1 c=$2 i ; (( i = cols * r + c )) ; echo "${data[$i]}" ; }
put () { local r=$1 c=$2 i ; (( i = cols * r + c )) ; data[$i]="$3" ; }

# convert the lines from input into the pseudo-2D data array:
i=0 ; row=0 ; col=0
for line in "${lines[@]}" ; do
    line="$line$delim"
    while [ -n "$line" ] ; do
        case "$line" in
            *${delim}*) data[$i]="${line%%${delim}*}" ; line="${line#*${delim}}" ;;
            *)          data[$i]="${line}"            ; line=                     ;;
        esac
        (( ++i ))
    done
    [ 0 = "$cols" ] && (( cols = i )) 
done
rows=${#lines[@]}

# output the data array as a matrix, using the get accessor
for    (( row=0 ; row < rows ; ++row )) ; do
   printf 'row %2d: ' $row
   for (( col=0 ; col < cols ; ++col )) ; do
       printf '%5s ' "$(get $row $col)"
   done
   printf '\n'
done

Выход:

$ ./tabtest 
row  0:                   
row  1:                bC 
row  2:          cB       
row  3:          dB    dC 
row  4:    eA             
row  5:    fA          fC 
row  6:    gA    gB       
row  7:    hA    hB    hC 

Конечно


IFS=,
echo $'one\t\tthree' | tr \\11 , | (
  read one two three
  printf '<%s> ' "$one" "$two" "$three"; printf '\n'
)

Я немного перестроил пример, но только для того, чтобы он работал в любой оболочке Posix.

Обновление: Да, кажется, что пробел особенный, по крайней мере, если он в IFS. Смотрите вторую половину этого абзаца от bash(1):

   The shell treats each character of IFS as a delimiter, and  splits  the
   results of the other expansions into words on these characters.  If IFS
   is unset, or its value is exactly <space><tab><newline>,  the  default,
   then  any  sequence  of IFS characters serves to delimit words.  If IFS
   has a value other than the default, then sequences  of  the  whitespace
   characters  space  and  tab are ignored at the beginning and end of the
   word, as long as the whitespace character is in the value  of  IFS  (an
   IFS whitespace character).  Any character in IFS that is not IFS white-
   space, along with any adjacent IFS whitespace  characters,  delimits  a
   field.   A  sequence  of IFS whitespace characters is also treated as a
   delimiter.  If the value of IFS is null, no word splitting occurs.

Не нужно использовать tr, но нужно чтобы IFS является непробельным символом (иначе, как вы уже видели, множественные числа свернуты в одиночные).

$ IFS=, read -r one two three <<<'one,,three'
$ printf '<%s> ' "$one" "$two" "$three"; printf '\n'
<one> <> <three>

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

$ 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'
<one> <> <three>

Я написал функцию, которая работает вокруг этой проблемы. Эта конкретная реализация особенно важна для столбцов, разделенных табуляцией, и строк, разделенных символом новой строки, но это ограничение можно снять как простое упражнение:

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"
}

Использование следующим образом:

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

Вот быстрая и простая функция, которую я использую, чтобы избежать вызова внешних программ или ограничения диапазона вводимых символов. Работает только в bash (наверное).

Если это позволяет учесть больше переменных, чем полей, тем не менее, его необходимо изменить в соответствии с ответом Чарльза Даффи.

# Substitute for `read -r' that doesn't merge adjacent delimiters.
myread() {
        local input
        IFS= read -r input || return $?
        while [[ "$#" -gt 1 ]]; do
                IFS= read -r "$1" <<< "${input%%[$IFS]*}"
                input="${input#*[$IFS]}"
                shift
        done
        IFS= read -r "$1" <<< "$input"
}

bash 4+ с файлом карты

Мы знаем, что не может появляться внутри строки. Таким образом, мы можем преобразовать все\tк\nа затем прочитайте результат с помощьюmapfile.

      printf $'11\t12\t13\n\t22\t23\n31\t32\t\n\t42\t' >data
      mapfile -t rows <data
declare -p rows

for r in "${rows[@]}"; do
    mapfile -t row <<<"${r//$'\t'/$'\n'}"
    declare -p row
done
      declare -a rows=([0]=$'11\t12\t13' [1]=$'\t22\t23' [2]=$'31\t32\t' [3]=$'\t42\t')
declare -a row=([0]="11" [1]="12" [2]="13")
declare -a row=([0]="" [1]="22" [2]="23")
declare -a row=([0]="31" [1]="32" [2]="")
declare -a row=([0]="" [1]="42" [2]="")

или:

      while IFS= read -r row || [[ -n $row ]]; do
    mapfile -t row <<<"${row//$'\t'/$'\n'}"
    declare -p row
done <data
      declare -a row=([0]="11" [1]="12" [2]="13")
declare -a row=([0]="" [1]="22" [2]="23")
declare -a row=([0]="31" [1]="32" [2]="")
declare -a row=([0]="" [1]="42" [2]="")

При необходимости элементы массива можно присвоить простым переменным.


bash 3 с чтением

Та же идея, но просто используяread:

      while IFS= read -r line || [[ -n $line ]]; do
    row=()
    while IFS= read -r field; do
        row+=("$field")
    done <<<"${line//$'\t'/$'\n'}"
    declare -p row
done <data
      declare -a row='([0]="11" [1]="12" [2]="13")'
declare -a row='([0]="" [1]="22" [2]="23")'
declare -a row='([0]="31" [1]="32" [2]="")'
declare -a row='([0]="" [1]="42" [2]="")'

Чтобы предотвратить свертывание пустых полей, вы можете использовать любой разделитель, кроме символов пробела IFS.

Пример того, как будут вести себя разные разделители:

      #!/bin/bash

for delimiter in  $'\t'  ','  '|'  $'\377'  $'\x1f'  ;do
  line="one${delimiter}${delimiter}three"
  IFS=$delimiter read one two three <<<"$line"
  printf '<%s> ' "$one" "$two" "$three"; printf '\n'
done

<one> <three> <>
<one> <> <three>
<one> <> <three>
<one> <> <three>
<one> <> <three>

Или использовать оригинальный пример OP:

      IFS='|' read one two three <<<$(tr '\t' '|' <<<$'one\t\tthree')
printf '<%s> ' "$one" "$two" "$three"; printf '\n'

<one> <> <three>
Другие вопросы по тегам