Sponsored Content
Top Forums Shell Programming and Scripting Adding numbers matching with words Post 302547100 by shoaibjameel123 on Sunday 14th of August 2011 12:22:44 AM
Old 08-14-2011
Adding numbers matching with words

Hi All,

I have a file which looks like this:

Code:
abc 1
abc 2
abc 3
abc 4
abc 5
bcd 1
bcd 3
bcd 3
bcd 5
cde 7

This file is just a miniature version of what I really have. Original file is some 1 million lines long.

I have tried to come up with the code for what I wish to accomplish but to no avail.

The task: What I want to do is the read this file (filename: log.txt) and add all those numbers which correspond to the same word and hence shrink the file to an output file say output_log.txt. I mean this is what I want:

Code:
abc 15
bcd 12
cde 7

What I have done is that added all the numbers which appear against the same word that is why I have put 15 against abc that is (1+2+3+4+5=15)

I know how to add the numbers of a coloumn using this

Code:
more log.txt | awk '{sum+=$1} END {print sum}'

But I am not able to figure out as to how to shrink the file and put the numbers against them. I am using Linux with BASH.

---------- Post updated at 12:22 PM ---------- Previous update was at 12:14 PM ----------

Great..

I could figure it out after reading some of the posts in this forum:

Here's my code

Code:
awk ' {arr[$1]+=$2}  END{for (i in arr){print i, arr[i] }}' log.txt > output_log.txt

 

10 More Discussions You Might Find Interesting

1. Web Development

Query to print numbers in words

Hi, If i give a number say "1234" the output of mysql query should be: one thousand and twenty four How to write mysql query for this? With regards Vanitha (5 Replies)
Discussion started by: vanitham
5 Replies

2. Shell Programming and Scripting

Extract numbers below words with awk

Hi all, Please some help over here. I have a Sales.txt file containing info in blocks for every sold product in the pattern showed below (only for 2 products). NEW BLOCK SALE DATA PRODUCT SERIAL 79833269999 146701011945004 .Some other data .Some... (17 Replies)
Discussion started by: cgkmal
17 Replies

3. Shell Programming and Scripting

Matching words in Perl

Hi, I have an array in which one column can contain any statement. From multiple rows of that column I want to match the statement like "Execution Started." If that row contains "Execution started." then only I have to fetch other data of other columns of that particular row. I dont want... (2 Replies)
Discussion started by: monika
2 Replies

4. UNIX for Dummies Questions & Answers

Adding words after a set of words

Greetings. I am a UNIX newbies. I am currently facing difficulties dealing with a large data set and I would like to ask for helps. I have a input file like this: ak 1 AAM1 ak 2 AAM1 ak 3 AAM1 ak 11 AMM2 ak 12 AMM2 ak 13 AMM2 ak 14 AMM2 Is there any possibility for me to... (7 Replies)
Discussion started by: Amanda Low
7 Replies

5. Shell Programming and Scripting

Print only matching words

Hi All, I have searched the forum and tried to print only matching(pattern) words from the file, but its printing entire line. I tried with grep -w. I am on sunsolaris. Eg: cat file A|A|F1|F2|A|F3|A A|F10|F11|F14|A| F20|A|F21|A|F25 I have to search for F (F followed by numbers) and ... (5 Replies)
Discussion started by: gsjdrr
5 Replies

6. Shell Programming and Scripting

Difference between words and not between numbers

Hi, Sorry in advance for propably a silly question, but I am a bit lost. On some of the linux job flow I have the following check: if ($file != 1500) then echo ERROR It works ok, all times $file is not equal to 1500 I have the error message. I try to do something similar... (7 Replies)
Discussion started by: essemario
7 Replies

7. UNIX for Dummies Questions & Answers

Trying to sort words and numbers associated with them.

Hi. I have a file containing words and numbers associated with them as follows - c 2 b 5 c 5 b 6 a 10 b 16 c 18 a 19 b 21 c 27 a 28 b 33 a 76 a 115 c 199 c 251 a 567 a 1909 (4 Replies)
Discussion started by: maq
4 Replies

