Save output into file bash scripting

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?

Is this a homework assignment?

It is kind of hard to guess at how to change your program to write its output in the order you want if you don't show us the code that is writing output in snake order... Show us your code!

its an assignment. im stuck at this part.

#!/bin/bash
#program for columnar transposition
#file from previous question (a)
file="encrypted.txt"

#remove all the spaces and newline and put it on a new file
name=$(cat $file)
echo $name | tr -d ' ' > temp.txt

file4="temp.txt"
name4=$(cat $file4)
echo $name4 |  sed 's/\(.\)/\1\n/g' > file4.txt

echo "enter how many cols:"
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

---------- Post updated at 05:17 PM ---------- Previous update was at 05:14 PM ----------

the output is all ok and correct.
now i dont know how to put all the output into 1 single string.
like i said, i googled it and found how to combine all but not with any kind of sorting

Homework needs to be posted to the Homework & Coursework Questions forum and needs to be submitted with a fully filled out template as provided in the Rules for Homework & Coursework Questions Forum.

Please fill out the form and resubmit your question in the proper forum.

This thread is closed.