Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Answers to Frequently Asked Questions Finding maximum occurrence value using awk Post 302788219 by meet77 on Monday 1st of April 2013 09:42:59 AM
Old 04-01-2013
Finding maximum occurrence value using awk

Hi everyone, I'm a new member at the forum
I have a file like this: field 2 values are either 0 or negative. file test4:

Code:
100815    -20
118125    0
143616    0
154488    0
154488    0
154488    -6
196492    -5
196492    -9
196492    -7
27332    0
29397    0

I would like to print a line containing the maximum value in field 2 of all occurrences for each value in field 1. So the desired output should be:

Code:
100815    -20
118125    0
143616    0
154488    0
196492    -5
27332    0
29397    0

I am using awk to do this. But, there are two problems: first awk prints nothing when I try to get the maximum for the values with negative numbers but it has no problem when I try the same with positive numbers. This made me take the absolute values which I can later turn back to the original values. The second problem, I get the following output with my code:

Code:
100815    20
118125    0
143616    0
154488    6
196492    7
27332    0
29397    0

Although with absolute values I should get:

Code:
100815    20
118125    0
143616    0
154488    0
196492    5
27332    0
29397    0

My code is:
awk '{$2>0?$2=$2:$2=-$2} $2==0 {$2=0} {print}' test4 | awk 'NR==1 {a[$1]=$2} {a[$1]=$2 ; if ($2<a[$1]) a[$1]=$2; else a[$1]=a[$1];} END {for(i in a) print i"\t"a[i];}'

I am sure I'm missing something basic and this could probably be done in a much simpler way. Any help is appreciated
Best reagrds to all

Last edited by joeyg; 04-01-2013 at 10:48 AM.. Reason: Please wrap data and commands inside CodeTags
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

awk + last occurrence

Hi, I'm attempting to search, using awk, a pattern range in a file. Something like: >awk '/first bit of text.../,/...last bit of text/' file Is it possible to print only the last (or first) occurrence of the pattern range this way? Thanks for any suggestions. Al (2 Replies)
Discussion started by: agibbs
2 Replies

2. UNIX for Dummies Questions & Answers

Finding nth occurrence in line and replacing it

Hi, I have several files with data that have to be imported to a database. These files contain records with separator characters. Some records are corrupt (2 separators are missing) and I need to correct them prior to importing them into the db. Example: ... (5 Replies)
Discussion started by: stresing
5 Replies

3. UNIX for Dummies Questions & Answers

Unix shell script for finding top ten files of maximum size

I need to write a Unix shell script which will list top 10 files in a directory tree on basis of size. i.e. first file should be the biggest in the whole directory and all its sub directories. Please suggest any ideas (10 Replies)
Discussion started by: abhilashnair
10 Replies

4. Shell Programming and Scripting

stop unix find on a directory structure after finding 1st occurrence

Hi, Has anyone tried to restrict Solaris 10 unix find on a large directory structure based on time to stop running after finding the first occurrence of a matching query. Basically I'm trying to build up a usage map of user workspaces based on file modification (week/month/3 months/year etc) and... (3 Replies)
Discussion started by: jm0221
3 Replies

5. Shell Programming and Scripting

Awk regular expression - I need exactly 1 occurrence of it

Hi all, I am processing a file with awk that looks like this: " 0.0021 etc 0.0123 etc 0.1234 etc ... 0.5324 etc 0.5434 etc 0.6543 etc ... 1.0344 etc 1.1344 etc ... 1.5345 etc 1.5632 etc " I need to print out only the lines that have '0' or '5' after the comma, plus I need only... (11 Replies)
Discussion started by: ioannisp
11 Replies

6. Shell Programming and Scripting

Finding Maximum value in a column

Hello, I am trying to get a script to work which will find the maximum value of the fourth column and assign that value to all rows where the first three columns match. For example: 1111 2222 AAAA 0.3 3333 4444 BBBB 0.7 1111 2222 AAAA 0.9 1111 2222 AAAA 0.5 3333 4444 BBBB 0.4 should... (8 Replies)
Discussion started by: jaysean
8 Replies

7. Shell Programming and Scripting

Perl- Finding average "frequency" of occurrence of duplicate lines

Hello, I am working with a perl script that tries to find the average "frequency" in which lines are duplicated. So far I've only managed to find the way to count how many times the lines are repeated, the code is as follows: perl -ae' my $filename= $ENV{'i'}; open (FILE, "$filename") or... (10 Replies)
Discussion started by: acsg
10 Replies

8. Shell Programming and Scripting

Finding minimum maximum and average

I am trying to find the minimum maximum and average from one file which has values Received message from https://www.demandmatrix.net/app/dm/xml] in milliseconds. Received message from https://www.demandmatrix.net/app/dm/xml] in milliseconds. Received message from... (5 Replies)
Discussion started by: aroragaurav.84
5 Replies

9. UNIX for Dummies Questions & Answers

Finding maximum occurrence value using awk

Hi everyone, I'm a new member at the forum I mistakenly posted this elsewhere too. I have a file like this: field 2 values are either 0 or negative. file test4: 100815 -20 118125 0 143616 0 154488 0 154488 0 154488 -6 196492 -5 196492 -9 196492 -7 27332 0... (5 Replies)
Discussion started by: meet77
5 Replies

10. Shell Programming and Scripting

Finding the maximum timestamp in a folder

I've the files in a directory in the following format having date +%Y%m%d%H YR_MNTH_2013061205 YR_MNTH_2013060107 and i need the latest file i.e; YR_MNTH_2013061205 to be moved to another folder #!/bin/ksh # Ksh 88 Version for test_time in YR* do --- done How can i achieve that !... (2 Replies)
Discussion started by: smile689
2 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 11:17 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy