[Solved] Column manipulation


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] Column manipulation
# 1  
Old 01-25-2012
[Solved] Column manipulation

Hi Everyone,

I was wondering if someone could help me to transform my data into a format I need.
Here is an example of what my data looks like

HTML Code:
  E F G H
A 1 2 3 4
B 5 6 7 8
C 9 1 2 3
D 4 5 6 7
and this is what I would need it to look like:

HTML Code:
AE 1
BE 5
CE 9
DE 4
AF 2
BF 6
CF 1
DF 5
etc
Basically, I need to take every column and put it under each other.
I've tried doing it with awk by creating separate files for each column and then combine them by using cat, but it takes a lot of time and a lot of temporary files.

Many thanks for your help and time in advance!
# 2  
Old 01-25-2012
Is that really what the data looks like, or are the row/column labels stuff added for illustration?
# 3  
Old 01-25-2012
The data really has labels (just not A,B, C etc)

Many thanks for your time!
# 4  
Old 01-25-2012
... and no white space in the column or row labels?

In the custom of this site, please post your solution first.
# 5  
Old 01-26-2012
... there are no specs in the names of the columns or rows (the label looks like this: aaaa_bbbb)

My solution was to make files for each column with awk:
Code:
awk '{print $1, $2}' file1 > file2
awk '{print $1, $3}' file1 > file3
awk '{print $1, $4}' file1> file4
etc

then combine them with cat:

Code:
cat file2 file3 file4 > file 5

The problem is that I don't have the column's label in the first column and since there are a lot of columns it takes me forever even if I make a loop, not to mention the number of temporary files and cat commands. So, I was wondering if anyone had a better solution.

Many thanks for your time!

Last edited by methyl; 01-26-2012 at 07:45 AM.. Reason: please use code tags
# 6  
Old 01-26-2012
try this

Code:
awk 'NR==1{;a=$0} NR>1{for(i=1;i<NF;i++) {v=$(i+1);r=$1;b=$0;$0=a;c=$i;$0=b;print r c":"v };  }' file

This User Gave Thanks to tarun_agrawal For This Post:
# 7  
Old 01-26-2012
It works wonderfully! Just what I needed!

Thank you so so so much!!!!!!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Column manipulation

Hi, I's like to ask for help with the following question: this is an example file: 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 I need to have it re-arranged in the following manner: first 2 columns, zero, zero, last two columns, columns 3 to... (6 Replies)
Discussion started by: zajtat
6 Replies

2. UNIX for Advanced & Expert Users

Column manipulation of a file

In a space delimited file how would you go about removing 2 and 3 column of a file and add "word" to the first column of the file in both awk and vi? (2 Replies)
Discussion started by: cokedude
2 Replies

3. Shell Programming and Scripting

[Solved] Data manipulation

Hallo Team, I need your help. I have a file that has two colums. See sample below: 105550 0.28 105550 0.24 125550 0.28 125550 0.24 215650 0.28 215650 0.24 315550 0.28 315550 0.24 335550 0.28 335550 0.24 40555 0.21 40555 0.17 415550 0.21 415550 0.17 43555 0.21 43555 0.17 (5 Replies)
Discussion started by: kekanap
5 Replies

4. Shell Programming and Scripting

Column manipulation

Hallo Team, I have a .csv file. I would like to have another column called Prefix. Under column Prefix i would like to have the first 3 numbers from the theird column. Other Licenced Operator Alpha Code Numbering Per minute Interconnect Fee per rate group (R Excluding VAT) ... (4 Replies)
Discussion started by: kekanap
4 Replies

5. Shell Programming and Scripting

[Solved] awk manipulation of sequentially named files

Hello, I am a very novice user of awk, I have a set of files named file001, file002, file003, file004, etc., each contains four fields (columns of data) separated each by a uneven number of spaces. I want to substitute those spaces by a TAB, so I am using this line of awk script: awk -v OFS="\t"... (4 Replies)
Discussion started by: jaldo0805
4 Replies

6. Shell Programming and Scripting

Column manipulation and counter

Hello, I was wondering how I could change this input: cat f1 to produce this output: As you can tell, I have several unsorted repeating strings in col1 of the input file. Each of these strings has an associated word in col2. Col2 has a total of 4 possible words (go, stop,... (3 Replies)
Discussion started by: verse123
3 Replies

7. UNIX for Dummies Questions & Answers

[Solved] Text manipulation help

Hello Unix.com How can I sort from a large email list only the emails that finish with .ca domain? cat <list> | grep "\.ca\b" >> <new list> isnt working perfectly. Any tips? Best regards, Galford D. Weller (2 Replies)
Discussion started by: galford
2 Replies

8. Shell Programming and Scripting

[Solved] Sorting a column based on another column

hello, I have a file as follows: F0100010 A C F0100040 A G BTA-28763-no-rs 77.2692 F0100020 A G F0100030 A T BTA-29334-no-rs 11.4989 F0100030 A T F0100020 A G BTA-29515-no-rs 127.006 F0100040 A G F0100010 A C BTA-29644-no-rs 7.29827 F0100050 A... (9 Replies)
Discussion started by: Homa
9 Replies

9. UNIX for Dummies Questions & Answers

[Solved] Deleting all rows where the first column equals the second column

Hi, I have a tab delimited text file where the first two columns equal numbers. I want to delete all rows where the value in the first column equals the second column. How do I go about doing that? Thanks! Input: 1 1 ABC DEF 2 2 IJK LMN 1 2 ZYX OPW Output: 1 2 ZYX OPW (2 Replies)
Discussion started by: evelibertine
2 Replies

10. Shell Programming and Scripting

Column manipulation

Hi, I have an input file in which I need to add a column with the value of part of another column. I.e. I have the following line (example, not the actual data): field1,field2,field3,test12,field5,field6I need to get the last two characters of the 4th field, and add them at the end as a... (5 Replies)
Discussion started by: Subbeh
5 Replies
Login or Register to Ask a Question