Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Grep certain lines with condition Post 302803549 by MadeInGermany on Tuesday 7th of May 2013 04:28:26 AM
Old 05-07-2013
Code:
awk -F, '!($1 in max) || $2>max[$1] {max[$1]=$2} END {for (i in max) print i FS max[i]}' input

Before reaching the END you don't know what the maximum is, hence cannot immediately print anything - a simple grep cannot work.

Last edited by MadeInGermany; 05-07-2013 at 05:55 AM..
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

grep command with AND condition

I want to do a grep with AND condition. I have three files. file1.txt ======== UNIX ...... WINDOWS ........ ORACLE file2.txt ======== UNIX ....... WINDOWS ...and many such files in a directory (6 Replies)
Discussion started by: prasperl
6 Replies

2. Homework & Coursework Questions

Grep line above X condition

1. The problem statement, all variables and given/known data: I have to grep a data file called datebook.txt. The last information in each line is a salary. I have to grep all the lines which precede those lines with 6 figure salaries. I can't SID it, or use Perl. It has to be grep (or egrep or... (3 Replies)
Discussion started by: DrSammyD
3 Replies

3. Shell Programming and Scripting

grep inside if condition - need help

hi i need help with below code. if ] then log "Exiting the script as ID= NULL" log "Please run script first." fi i am calling grep inside this but its not running any ideas why ?? input file is like this -- Msg 102, Level 20, State 1: Server... (4 Replies)
Discussion started by: dazdseg
4 Replies

4. Shell Programming and Scripting

use awk pick value from lines as condition for grep

Hi Folks! I have a file like this 000000006 dist:0.0 FILE ./MintRoute/MultiHopWMEWMA.nc LINE:305:1 NODE_KIND:131 nVARs:4 NUM_NODE:66 TBID:733 TEID:758 000000000 dist:0.0 FILE ./Route/MultiHopLEPSM.nc LINE:266:1 NODE_KIND:131 nVARs:4 NUM_NODE:66 TBID:601 TEID:626 000000001 ... (2 Replies)
Discussion started by: jackoverflow
2 Replies

5. Shell Programming and Scripting

Perl XML, find matching condition and grep lines and put the lines somewhere else

Hi, my xml files looks something like this <Instance Name="New York"> <Description></Description> <Instance Name="A"> <Description></Description> <PropertyValue Key="false" Name="Building A" /> </Instance> <Instance Name="B"> ... (4 Replies)
Discussion started by: tententen
4 Replies

6. Shell Programming and Scripting

grep in the if condition

Hi, In this code can able to match the pattern without case sensitive. Is that possible? if u knw plz help me... code: echo "Enter name to search" read n if ; echo "name found" else echo "Not Found" fi (8 Replies)
Discussion started by: boopal
8 Replies

7. Shell Programming and Scripting

Where condition in grep or awk?

Dear All, I need help.. I am having a csv file. Home_TITLE,People_TITLE,Repo_ALIAS HMN5530,RKY5807,/mine_repo/rike001 HMN5530,SRY6443,/mine_repo/rike001 HMN5530,ARDY001,/mine_repo/rike001 If i have two value in varible RKY5807, HMN5530. how can fetch and store another value... (6 Replies)
Discussion started by: yadavricky
6 Replies

8. UNIX for Dummies Questions & Answers

How to grep with certain condition?

hi all, i have an xml what i have to do is to search for the source id(s1) and if it matches with that in xml then extract the file mask from the name of the file i.e if the file name is idr_%YYYY%%MM%%DD%_%N%.idr then , i want the part after first % and before last % ie in this case ... (5 Replies)
Discussion started by: ramsavi
5 Replies

9. UNIX for Dummies Questions & Answers

Grep with condition

I have file input AAAA_XX_Start> rlong . 0W 130526-11:36:13 10.128.13.226 9.0j RBS_NODE_MODEL_N_1_50 stopfile=/tmp/13019 .. ================================================================================================================= MO ... (2 Replies)
Discussion started by: radius
2 Replies
Sort::Key::Top(3pm)					User Contributed Perl Documentation				       Sort::Key::Top(3pm)

