Visit Our UNIX and Linux User Community


Find repeated word and take sum of the second field to it ,for all the repeated words in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find repeated word and take sum of the second field to it ,for all the repeated words in awk
# 1  
Old 07-02-2013
Find repeated word and take sum of the second field to it ,for all the repeated words in awk

Hi below is the input file, i need to find repeated words and sum up the values of it which is second field from the repeated work.Im trying but getting no where close to it.Kindly give me a hint on how to go about it

Input
Code:
fruits,apple,20,fruits,mango,20,veg,carrot,12,veg,raddish,30
fruits,pinapple,10,fruits,orange,5,fruits,apple,20,Grains,wheat,100

Output
Code:
fruits,40,veg,42
fruits,35,Grains,100


Code:
 
awk -F '{
  for (j=1;j<=NF;j=j+2)
   {
    for(i=3;i<=NF;i=i+3)
    {
     if ($j=$(j+2))
      {
      tot=0 
      tot=$i+$(i+2)+tot
      toprint1=$j "," tot
      }
     if ($i!=$j)
      {
      printf $j "," $i
      }  
     }
    } 
     printf toprint1 
   }' filename


Last edited by 100bees; 07-03-2013 at 02:19 AM.. Reason: Corrected the input file
# 2  
Old 07-02-2013
yes thanks for the correction
# 3  
Old 07-02-2013
I think your input should be:
Code:
fruits,apple,20,fruits,mango,20,veg,carrot,12,veg,raddish,30
fruits,pinapple,10,fruits,orange,5,fruits,apple,20,Grains,wheat,100

---------- Post updated at 08:37 AM ---------- Previous update was at 08:24 AM ----------

If you don't mind commas at the line ends:
Code:
awk -F"," '{for (i=1;i<=NF;i+=3) a[$i]+=$(i+2);for (i in a) printf i","a[i]",";delete a;printf "\n"}' input

This User Gave Thanks to bartus11 For This Post:
# 4  
Old 07-03-2013
@bartus11 -Thank you, comma at the end is not a problem,but could you please explain me the code , and should it be delete a[i]
Code:
awk -F"," '{for (i=1;i<=NF;i+=3) a[$i]+=$(i+2);for (i in a) printf i","a[i]",";delete a;printf "\n"}' input

But I am getting an ouput like this :

Code:
fruits,40,
veg,42,
 
fruits,75,
Grains,100,
 
fruits,75,


Last edited by Scrutinizer; 07-03-2013 at 03:26 AM..
# 5  
Old 07-03-2013
Post output of:
Code:
cat -ev input

# 6  
Old 07-03-2013
the output of cat -ev input is

Code:
fruits,apple,20,fruits,mango,20,veg,carrot,12,veg,raddish,30$
fruits,pinapple,10,fruits,orange,5,fruits,apple,20,Grains,wheat,100$
$


Last edited by Scrutinizer; 07-03-2013 at 03:25 AM.. Reason: code tags
# 7  
Old 07-03-2013
Are you sure you aren't using
Code:
print i","a[i]"

instead of
Code:
printf i","a[i]"

@bartus11: When using printf it is better to specify the printf format field:
Code:
printf "%s,%s,",i,a[i]

.
delete a is not standard awk. Instead one could use:
Code:
for (i in a) { printf "%s,%s,",i,a[i]; delete a[i]} printf "\n"}'


Last edited by Scrutinizer; 07-03-2013 at 05:20 AM..

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove repeated letter words

Hi, I have this text file with these words and I need help with removing words with repeated letter from these lines. 1 ama 5 bib 29 bob 2 bub 5 civic 2 dad 10 deed 1 denned 335 did 1 eeee 1 eeeee 2 eke 8... (4 Replies)
Discussion started by: crepe6
4 Replies

2. UNIX for Beginners Questions & Answers

awk script to find repeated IP adress from trace file (.tr)

+ 8.00747 /NodeList/0/DeviceList/0/$ns3::PointToPointNetDevice/TxQueue/Enqueue ns3::PppHeader (Point-to-Point Protocol: IP (0x0021)) ns3::Ipv4Header (tos 0x0 DSCP Default ECN Not-ECT ttl 63 id 0 protocol 17 offset (bytes) 0 flags length: 540 10.1.3.3 > 10.1.2.4) ns3::UdpHeader (length: 520 49153 >... (11 Replies)
Discussion started by: Nipa
11 Replies

3. Shell Programming and Scripting

Choosing between repeated entries based on a column field

Hello, I have an input file: LOC_Os04g01890\LOC_Os05g17604 0.051307 LOC_Os04g01890\LOC_Os05g17604 0.150977 LOC_Os04g01890\LOC_Os05g17604 0.306231 LOC_Os04g01890\LOC_Os06g33100 0.168037 LOC_Os04g01890\LOC_Os06g33100 0.236293 ... (3 Replies)
Discussion started by: Sanchari
3 Replies

4. Shell Programming and Scripting

How to find repeated string in a text file

I have a text file where I need to find the string = ST*850* This string is repetaed several times in the file, so I need to know how many times it appears in the file, this is the text files: ISA*00* *00* *08*925485USNR *ZZ*IMSALADDERSP... (13 Replies)
Discussion started by: cucosss
13 Replies

5. UNIX for Dummies Questions & Answers

awk -repeated pattern match

Hi. How can I write this differently: awk '$3 ~ /0001/{print}' Is there a way to write 0001 differently. I am looking for the pattern 01, with 3 or more 0 and 3 or more 1 in a pattern. Thanks. (12 Replies)
Discussion started by: danieladna
12 Replies

6. Shell Programming and Scripting

delete repeated strings (tags) in a line and concatenate corresponding words

Hello friends! Each line of my input file has this format: word<TAB>tag1<blankspace>lemma<TAB>tag2<blankspace>lemma ... <TAB>tag3<blankspace>lemma Of this file I need to eliminate all the repeated tags (of the same word) in a line, as in the example here below, but conserving both (all) the... (2 Replies)
Discussion started by: mjomba
2 Replies

7. Shell Programming and Scripting

Help in counting the no of repeated words with count in a file

Hi Pls help in solving my doubt.Iam having file like below file1.txt priya jenny jenny priya raj radhika priya bharti bharti Output required: I need a output like count of repeated words with name for ex: priya 3 jenny 2 (4 Replies)
Discussion started by: bha148
4 Replies

8. Shell Programming and Scripting

To find the repeated name in subject

Hi, I am using a mail application, When I use to check with the command, I will get the outputs as q -s 6128175 (9, 9/6128175) Return-path: minka.bell@abcd.org From: "Minka Bell" <minka.bell@abcd.org> To: <dcrouch@xyz.org>, Subject: Open items with new PICT system Date: Wed,... (12 Replies)
Discussion started by: gsiva
12 Replies

9. Shell Programming and Scripting

Delete repeated word in text file

Hi expert, I am using C shell. And i trying to delete repeated word. Example file.txt: BLUE YELLOW RED VIOLET RED RED BLUE WHITE YELLOW BLACK and i wan store the output into a new file: BLUE (6 Replies)
Discussion started by: vincyoxy
6 Replies

10. UNIX for Dummies Questions & Answers

how to find a word repeated in a file

Hi everyone, I have a file in which a word is repeated more than one time and I want to know how many times it is repeated. ex: if i repeated word 'guru' in 10 lines I can get the o/p as: cat filename | grep -c 'guru'. How ever if the word is repeated more than one time, then how can I... (4 Replies)
Discussion started by: gurukottur
4 Replies

Featured Tech Videos