Row to Columns


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Row to Columns
# 1  
Old 03-21-2007
Row to Columns

Hi,

I have a file like this.

1,1,1,0,0,0
1,1,2,1,0,0
1,1,3,0,0,0
1,1,4,0,0,0
...........
...........
1,1,24,0,0,0
1,1,25,0,0,0
1,1,26,1,0,0
1,1,27,0,0,0
1,2,1,0,0,0
1,2,2,0,0,0
1,2,3,0,0,0
1,2,4,0,0,0
1,2,5,1,0,0
1,2,6,1,0,0
............

I need to create an output file like this:

1
1
1
0
0
0
2
1
0
0

Basically, the output is like this:
1. The first record repeats as it is in vertical format.
2. Second record does not repeat completely. If u look at the file between teh first and 2nd record, what si common is 1,1. So, that is not repeated. But the data from 3rd column and onwards comes.
3. This repeats till we loop through all the 1,1 in the first and 2nd columns.
4. Then we start with record 1,2,1,0,0,0, because the first and the 2nd columns do not match
5. When we read the next record, we skip 1,2 and just take values from 3rd column onwards.

Hope my requirement or description is clear. Thanks.

Satish
# 2  
Old 03-21-2007
Please give a try on thsi..

awk -F"," '{if (NR==1)
{first=$1; sec=$2; flag=0; }
if(first==$1 && sec==$2)
{ if (flag==1) {print $3"\n"$4"\n"$5"\n"$6}
else
{print first"\n"sec"\n"$3"\n"$4"\n"$5"\n"$6; flag=1;}
}
else
{first=$1; sec=$2; flag=0;
if (flag==1) {print $3"\n"$4"\n"$5"\n"$6;}
else
{print first"\n"sec"\n"$3"\n"$4"\n"$5"\n"$6;flag=1;}
} }' filename

Thnx.Dennis
# 3  
Old 03-21-2007
Code:
# more file
1,1,1,0,0,0
1,1,2,1,0,0
# tr ',' '\n' < file
1
1
1
0
0
0
1
1
2
1
0
0

# 4  
Old 03-21-2007
Quote:
Originally Posted by ghostdog74
Code:
# more file
1,1,1,0,0,0
1,1,2,1,0,0
# tr ',' '\n' < file
1
1
1
0
0
0
1
1
2
1
0
0

Ghost dog..
It is not a simple row-to-column conversion...
# 5  
Old 03-21-2007
my bad. here's a Python alternative:
Code:
d = {} #store results
for line in open("file"):
  line = line.strip().split(",")
  firsttwo = ','.join(line[0:2])
  therest = ','.join(line[2:]) 
  if not d.has_key(firsttwo):
    d[firsttwo] = therest
  else:
    d[firsttwo] = d[firsttwo] + "," +  therest

for key in sorted(d.keys()):
  print key.replace(",","\n")
  print d[key].replace(",","\n")

output:
Code:
# ./test.py
1
1
1
0
0
0
2
1
0
0
3
0
0
0
4
0
0
0
24
0
0
0
25
0
.
.
.

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Transpose columns to row

Gents Using the attached file and using this code. awk '{print substr($0,4,2)}' input.txt | sort -k1n | awk '{a++}END{for(i in a) print i,a}' | sort -k1 > output i got the this output. 00 739 01 807 02 840 03 735 04 782 05 850 06 754 07 295 08 388 09 670 10 669 11 762 (8 Replies)
Discussion started by: jiam912
8 Replies

2. UNIX for Dummies Questions & Answers

Select 2 columns and transpose row by row

Hi, I have a tab-delimited file as follows: 1 1 2 2 3 3 4 4 a a b b c c d d 5 5 6 6 7 7 8 8 e e f f g g h h 9 9 10 10 11 11 12 12 i i j j k k l l 13 13 14 14 15 15 16 16 m m n n o o p p The output I need is: 1 1 a a 5 5 e e 9 9 i i 13... (5 Replies)
Discussion started by: mvaishnav
5 Replies

3. UNIX for Dummies Questions & Answers

Dividing all columns by the nth row

Hi All! if I have a file like this: 8 10 12 14 16 18 0 2 6 2 4 6 8 10 12 16 18 10 6 8 12 16 18 0 2 2 6 2 2 2 2 2 2 2 2 2 10 12 16 4 8 16 4 16 0 8 10 14 16 0 4 8 12 14 And I want to divide all the lines by line 4 for example... (6 Replies)
Discussion started by: cosmologist
6 Replies

4. UNIX for Dummies Questions & Answers

help [[row and columns]]

i ask to do ,,program that convert the last row to be the first row ,,,and after that exchange the the columns ex,, 1 2 3 4 5 6 7 8 9 to be 7 8 9 4 5 6 1 2 3 and then to be 9 8 7 6 5 4 3 2 1 (0 Replies)
Discussion started by: khaled1989kh
0 Replies

5. Shell Programming and Scripting

help [[row and columns]]

i ask to do ,,program that convert the last row to be the first row ,,,and after that exchange the the columns ex,, 1 2 3 4 5 6 7 8 9 to be 7 8 9 4 5 6 1 2 3 and then to be 9 8 7 6 5 4 3 2 1 give mee the code .... (0 Replies)
Discussion started by: khaled1989kh
0 Replies

6. Shell Programming and Scripting

Print columns from each row

I have awk command to print column 8 awk '/select/ {print $8}' which will print column 8 But I need to print 3, 5 and 8 column in a row and each column should be de-limited by "\t" Hope anyone help me quickly. (2 Replies)
Discussion started by: elamurugu
2 Replies

7. UNIX for Dummies Questions & Answers

convert matrix to row and columns

Dear Unix Gurus, I have a sample data set that looks like this y1 y2 y3 y4 y5 x1 0.3 0.5 2.3 3.1 5.1 x2 1.2 4.1 3.5 1.7 1.2 x3 3.1 2.1 1.0 4.1 2.1 x4 5.0 4.0 6.0 7.0 1.1 I want to open it up so that I get x1 y1 0.3 x2 y1 1.2 x3 y1 3.1 x4 y1 5.0 x1 y2 0.5 x2 y2... (3 Replies)
Discussion started by: tintin72
3 Replies

8. Shell Programming and Scripting

Row to Columns question

Hi, I have a question for Row to Columns by script. e.g. i have a file Start 1 2 3 4 End Start 1 2 3 3 4 4 End (6 Replies)
Discussion started by: bleach8578
6 Replies

9. Shell Programming and Scripting

3 Columns to Row question

I have a file that looks like this... a b c d e f g h i I would like to it to be like this a b c d e f g h i Thanks (8 Replies)
Discussion started by: elbombillo
8 Replies

10. Shell Programming and Scripting

how to change row into columns

hi, I have a input file like a,123,456,789,012,.......,b I need to change the output file into a,123,b a,456,b a,789,b a,012,b a,...,b like that.. how to achieve that through UNIX................. (5 Replies)
Discussion started by: aaha_naga
5 Replies
Login or Register to Ask a Question