Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Finding maximum occurrence value using awk Post 302788233 by joeyg on Monday 1st of April 2013 10:00:52 AM
Old 04-01-2013
Your original post

This is your original post, showing each dataset on its own line.

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:
Code:
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
 

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. Answers to Frequently Asked Questions

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: 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... (1 Reply)
Discussion started by: meet77
1 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
form_driver(3CURSES)					     Curses Library Functions					      form_driver(3CURSES)

NAME
form_driver - command processor for the forms subsystem SYNOPSIS
cc [ flag... ] file... -lform -lcurses [ library... ] #include <form.h> int form_driver(FORM *form, int c); DESCRIPTION
The form_driver() function is the workhorse of the forms subsystem; it checks to determine whether the character c is a forms request or data. If it is a request, the form driver executes the request and reports the result. If it is data (a printable ASCII character), it enters the data into the current position in the current field. If it is not recognized, the form driver assumes it is an application- defined command and returns E_UNKNOWN_COMMAND. Application defined commands should be defined relative to MAX_COMMAND, the maximum value of a request listed below. Form driver requests: REQ_NEXT_PAGE Move to the next page. REQ_PREV_PAGE Move to the previous page. REQ_FIRST_PAGE Move to the first page. REQ_LAST_PAGE Move to the last page. REQ_NEXT_FIELD Move to the next field. REQ_PREV_FIELD Move to the previous field. REQ_FIRST_FIELD Move to the first field. REQ_LAST_FIELD Move to the last field. REQ_SNEXT_FIELD Move to the sorted next field. REQ_SPREV_FIELD Move to the sorted prev field. REQ_SFIRST_FIELD Move to the sorted first field. REQ_SLAST_FIELD Move to the sorted last field. REQ_LEFT_FIELD Move left to field. REQ_RIGHT_FIELD Move right to field. REQ_UP_FIELD Move up to field. REQ_DOWN_FIELD Move down to field. REQ_NEXT_CHAR Move to the next character in the field. REQ_PREV_CHAR Move to the previous character in the field. REQ_NEXT_LINE Move to the next line in the field. REQ_PREV_LINE Move to the previous line in the field. REQ_NEXT_WORD Move to the next word in the field. REQ_PREV_WORD Move to the previous word in the field. REQ_BEG_FIELD Move to the first char in the field. REQ_END_FIELD Move after the last char in the field. REQ_BEG_LINE Move to the beginning of the line. REQ_END_LINE Move after the last char in the line. REQ_LEFT_CHAR Move left in the field. REQ_RIGHT_CHAR Move right in the field. REQ_UP_CHAR Move up in the field. REQ_DOWN_CHAR Move down in the field. REQ_NEW_LINE Insert/overlay a new line. REQ_INS_CHAR Insert the blank character at the cursor. REQ_INS_LINE Insert a blank line at the cursor. REQ_DEL_CHAR Delete the character at the cursor. REQ_DEL_PREV Delete the character before the cursor. REQ_DEL_LINE Delete the line at the cursor. REQ_DEL_WORD Delete the word at the cursor. REQ_CLR_EOL Clear to the end of the line. REQ_CLR_EOF Clear to the end of the field. REQ_CLR_FIELD Clear the entire field. REQ_OVL_MODE Enter overlay mode. REQ_INS_MODE Enter insert mode. REQ_SCR_FLINE Scroll the field forward a line. REQ_SCR_BLINE Scroll the field backward a line. REQ_SCR_FPAGE Scroll the field forward a page. REQ_SCR_BPAGE Scroll the field backward a page. REQ_SCR_FHPAGE Scroll the field forward half a page. REQ_SCR_BHPAGE Scroll the field backward half a page. REQ_SCR_FCHAR Horizontal scroll forward a character. REQ_SCR_BCHAR Horizontal scroll backward a character REQ_SCR_HFLINE Horizontal scroll forward a line. REQ_SCR_HBLINE Horizontal scroll backward a line. REQ_SCR_HFHALF Horizontal scroll forward half a line. REQ_SCR_HBHALF Horizontal scroll backward half a line. REQ_VALIDATION Validate field. REQ_PREV_CHOICE Display the previous field choice. REQ_NEXT_CHOICE Display the next field choice. RETURN VALUES
The form_driver() function returns one of the following: E_OK The function returned successfully. E_SYSTEM_ERROR System error. E_BAD_ARGUMENT An argument is incorrect. E_NOT_POSTED The form is not posted. E_INVALID_FIELD The field contents are invalid. E_BAD_STATE The routine was called from an initialization or termination function. E_REQUEST_DENIED The form driver request failed. E_UNKNOWN_COMMAND An unknown request was passed to the form driver. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |MT-Level |Unsafe | +-----------------------------+-----------------------------+ SEE ALSO
curses(3CURSES), forms(3CURSES), attributes(5) NOTES
The header <form.h> automatically includes the headers <eti.h> and <curses.h>. SunOS 5.11 31 Dec 1996 form_driver(3CURSES)
All times are GMT -4. The time now is 05:52 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy