LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   only the header line row to column (https://www.linuxquestions.org/questions/linux-newbie-8/only-the-header-line-row-to-column-4175424632/)

atjurhs 08-29-2012 01:53 PM

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

pan64 08-29-2012 02:00 PM

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.

David the H. 08-29-2012 06:00 PM

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.

atjurhs 08-29-2012 08:06 PM

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

dru8274 08-30-2012 04:19 AM

Quote:

Originally Posted by atjurhs (Post 4767617)
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!

atjurhs 08-30-2012 09:26 AM

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


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