LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-15-2012, 02:14 PM   #1
t.othoneos
LQ Newbie
 
Registered: Oct 2011
Location: Athens, Greece
Posts: 16

Rep: Reputation: Disabled
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
 
Old 03-15-2012, 02:31 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,546

Rep: Reputation: 468Reputation: 468Reputation: 468Reputation: 468Reputation: 468
Quote:
Originally Posted by t.othoneos View Post
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
 
Old 03-15-2012, 02:36 PM   #3
t.othoneos
LQ Newbie
 
Registered: Oct 2011
Location: Athens, Greece
Posts: 16

Original Poster
Rep: Reputation: Disabled
With
Code:
var=$(grep whatever)
the data are not transposed. The value is only the last grep result
 
Old 03-15-2012, 02:43 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
For comma separated values, I would add:
Code:
var=$(grep whatever | paste -sd,)
 
Old 03-15-2012, 02:46 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by t.othoneos View Post
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.
 
Old 03-15-2012, 02:48 PM   #6
t.othoneos
LQ Newbie
 
Registered: Oct 2011
Location: Athens, Greece
Posts: 16

Original Poster
Rep: Reputation: Disabled
The command is:

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

Code:
AR
AT
BG
BO
BR
CL
CN
 
Old 03-15-2012, 02:54 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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?
 
Old 03-15-2012, 03:06 PM   #8
t.othoneos
LQ Newbie
 
Registered: Oct 2011
Location: Athens, Greece
Posts: 16

Original Poster
Rep: Reputation: Disabled
I restarted the machine and it seems to be working now. Don't know what i got wrong. Thnx anyway.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] find a null value in a row/column and delete entire row umix Linux - Newbie 10 10-13-2011 01:26 AM
[SOLVED] Transpose multiple rows into a single column wonjusup Linux - Newbie 13 04-09-2011 06:53 AM
transpose row to column? resolute155 Programming 3 09-07-2009 02:29 PM
Transpose info between blocks from column to lines arragement cgcamal Programming 1 11-16-2008 08:48 PM
Transposing a column into a row mayyash Linux - General 1 09-30-2005 02:23 AM


All times are GMT -5. The time now is 01:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration