How to sum a column 2 and column 3


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to sum a column 2 and column 3
# 22  
Old 02-23-2012
Please use code tags for codes and data samples. Otherwise extra spaces would be truncated and the post would not be displayed as you intend it to be. Now, we're not able to make out the format in which C1 and C2 are displayed.
This User Gave Thanks to balajesuri For This Post:
# 23  
Old 02-23-2012
Hi balajesuri,

Here is my below sample output file

Code:
bcbg  27   20 
blazer  34   30 
blazer  41   35 
boots  69   57 
boots  70   63 
converse  28   27 
converse  32   25 
dolce%20vita  47   42 
dolce%20vita  51   44 
dresses  22   13 
dresses  40   38 
dresses  46   35 
Ella%20moss  26   22 
flats  23   17 
flats  24   20 
free%20people  43   40 
free%20people  54   47 
frye  109   97 
frye  122   103 
frye  29   26 
handbags  27   20 
hive%20and%20honey  22   21 
hive%20and%20honey  24   20 
hunter  57   38 
hunter  64   50 
j%20brand  38   33 
j%20brand  39   34 
joie  39   34 
joie  51   43 
juicy%20couture  48   40 
juicy%20couture  65   52 
jumpsuit  29   27 
jumpsuit  38   34 
Kate%20spade  85   66 
Kate%20spade  89   75 
Lacoste  28   25 
Lacoste  33   30 
leopard  26   26 
lucky%20brand  38   33 
lucky%20brand  48   40 
marc%20jacobs  27   27 
Marc%20Jacobs  38   29 
Marc%20Jacobs  57   48 
maternity  48   38 
maternity  50   41 
maxi%20dress  49   41 
maxi%20dress  50   42 
maxi  25   21 
maxi  31   22 
michael%20kors  38   29 
michael%20kors  39   29 
Michael%20kors  73   62 
Michael%20kors  75   66 
nine%20west  29   25 
nine%20west  34   22 
order%20status  41   39 
order%20status  53   47 
peacock  38   31 
peacock  39   30 
pediped  31   23 
puma  29   23 
puma  30   26 
Rachel%20pally  38   30 
rain%20boots  29   22 
rose%20gold%20shoes  25   21 
rose%20gold  39   30 
rose%20gold  66   40 
rose%20gold  82   58 
Sam%20Edelman  34   28 
Sam%20Edelman  59   52 
sanctuary  23   18 
sanctuary  26   20 
scarf  23   20 
shoes  41   37 
shoes  45   35 
shoshanna  31   25 
sorel  31   27 
sperry  36   35 
sperry  40   34 
splendid  69   60 
splendid  82   69 
steve%20madden  35   28 
steve%20madden  40   37 
Steve%20madden  65   57 
Steve%20madden  68   59 
swedish%20hasbeens  25   22 
swim  31   24 
tinley%20road  43   41 
tinley%20road  62   50 
toms  106   87 
toms  88   74 
Tory%20burch  26   19 
true%20religion  31   30 
true%20religion  33   28 
ugg  28   23 
ugg  45   38 
Vince%20camuto  35   30 
Vince%20camuto  37   30 
wedges  28   23 
wedges  34   31


Regards
Paulwintech

Last edited by Paulwintech; 02-23-2012 at 01:43 AM..
# 24  
Old 02-23-2012
1. Post #16 satisfies your original requirement and takes care of extra spaces too.

2. To truncate extra spaces from the sample file posted in post #23, try one of these:
Code:
tr -s ' ' < inputfile

or
Code:
sed 's/[ ][ ]*/ /g' inputfile

or
Code:
perl -lpe 's/\s+/ /g' inputfile

or
Code:
awk '{print $1,$2,$3}' inputfile

There're lots of posts in this forum on how to truncate extra spaces. Have you searched for them?
# 25  
Old 02-23-2012
To remove the double/triple spaces....

Code:
 
awk '{print $1,$2,$3}' input.txt > output.xt
 perl -lane '$_=~s/\s+/ /g;print $_' input.txt > output.txt
tr -s " " < input.txt > output.txt

# 26  
Old 02-23-2012
Hi itkamaraj/ balajesuri,

I tired all the commands - no luck..... output is same as input file, nil space are removed.

Regards
Paulwintech
# 27  
Old 02-23-2012
can you post the output of the below command

Code:
 
head your-input-file.txt | od -c

# 28  
Old 02-23-2012
Hi itkamaraj,

here is the output

Code:
0000000   b   c   b   g     240   2   7 240     240   2   0 240  \n   b
0000020   l   a   z   e   r     240   3   4 240     240   3   0 240  \n
0000040   b   l   a   z   e   r     240   4   1 240     240   3   5 240
0000060  \n   b   o   o   t   s     240   6   9 240     240   5   7 240
0000100  \n   b   o   o   t   s     240   7   0 240     240   6   3 240
0000120  \n   c   o   n   v   e   r   s   e     240   2   8 240     240
0000140   2   7 240  \n   c   o   n   v   e   r   s   e     240   3   2
0000160 240     240   2   5 240  \n   d   o   l   c   e   %   2   0   v
0000200   i   t   a     240   4   7 240     240   4   2 240  \n   d   o
0000220   l   c   e   %   2   0   v   i   t   a     240   5   1 240
0000240 240   4   4 240  \n   d   r   e   s   s   e   s     240   2   2
0000260 240     240   1   3 240  \n
0000267

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sum of a column as new column based on header in a script

Hello, I am trying to store sum of a column as a new column inside a file but have to find the column names dynamically I/p c1,c2,c3,c4,c5 10,20,30,40,50 20,30,40,50,60 If i want to find sum only column c1, c3 and output it as c6,c7 O/p c1,c2,c3,c4,c5,c6,c7 10,20,30,40,50,30,70... (6 Replies)
Discussion started by: mkathi
6 Replies

2. Shell Programming and Scripting

awk to Sum columns when other column has duplicates and append one column value to another with Care

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (1 Reply)
Discussion started by: as7951
1 Replies

3. UNIX for Beginners Questions & Answers

Sum the values in the column using date column

I have a file which need to be summed up using date column. I/P: 2017/01/01 a 10 2017/01/01 b 20 2017/01/01 c 40 2017/01/01 a 60 2017/01/01 b 50 2017/01/01 c 40 2017/01/01 a 20 2017/01/01 b 30 2017/01/01 c 40 2017/02/01 a 10 2017/02/01 b 20 2017/02/01 c 30 2017/02/01 a 10... (6 Replies)
Discussion started by: Booo
6 Replies

4. UNIX for Dummies Questions & Answers

Match sum of values in each column with the corresponding column value present in trailer record

Hi All, I have a requirement where I need to find sum of values from column D through O present in a CSV file and check whether the sum of each Individual column matches with the value present for that corresponding column present in the trailer record. For example, let's assume for column D... (9 Replies)
Discussion started by: tpk
9 Replies

5. Shell Programming and Scripting

Sum column values based in common identifier in 1st column.

Hi, I have a table to be imported for R as matrix or data.frame but I first need to edit it because I've got several lines with the same identifier (1st column), so I want to sum the each column (2nd -nth) of each identifier (1st column) The input is for example, after sorted: K00001 1 1 4 3... (8 Replies)
Discussion started by: sargotrons
8 Replies

6. Shell Programming and Scripting

awk to sum a column based on duplicate strings in another column and show split totals

Hi, I have a similar input format- A_1 2 B_0 4 A_1 1 B_2 5 A_4 1 and looking to print in this output format with headers. can you suggest in awk?awk because i am doing some pattern matching from parent file to print column 1 of my input using awk already.Thanks! letter number_of_letters... (5 Replies)
Discussion started by: prashob123
5 Replies

7. Shell Programming and Scripting

Sum Of Column Based On Column Condition

I have a following inputfile MT,AP,CDM,TTML,MUM,GS,SUCC,3 MT,AP,CDM,TTSL,AP,GS,FAIL,9 MT,AP,CDM,RCom,MAH,GS,SUCC,3 MT,AP,CDM,RTL,HP,GS,SUCC,1 MT,AP,CDM,Uni,UPE,GS,SUCC,2 MT,AP,CDM,Uni,MUM,GS,SUCC,2 TTSL,AP,GS,MT,MAH,CDM,SUCC,20 TTML,AP,GS,MT,MAH,CDM,FAIL,10... (2 Replies)
Discussion started by: siramitsharma
2 Replies

8. UNIX for Dummies Questions & Answers

How to sum rows in e.g. column 1 by a category in e.g. column 2

Hi, I've shown an example of what I would like to achieve below. In the example file, I would like to sum the values in column 2 for each distinct category in column 3 (presumably making an array?) and print the sum as well as the category name and length (note:length always corresponds with... (8 Replies)
Discussion started by: auburn
8 Replies

9. Shell Programming and Scripting

print unique values of a column and sum up the corresponding values in next column

Hi All, I have a file which is having 3 columns as (string string integer) a b 1 x y 2 p k 5 y y 4 ..... ..... Question: I want get the unique value of column 2 in a sorted way(on column 2) and the sum of the 3rd column of the corresponding rows. e.g the above file should return the... (6 Replies)
Discussion started by: amigarus
6 Replies

10. UNIX for Dummies Questions & Answers

How do I sum one column based on another column?

Hi, I am new to this forum and new to awk. I have a file that contains 2 columns. Heres an example of what it looks like: 10 + 20 + 40 + 50 - 70 - So the file is tab-delimited. What I want to do is add 10 to column 1 whenever column 2 is + and substract 10 from column 1... (1 Reply)
Discussion started by: phil_heath
1 Replies
Login or Register to Ask a Question