8. Shell Programming and Scripting

Put numbers against the words

Hi All, I tried to solve this but the result gives me all zeros for one file. I failed to do for all 500 files. I have some 500 files with the extension .dat I have another set of files; 500 in number with extension .dic I created these .dic files by using sort -u from the actual .dat files.... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

9. Shell Programming and Scripting

matching group of words

Hi, I am stuck with a problem, will be thankful for your guidance and help. I have two files. Each line is a group of words with first word as group Id. eg. 'gp1' in File1 and 'grp1' in File2. <File1> gp1 : xyz xys3 syt2 ssx itt kty gp2 : syt2 kgk iti op2 gp3 : ppy yt5 itt sky... (11 Replies)
Discussion started by: mira
11 Replies

10. Shell Programming and Scripting

Adding (as in arithmetic) to numbers in columns in file, and writing new file with new numbers

Hi again. Sorry for all the questions — I've tried to do all this myself but I'm just not good enough yet, and the help I've received so far from bartus11 has been absolutely invaluable. Hopefully this will be the last bit of file manipulation I need to do. I have a file which is formatted as... (4 Replies)
Discussion started by: crunchgargoyle
4 Replies
Locale::Codes::LangExt(3pm)				 Perl Programmers Reference Guide			       Locale::Codes::LangExt(3pm)

NAME
Locale::Codes::LangExt - standard codes for language extension identification SYNOPSIS
use Locale::Codes::LangExt; $lext = code2langext('acm'); # $lext gets 'Mesopotamian Arabic' $code = langext2code('Mesopotamian Arabic'); # $code gets 'acm' @codes = all_langext_codes(); @names = all_langext_names(); DESCRIPTION
The "Locale::Codes::LangExt" module provides access to standard codes used for identifying language extensions, such as those as defined in the IANA language registry. Most of the routines take an optional additional argument which specifies the code set to use. If not specified, the default IANA language registry codes will be used. SUPPORTED CODE SETS
There are several different code sets you can use for identifying language extensions. A code set may be specified using either a name, or a constant that is automatically exported by this module. For example, the two are equivalent: $lext = code2langext('acm','alpha'); $lext = code2langext('acm',LOCALE_LANGEXT_ALPHA); The codesets currently supported are: alpha This is the set of three-letter (lowercase) codes from the IANA language registry, such as 'acm' for Mesopotamian Arabic. This is the default code set. ROUTINES
code2langext ( CODE [,CODESET] ) langext2code ( NAME [,CODESET] ) langext_code2code ( CODE ,CODESET ,CODESET2 ) all_langext_codes ( [CODESET] ) all_langext_names ( [CODESET] ) Locale::Codes::LangExt::rename_langext ( CODE ,NEW_NAME [,CODESET] ) Locale::Codes::LangExt::add_langext ( CODE ,NAME [,CODESET] ) Locale::Codes::LangExt::delete_langext ( CODE [,CODESET] ) Locale::Codes::LangExt::add_langext_alias ( NAME ,NEW_NAME ) Locale::Codes::LangExt::delete_langext_alias ( NAME ) Locale::Codes::LangExt::rename_langext_code ( CODE ,NEW_CODE [,CODESET] ) Locale::Codes::LangExt::add_langext_code_alias ( CODE ,NEW_CODE [,CODESET] ) Locale::Codes::LangExt::delete_langext_code_alias ( CODE [,CODESET] ) These routines are all documented in the Locale::Codes::API man page. SEE ALSO
Locale::Codes The Locale-Codes distribution. Locale::Codes::API The list of functions supported by this module. http://www.iana.org/assignments/language-subtag-registry The IANA language subtag registry. AUTHOR
See Locale::Codes for full author history. Currently maintained by Sullivan Beck (sbeck@cpan.org). COPYRIGHT
Copyright (c) 2011-2013 Sullivan Beck This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.18.2 2013-11-04 Locale::Codes::LangExt(3pm)
All times are GMT -4. The time now is 02:40 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy