3

I need help transposing a file that just simply has some numbers in them with rows and columns. I can't use awk shell or perl so it makes it kind of hard. I've been working on it for a couple of hours and can't get it working correctly. I tried a couple of other things but this is what I have right now. It runs, but it doesn't print out anything, so that leads me to conclude that something is wrong within my code. Also if you dont know by transpose if a file had :

1 2 3 
4 5 6 

... it would then print out

1 4
2 5
3 6

Here is my code:

if [ $# -gt 2 ]
then
  echo"System error">&2
exit 1

elif [[ $# -eq 2 && -e "$2" && -r "$2" ]]
then 
while read -a line; do
    for ((i=0; i < "${#line[@]}"; i++)); do
        a[$i]="${a[$i]} ${line[$i]}"
    done
done < $2

for ((i=0; i < ${#a[@]}; i++)); do
    echo ${a[i]}
done


fi
8
  • It works for me (except that the $# -gt 2 condition needs a space between echo and the message). How are you running it (and what arguments are you passing)? Commented Oct 10, 2019 at 5:17
  • Thank you for the reply. so when I go to run this script I go: matrix transpose m1... which matrix is the name of this file, tranpsose is this function and then m1 is just a text file containing numbers I need to tranpsose. Thank you for the reply Commented Oct 10, 2019 at 5:19
  • That sounds like it should work. I'd try putting set -x before the problem section, so you can see what's happening as it runs. Commented Oct 10, 2019 at 5:22
  • Where exactly do you mean in needs space? are you referring too the echo" system failure" because that doesn't really do anything Commented Oct 10, 2019 at 5:24
  • Yes, it should be echo "system failure" -- try both with and without the space, and compare the results. Commented Oct 10, 2019 at 5:26

2 Answers 2

2

If possible use awk:

Source (file.txt):

1 2 3
4 5 6

Result:

1 4
2 5
3 6

Oneline awk sctript:

awk '{ for (i=1; i<=NF; i++) a[i]= (a[i]? a[i] FS $i: $i) } END{ for (i in a) print a[i] }' file.txt

It works same with

1 2 3         1 4 7
4 5 6    ->   2 5
7             3 6

and with

1 2 3 4       1 5
5 6 7    ->   2 6
              3 7
              4
Sign up to request clarification or add additional context in comments.

1 Comment

Great solution. Should be accepted.
1

Instead of writing a Bash function, we could use rs, written specifically for reshaping matrices. This command does exactly what you ask for:

rs -T

3 Comments

But this has to be installed, right? So the awk solution might fit better.
Well, yes - but it's such a useful tool, it's worth getting your sysadmin to install it everywhere!
Okay, it might be worth to install it. If reshaping matrices will become a common task for me I will consider rs. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.