To transpose row into column

Hi All,
In shell, I have below data coming from some some text file as below:

                    .     351706    5861.8       0.026        0.012       12.584        0.026       0.012      12.582      0.000      0.000      0.000

Now i need the above data to be transposed as below

351706
5861.8
0.026
0.012
12.584
0.026
0.012
12.582
0.000
0.000
0.000

Please advise

Hope this helps:

echo "351706 5861.8 0.026 0.012 12.584 0.026 0.012 12.582 0.000 0.000 0.000" | sed 's/ /\n/g'

OR

echo "351706 5861.8 0.026 0.012 12.584 0.026 0.012 12.582 0.000 0.000 0.000" | tr ' ' '\n'
1 Like

Thanks for looking into this . Actually

. 351706 5861.8 0.026 0.012 12.584 0.026 0.012 12.582 0.000 0.000 0.000

is the redirected output and is dynamic and will store in some file , let say file1 , can we do it through awk
I am able to get it through

$ awk '{$1=$2=""; $0=$0; $1=$1}1' OFS="\n" file1 

but the issue is that i am unable to fetch the first numeric value i.e. 351706 here

5861.8
0.026
0.012
12.584
0.026
0.012
12.582
0.000
0.000
0.000

Thanks Again!!

echo ". 351706 5861.8 0.026 0.012 12.584 0.026 0.012 12.582 0.000 0.000 0.000" | awk '$1=$1' OFS="\n"
1 Like

Try

$ echo ". 351706 5861.8 0.026 0.012 12.584 0.026 0.012 12.582 0.000 0.000 0.000" | awk '/[0-9]/' RS=" "
351706
5861.8
0.026
0.012
12.584
0.026
0.012
12.582
0.000
0.000
0.000
awk '{for(i=2;i<=NF;i++) print $i}' file1
1 Like

hi Techie, The output of above is same what i have achieved but , where as i dont want . in the first line,

.
351706
5861.8
0.026
0.012
12.584
0.026
0.012
12.582
0.000
0.000
0.000

My way of doing is:

sed -n "11p" sample.txt >tmp 

                   .     351706    5861.8       0.026        0.012       12.584        0.026       0.012      12.582      0.000      0.000      0.000
$ cat tmp
 .     351706    5861.8       0.026        0.012       12.584        0.026       0.012      12.582      0.000      0.000      0.000
 
$ awk '$1=$1' OFS="\n" tmp   
 
.
351706
5861.8
0.026
0.012
12.584
0.026
0.012
12.582
0.000
0.000
0.000

My issue is this i dont want "." in the beiginning , can u pls help me , i can i achieve it

I want to use take the output from tmp and then transpose it in column , the way i have shown above.

Did you try #5 and #6 ?

--edit--

$ awk '/[0-9]/' RS=" " tmp

Hi Yodi,
Thanks for your one liner code. it worked perfectly..

One more tip if you may like to give me, say i have to present the data in below format , what would be the awk code

5861.8
0.026
0.012
12.584
0.026
0.012
12.582
0.000
0.000
0.000
351706

change loop make { for(i=3;i<=NF;i++)....}

1 Like
xargs -n1 <yourfile
1 Like

Hello,

Here are some more approaches are there.

1st:

cat change_row_row1 | tr ' ' '\n' | grep -v '^$'

2nd:

perl -p -e 's/\s+/\n/g' change_row_row1 | grep -v '^$'

Output will be as follows in both conditions.

351706
5861.8
0.026
0.012
12.584
0.026
0.012
12.582
0.000
0.000
0.000

Thanks,
R. Singh

Thanks

$ cat tmp
575732 159.93 0.012 0.005 0.297 0.012 0.004 0.297 0.000 0.000 0.006 0.001 0.000 0.017
$ awk '{for(i=1;i<=NF;i++) print $i}' tmp
575732
159.93 ----I have issue like i want 159.93 value to be printed at last row of column 
0.012
0.005
0.297
0.012
0.004
0.297
0.000
0.000
0.006
0.001
0.000
0.017

the above highlighted data <159.93> should be at this location

Expected Results should be as follows:

575732
0.012
0.005
0.297
0.012
0.004
0.297
0.000
0.000
0.006
0.001
0.000
0.017
159.93
$ awk '{last=$2;for(i=1;i<=NF;i++)if(i!=2)print $i;print last}' tmp
$ awk '{last=$2;for(i=1;i<=NF;i++)if(i!=2)print $i}END{print last}' tmp
$ awk '{last=$2; $2=""; $0=$0; $1=$1; for(i=1;i<=NF;i++)print $i;print last}' tmp

Hello,

Could you please try the following one.

awk -vs1="159.93" '$0 !~ s1 {print}; END{print s1}' file_name

Output will be as follows.

75732
0.012
0.005
0.297
0.012
0.004
0.297
0.000
0.000
0.006
0.001
0.000
0.017
159.93

Thanks,
R. Singh

This will not work for given input, RS is missing

Thanks Akshay, following should work.

xargs -n1 < file_name | awk -vs1="159.93" '$0 !~ s1 {print}; END{print s1}'

Output should be as follows.

575732
0.012
0.005
0.297
0.012
0.004
0.297
0.000
0.000
0.006
0.001
0.000
0.017
159.93

Thanks,
R. Singh

1 Like