Функция транспонирования матрицы в bash - PullRequest
0 голосов
/ 17 апреля 2020

Я пишу функцию транспонирования, которая транспонирует размер MxN в NxM. вот моя матрица ввода

1^I2^I3$
4^I5^I6$
7^I8^I9$

также может быть

1 2 3
4 5 6
7 8 9

И в настоящее время у меня есть следующий код:

transpose() {
  tempfile1=./tempTrans
  tempfile2=./tempDim
  tempfile3=./tempcellFile

  cols=0
  colCount=1
  tempCell=0
  result=0

  #info of dimension, store in a temp file, dims is other function I have
  dims $1 > "$tempfile2"  
  cols=$( dims $1| cut -c 3 )

  while (( "$colCount" < (($cols + 1)) ))
  do

    # cut the input file and store into temp cell file
    tempCell=$(cut -f "$colCount" "$1")
    printf "%s" "$tempCell" > "$tempfile3"
    (( colCount++ ))

    # convert all the \n and the \t
    # I think where the problem should be
    result=$(cat "$tempfile3" | tr "\n" "\t")
    printf "%s" "$result" > "$tempfile1"


    # add anew line to the ned
    echo >> "$tempfile1"

  done

  # print trans result and delete all temp files
  cat "$tempfile1"
  rm $tempfile1
  rm $tempfile2
  rm $tempfile3 

  exit 0
}

Результат предположим, что

1 4 7
2 5 8
3 6 9

Но мой код печатает только

3 6 9

Это моя первая bash программа, и я действительно не знаю, где я делаю ошибку и как ее изменить.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...