Modify rows to columns in bash script??
I have this file.txt:
0 0 0 1 2 3 0 0 0 4 5 6 0 0 0 7 8 9 I want it to look like file2.txt: 0 0 0 0 0 0 0 0 0 1 4 7 2 5 8 3 6 9 What is the easiest way to do it? Prob. a single command line? |
I don't have a simple command, but awk would be the tool. Shouldn't be too difficult.
|
Modify rows to columns in bash script??
You could read each line into a loop and make each line an array. Then format the second file accordingly.
|
It would be nice to see what you have tried first, otherwise it is all academic and you can use any number of tools for the job.
|
i recently tried something similar with text.
it requires 2 nested for-loops, if you get them right it's a really short script. |
Using bash looks like homework. In real life I would read into a spreadsheet, then copy and paste with the transpose option.
|
Not homework, but I'll give it a try. I wasn't looking for a for loop but seems that might do the trick...
|
Not a homework, but here's the solution:
#!/bin/bash #Read in the bvecs file BVECFILE=$1 BVECNEWFILE="bvecs_new" #Store it as an array readarray BVEC < $BVECFILE BVECNUM=${#BVEC[@]} #Check the number of directions GRADNUM=$(echo $BVEC | wc -w ) #Indexing at i=1 so we get rid of the first vol0 indexed as 0 if [ -f $BVECNEWFILE ] ; then echo "$BVECNEWFILE already exists. Removing and replacing..." rm $BVECNEWFILE fi echo "New bvecs are generated in "$BVECNEWFILE" " #echo "GRADX is $GRADX" for ((i=2; i<=${GRADNUM}; i++ )); do gx=$(echo ${BVEC[0]} | awk -v x=$i '{print $x}') gy=$(echo ${BVEC[1]} | awk -v x=$i '{print $x}') gz=$(echo ${BVEC[2]} | awk -v x=$i '{print $x}') echo "$gx $gy $gz" >> $BVECNEWFILE done |
^ Nice! I never seem to have the time to learn awk properly. I found this to be an entertaining problem, so I did my own variant in Python. Looks like this:
Code:
#!/usr/bin/env python3 The original file: Code:
$ cat f1.txt Code:
$ cat f2.txt HMW |
OK, I couldn't NOT come up with a minimalist awk example since no one else did (actually I had my son do it). ;)
Because we are turning the corner, let's call it corner.awk, with the following lines: Code:
{ Code:
# awk -f corner.awk file.txt Gotta love awk! And thanks for the exercise as well! |
Not minimalist, but mine was similarly done (in awk) but using an array of arrays - I find them easy to "walk" as you can use "for i in array" followed by "for j in array[i]" ...
No need to know the bounds in advance. Edit: - :doh: not relevant here as the prints needed to be ordered - I did similar to @astrogeek for the printing. Just a nit - hanging whitespace on lines makes regex using the "$" anchor more complex than necessary. |
Quote:
Here is one without trailing whitespace and two lines shorter! Code:
{ EDIT **** Although I use a linear array the associative keys are effectively compound to produce the same logic as would an array of arrays, allowing same use of (i,j), or (n,f) in my own case. |
Cute - I did it as follows, but needs an extra print statement for the newline. I might steal that idea ... ;)
Code:
printf(n==NR?"%d" : "%d ",_[n][m]) |
Quote:
When my son produced his first cut, he had a proper BEGIN block and used print(...) statements. He also gave us the linear array idea which is good! (My own first attempt used multiple arrays as well.) In order to get the newlines right with print he had to define ORS="" to override the default (\n), among other things. I wanted to rely on defaults without a BEGIN block to keep it minimal so I changed to use of printf(...), and hastily introduced the trailing space because I was somewhat rushed, but knew a ternary would fix it - just didn't do it. No need to "steal" the idea, you would have thought of it yourself in another minute or two! Anyway, sharing and immitation are both good things - and I have FREELY shared the thought! I like to never miss an opportunity to make that point, so let's make it "official" with a universal statement of copy rights... Code:
# Acknowledgment of right to use, copy, modify and distribute: |
^ Awesome, all of the above!
I toyed with only using loops for a while, but found it easier to, eh, "visualize" using a matrix. As I keep saying, one of these days, I might find the time and energy to learn awk properly. |
All times are GMT -5. The time now is 08:02 PM. |