Save output into file bash scripting

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    Hi there.

i have created a program that in the end it will give output like this

1   2   3   4   5
10  9   8   7   6
11  12  13  14  15
..............  17

i wonder how to save the output into a single string and into a file.
i.e 1 10 11 12 9 2 3 8 13 14 7 4 5 6 15 17 (in this order, its like a snake)

i googled it but i only came across with saving it row by row (1 2 3 4 5 10 9......)

any idea how?

  1. Relevant commands, code, scripts, algorithms:
file4="temp.txt"
name4=$(cat $file4)
echo $name4 |  sed 's/\(.\)/\1\n/g' > file4.txt

echo "enter how many rows :"
read cols

IFS=$'\n' a=($(cat file4.txt))
for j in $(seq ${#a[*]}); do
    [[ ${a[$j-1]} = $name ]] && echo "${a[$i]}"
done

for (( i=0; i<=$(( ${#a[@]} / cols )); ++i )); do
    for (( j=0; j<cols; ++j )); do
        if (( i%2 )); then idx=$(( (i + 1) / 2 * 2 * cols - j - 1 ))
        else idx=$(( (i / 2) * 2 * cols + j )); fi
        printf "%-4s  " "${a[idx]}"
    done
    printf "\n"
done
  1. The attempts at a solution (include all code and scripts):

  2. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Please repost and complete the entire form
---
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.