The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
adding columns Kelam_Magnus Shell Programming and Scripting 12 02-10-2009 10:56 AM
List to columns and awk help baghera Shell Programming and Scripting 17 08-28-2007 09:20 AM
How can I use columns with a command? chrchcol Shell Programming and Scripting 0 07-24-2006 08:51 PM
re arrange the columns ahmedwaseem2000 Shell Programming and Scripting 0 09-23-2005 03:51 AM
add columns with awk tonet Shell Programming and Scripting 4 06-21-2005 04:02 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-21-2007
vskr72 vskr72 is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 11
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 (permalink)  
Old 03-21-2007
dennis.jacob dennis.jacob is offline Forum Advisor  
dj - the student
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 607
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 (permalink)  
Old 03-21-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,553
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 (permalink)  
Old 03-21-2007
dennis.jacob dennis.jacob is offline Forum Advisor  
dj - the student
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 607
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 (permalink)  
Old 03-21-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,553
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
.
.
.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:16 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0