The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
create filename with 'DD/MM/YYYY' date format royalibrahim Shell Programming and Scripting 4 04-12-2008 08:24 AM
To convert multi format file to a readable ascii format gaur.deepti UNIX for Dummies Questions & Answers 5 03-25-2008 03:03 PM
How to create concatenate a file in tab format ahjiefreak UNIX for Dummies Questions & Answers 12 12-11-2007 08:22 PM
Convert UTF8 Format file to ANSI format rajreddy UNIX for Dummies Questions & Answers 9 05-25-2007 08:26 AM
Convert UTF8 Format file to ANSI format rajreddy UNIX for Advanced & Expert Users 1 05-24-2007 06:40 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-22-2008
enigma_83 enigma_83 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 7
Help to Create this file format structure

This data comes form the table and exported into the file in this format

File1 Format
weboffercode1,sourcecode1,1,1,1,1,1,1

weboffercode1,sourcecode2,1,1,1,1,1,1

weboffercode1,sourcecode1,1,1,1,1,1,1

weboffercode1,sourcecode3,1,1,1,1,1,1

weboffercode1,sourcecode3,1,1,1,1,1,1

weboffercode2,sourcecode1,1,1,1,1,1,1

weboffercode2,sourcecode1,1,1,1,1,1,1

weboffercode2,sourcecode2,1,1,1,1,1,1

weboffercode3,sourcecode1,1,1,1,1,1,1

weboffercode3,sourcecode2,1,1,1,1,1,1

I have to format this file in the following format as :-

File2 :
sum of the field f3,f4,f5,f6,f7,f8 for same WebOfferCode and Source code combination

weboffercode1,sourcecode1,2,2,2,2,2,2

weboffercode1,sourcecode2,1,1,1,1,1,1

weboffercode1,sourcecode3,2,2,2,2,2,2

sum of fields f3,f4,f5,f6,f7,f8 for same WebOfferCode

TOTAL, weboffercode1,5,5,5,5,5,5

sum of the field f3,f4,f5,f6,f7,f8 for same WebOfferCode and Source code combination

weboffercode2,sourcecode1,2,2,2,2,2,2

weboffercode2,sourcecode2,1,1,1,1,1,1

sum of fields f3,f4,f5,f6,f7,f8 for same WebOfferCode

TOTAL, weboffercode1,3,3,3,3,3,3

sum of the field f3,f4,f5,f6,f7,f8 for same WebOfferCode and Source code combination

weboffercode3,sourcecode1,1,1,1,1,1,1

weboffercode3,sourcecode2,1,1,1,1,1,1

sum of fields f3,f4,f5,f6,f7,f8 for same WebOfferCode
TOTAL, weboffercode2,2,2,2,2,2,2

So that if I save this file2.csv it is in the format of the picture as shown above.Let me know how can I do the operations on File1 to create such file format as File2…Primarily I was to know how to write a script to add the data present in the file column wise.
  #2 (permalink)  
Old 04-22-2008
davenorm davenorm is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 9
hmmm...

can't you just do it all in excel - i imagine excel is much better at maths than most shell script languages
  #3 (permalink)  
Old 04-22-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,416
Something like that ?
Code:
sort -t, -k1,2 inputfile | \
awk -F, '

function new_sourcecode(scode) {
   if (ssum3 != "")
      printf("%s,%s,%d,%d,%d,%d,%d,%d\n",weboffercode,sourcecode,
                                         ssum3,ssum4,ssum5,ssum6,ssum7,ssum8);
   ssum3 = ssum4 = ssum5 = ssum6 = ssum7 = ssum8 = 0;
   sourcecode = scode;
}

function new_weboffercode(wcode) {
   if (wsum3 != "")
      printf("TOTAL,%s,%d,%d,%d,%d,%d,%d\n",weboffercode,
                                            wsum3,wsum4,wsum5,wsum6,wsum7,wsum8);
   wsum3 = wsum4 = wsum5 = wsum6 = wsum7 = wsum8 = 0;
   weboffercode = wcode;
}
{
   if ($1 != weboffercode) {
      new_sourcecode($2);
      new_weboffercode($1);
   } else if ($2 != sourcecode) {
      new_sourcecode($2);
   }

   ssum3 += $3; wsum3 += $3;
   ssum4 += $4; wsum4 += $4;
   ssum5 += $5; wsum5 += $5;
   ssum6 += $6; wsum6 += $6;
   ssum7 += $7; wsum7 += $7;
   ssum8 += $8; wsum8 += $8;
}
END {
   new_sourcecode("");
   new_weboffercode("");
}
'
Output:
Code:
weboffercode1,sourcecode1,2,2,2,2,2,2
weboffercode1,sourcecode2,1,1,1,1,1,1
weboffercode1,sourcecode3,2,2,2,2,2,2
TOTAL,weboffercode1,5,5,5,5,5,5
weboffercode2,sourcecode1,2,2,2,2,2,2
weboffercode2,sourcecode2,1,1,1,1,1,1
TOTAL,weboffercode2,3,3,3,3,3,3
weboffercode3,sourcecode1,1,1,1,1,1,1
weboffercode3,sourcecode2,1,1,1,1,1,1
TOTAL,weboffercode3,2,2,2,2,2,2
Jean-Pierre.
  #4 (permalink)  
Old 04-25-2008
enigma_83 enigma_83 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 7
Thanks a lot it worked....

Well i tried running it worked..thanks!!1
  #5 (permalink)  
Old 04-29-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
Code:
nawk 'BEGIN{FS=","}
{
a[$1$2]=$3+a[$1$2]
b[$1$2]=$4+b[$1$2]
c[$1$2]=$5+c[$1$2]
d[$1$2]=$6+d[$1$2]
e[$1$2]=$7+e[$1$2]
f[$1$2]=$8+f[$1$2]
A[$1]=$3+A[$1]
B[$1]=$3+B[$1]
C[$1]=$3+C[$1]
D[$1]=$3+D[$1]
E[$1]=$3+E[$1]
F[$1]=$3+F[$1]
if(t[$1]=="")
	t[$1]=$2
else if (index(t[$1],$2)==0)
	t[$1]=sprintf("%s,%s",t[$1],$2)
}
END{
for (i in A)
{
	tt=split(t[i],temp,",")
	for (j=1;j<=tt;j++)
	{
		p=sprintf("%s%s",i,temp[j])
		print i","p","a[p]","b[p]","c[p]","d[p]","e[p]","f[p]
	}
	print "TOTAL,"i","A[i]","B[i]","C[i]","D[i]","E[i]","F[i]
}
}' filename
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 06:11 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