NAME
Sort::Key::Top - select and sort top n elements SYNOPSIS
use Sort::Key::Top (nkeytop top); # select 5 first numbers by absolute value: @top = nkeytop { abs $_ } 5 => 1, 2, 7, 5, 5, 1, 78, 0, -2, -8, 2; # ==> @top = (1, 2, 1, 0, -2) # select 5 first numbers by absolute value and sort accordingly: @top = nkeytopsort { abs $_ } 5 => 1, 2, 7, 5, 5, 1, 78, 0, -2, -8, 2; # ==> @top = (0, 1, 1, 2, -2) # select 5 first words by lexicographic order: @a = qw(cat fish bird leon penguin horse rat elephant squirrel dog); @top = top 5 => @a; # ==> @top = qw(cat fish bird elephant dog); DESCRIPTION
The functions available from this module select the top n elements from a list using several common orderings and custom key extraction procedures. They are all variations around keytopsort { CALC_KEY($_) } $n => @data; In array context, this function calculates the ordering key for every element in @data using the expression inside the block. Then it selects and orders the $n elements with the lower keys when compared lexicographically. It is equivalent to the pure Perl expression: (sort { CALC_KEY($a) cmp CALC_KEY($b) } @data)[0 .. $n-1]; If $n is negative, the last $n elements from the bottom are selected: topsort 3 => qw(foo doom me bar doz hello); # ==> ('bar', 'doz', 'doom') topsort -3 => qw(foo doom me bar doz hello); # ==> ('foo', 'hello', 'me') top 3 => qw(foo doom me bar doz hello); # ==> ('doom', 'bar', 'doz') top -3 => qw(foo doom me bar doz hello); # ==> ('foo', 'me', 'hello') In scalar context, the value returned by the functions on this module is the cutoff value allowing to select nth element from the array. For instance: # n = 5; scalar(topsort 5 => @data) eq (sort @data)[4] # true # n = -5; scalar(topsort -5 => @data) eq (sort @data)[-5] # true Note that on scalar context, the "sort" variations (see below) are usually the right choice: scalar topsort 3 => qw(me foo doz doom me bar hello); # ==> 'doz' scalar top 3 => qw(me foo doz doom me bar hello); # ==> 'bar' Note also, that the index is 1-based (it starts at one instead of at zero). The "atpos" set of functions explained below do the same and are 0-based. Variations allow to: - use the own values as the ordering keys topsort 5 => qw(a b ab t uu g h aa aac); # ==> a aa aac ab b - return the selected values in the original order top 5 => qw(a b ab t uu g h aa aac); # ==> a b ab aa aac - use a different ordering For instance comparing the keys as numbers, using the locale configuration or in reverse order: rnkeytop { length $_ } 3 => qw(a ab aa aac b t uu g h); # ==> ab aa aac rnkeytopsort { length $_ } 3 => qw(a ab aa aac b t uu g h); # ==> aac ab aa A prefix is used to indicate the required ordering: (no prefix) lexicographical ascending order r lexicographical descending order l lexicographical ascending order obeying locale configuration r lexicographical descending order obeying locale configuration n numerical ascending order rn numerical descending order i numerical ascending order but converting the keys to integers first ri numerical descending order but converting the keys to integers first u numerical ascending order but converting the keys to unsigned integers first ru numerical descending order but converting the keys to unsigned integers first - select the head element from the list sorted nhead 6, 7, 3, 8, 9, 9; # ==> 3 nkeyhead { length $_ } qw(a ab aa aac b t uu uiyii) # ==> 'a' - select the tail element from the list sorted tail qw(a ab aa aac b t uu uiyii); # ==> 'uu' nkeytail { length $_ } qw(a ab aa aac b t uu uiyii) # ==> 'uiyii' - select the element at position n from the list sorted atpos 3, qw(a ab aa aac b t uu uiyii); # ==> 'ab'; rnkeyatpos { abs $_ } 2 => -0.3, 1.1, 4, 0.1, 0.9, -2; # ==> 1.1 rnkeyatpos { abs $_ } -2 => -0.3, 1.1, 4, 0.1, 0.9, -2; # ==> -0.3 Note that for the atpos set of functions indexes start at zero. - return a list composed by the elements with the first n ordered keys and then the remaining ones. ikeypart { length $_ } 3 => qw(a bbbb cc ddddd g fd); # ==> a cc g bbbb ddddd fd - return two arrays references, the first array containing the elements with the first n ordered keys and the second with the rest. keypartref { length $_ } 3 => qw(a bbbb cc ddddd g fd); # ==> [a cc g] [bbbb ddddd fd] The full list of available functions is: top ltop ntop itop utop rtop rltop rntop ritop rutop keytop lkeytop nkeytop ikeytop ukeytop rkeytop rlkeytop rnkeytop rikeytop rukeytop topsort ltopsort ntopsort itopsort utopsort rtopsort rltopsort rntopsort ritopsort rutopsort keytopsort lkeytopsort nkeytopsort ikeytopsort ukeytopsort rkeytopsort rlkeytopsort rnkeytopsort rikeytopsort rukeytopsort head lhead nhead ihead uhead rhead rlhead rnhead rihead ruhead keyhead lkeyhead nkeyhead ikeyhead ukeyhead rkeyhead rlkeyhead rnkeyhead rikeyhead rukeyhead tail ltail ntail itail utail rtail rltail rntail ritail rutail keytail lkeytail nkeytail ikeytail ukeytail rkeytail rlkeytail rnkeytail rikeytail rukeytail atpos latpos natpos iatpos uatpos ratpos rlatpos rnatpos riatpos ruatpos keyatpos lkeyatpos nkeyatpos ikeyatpos ukeyatpos rkeyatpos rlkeyatpos rnkeyatpos rikeyatpos rukeyatpos part lpart npart ipart upart rpart rlpart rnpart ripart rupart keypart lkeypart nkeypart ikeypart ukeypart rkeypart rlkeypart rnkeypart rikeypart rukeypart SEE ALSO
Sort::Key, "sort" in perlfunc. The Wikipedia article about selection algorithms <http://en.wikipedia.org/wiki/Selection_algorithm>. COPYRIGHT AND LICENSE
Copyright (C) 2006-2008, 2011 by Salvador Fandin~o (sfandino@yahoo.com). This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available. perl v5.14.2 2011-06-17 Sort::Key::Top(3pm)
All times are GMT -4. The time now is 09:48 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy