![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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 |
|
||||
|
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")
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 . . . |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|