LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Transpose a column to a custom row (https://www.linuxquestions.org/questions/linux-newbie-8/transpose-a-column-to-a-custom-row-934639/)

t.othoneos 03-15-2012 02:14 PM

Transpose a column to a custom row
 
Hei,

I have a grep result like this:

AR
AT
BG
BO
BR
CL
CN

how can i get this to a variable: AR, AT, BG, BO, BR, CL, CN

danielbmartin 03-15-2012 02:31 PM

Quote:

Originally Posted by t.othoneos (Post 4627664)
Hei,

I have a grep result like this:

AR
AT
BG
BO
BR
CL
CN

how can i get this to a variable: AR, AT, BG, BO, BR, CL, CN

Code:

var=$(grep whatever)
Daniel B. Martin

t.othoneos 03-15-2012 02:36 PM

With
Code:

var=$(grep whatever)
the data are not transposed. The value is only the last grep result

colucix 03-15-2012 02:43 PM

For comma separated values, I would add:
Code:

var=$(grep whatever | paste -sd,)
:twocents:

colucix 03-15-2012 02:46 PM

Quote:

Originally Posted by t.othoneos (Post 4627679)
With
Code:

var=$(grep whatever)
the data are not transposed. The value is only the last grep result

Strange. Based on your description, it should put all the result from grep on one line, with string separated by space. Please, provide more details and the actual grep command.

t.othoneos 03-15-2012 02:48 PM

The command is:

Code:

grep pattern file.txt | cut -d " " -f2
The result is:

Code:

AR
AT
BG
BO
BR
CL
CN


colucix 03-15-2012 02:54 PM

It works for me:
Code:

$ cat file.txt 
hello AR world
hello AT world
hello BG world
hello BO world
hello BR world
hello CL world
hello CN world

$ var=$(grep .. file.txt | cut -d " " -f2)
$ echo $var
AR AT BG BO BR CL CN
$ var=$(grep .. file.txt | cut -d " " -f2 | paste -sd,)
$ echo $var
AR,AT,BG,BO,BR,CL,CN

Which shell are you using?

t.othoneos 03-15-2012 03:06 PM

I restarted the machine and it seems to be working now. Don't know what i got wrong. Thnx anyway.


All times are GMT -5. The time now is 04:20 AM.