LinuxQuestions.org
Review your favorite Linux distribution.
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 08-29-2012, 01:53 PM   #1
atjurhs
Member
 
Registered: Aug 2012
Posts: 133

Rep: Reputation: Disabled
only the header line row to column


Hi guys,

I've looked across the net and tried several scripts, but none of them give me the correct output.

I have a file with a header line with 50+ entries I'd like to take this row data and break it into multiple column data with and assign it an index number.

Here's an example:

abc,def,ghi,jkl,mno,pqr,stu,vwx,yzz

abc 1 pqr 6
def 2 stu 7
ghi 3 vwx 8
jkl 4 yzz 9
mno 5
 
Old 08-29-2012, 02:00 PM   #2
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,774

Rep: Reputation: 1272Reputation: 1272Reputation: 1272Reputation: 1272Reputation: 1272Reputation: 1272Reputation: 1272Reputation: 1272Reputation: 1272
would be nice to specify it better, and also show us what have you tried. It looks easy for awk or perl, but I still do not know how should be the output organized.
 
Old 08-29-2012, 06:00 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
That goes for the input too. What do the lines after the header look like? How many are there? How exactly should they appear in the output? Can there be more lines than header entries? And if so, do you loop them, or what? Etc.

In short, give us an actual example of the input and the desired output, and explain in detail how they relate to each other.

And please use ***[code][/code] tags*** around all of your code and data, to preserve formatting and to improve readability. Please do not use quote tags, bolding, colors, or other fancy formatting.
 
Old 08-29-2012, 08:06 PM   #4
atjurhs
Member
 
Registered: Aug 2012
Posts: 133

Original Poster
Rep: Reputation: Disabled
High David, thank you for your sound post on what is needed to help solve my problem and some formatting.

Unfortunately I can not post an actual data file because it is held on a LAN. I'll try to do a better job describing my input data file. It's a standard csv data file that could easily be read into excel and then do a transform (using excel) on just the first row, but my computer only has linux. Here's just an example representing what one of the data files looks like.


header_field_A, header_field_B, header_field_C, header_field_D, header_field_E, header_field_F, etc...
6.53565, 12345.67, -0.00651, 12345679, 31.56001, -99.99999, etc...
6.67348, 79026.89, -1.23123, 0.000456, 78.12349, -99.99999, etc...
.
.
.

so it's just one row of header fields, delimited by comas, and then the data is also delimited by comas. The number of rows of data can be in the thousands. Some of the files will have 12 columns and some of them have 21, some 54, and some 66. They're all different. Because they are all different, it would be really good if I had a listing of how many fields are in each file and what's in each field. Because the number of columns can also be pretty large (like 66) I think that a row to column transform on just the first row would be really good. It would also be really helpful to have an index value placed next to the header field. So an output file might looks like:

1 header_field_A
2 header_field_B
3 header_field_C
4 header_field_D
5 header_field_E
6 header_field_F
.
.
.

hopefully this give a better explanation of my problem, thanks soooo much, Tabitha

Last edited by atjurhs; 08-30-2012 at 09:25 AM.
 
Old 08-30-2012, 04:19 AM   #5
dru8274
Member
 
Registered: Oct 2011
Location: New Zealand
Distribution: Debian
Posts: 105

Rep: Reputation: 36
Quote:
Originally Posted by atjurhs View Post
I'd like to take this row data and break it into multiple column data with and assign it an index number.
Perhaps something like this...
Code:
# Make a sample data file
cat >data.dat <<EOF
1bc,2ef,3hi,4kl,5no,6qr,7tu,8wx,9zz,0er,ahj,bui,cou,drt,efg,fgh,hsd,itr
000,111,222,333,444
scfg,hjkl,hjk
EOF

# variable $rows is the number of rows per column

rows=5
awk -F, 'NR==1 { k='${rows}'
                 for (n=1; n<=k; n++) {
                    x="" ; for (m=n; m<=NF; m+=k) x=x m"  "$m"\t"
                    print x } }' data.dat | column -t

1  1bc  6   6qr  11  ahj  16  fgh
2  2ef  7   7tu  12  bui  17  hsd
3  3hi  8   8wx  13  cou  18  itr
4  4kl  9   9zz  14  drt
5  5no  10  0er  15  efg
Happy with ur solution... then tick "yes" and mark as Solved!

Last edited by dru8274; 08-30-2012 at 04:28 AM.
 
1 members found this post helpful.
Old 08-30-2012, 09:26 AM   #6
atjurhs
Member
 
Registered: Aug 2012
Posts: 133

Original Poster
Rep: Reputation: Disabled
Dru,

That works perfectly!!! this will get used on a lot of files, and maybe other guys here can use it

thanx soooo much!!!

tabitha

Last edited by atjurhs; 08-30-2012 at 09:28 AM.
 
  


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
Convert row to column micyew Programming 15 06-29-2012 01:28 PM
[SOLVED] find a null value in a row/column and delete entire row umix Linux - Newbie 10 10-13-2011 01:26 AM
[ask awk] remove certain row in a column dhodho Programming 18 06-08-2010 09:36 AM
transpose row to column? resolute155 Programming 3 09-07-2009 02:29 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 07:39 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration