Grep certain lines with condition


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep certain lines with condition
# 1  
Old 05-07-2013
Grep certain lines with condition

file input
Code:
aaaa,52C
aaaa,50C
bbbb,50C
bbbb,58C
aaaa,52C
bbbb,50C
aaaa,30C
bbbb,58C
cccc,60C

i want to print uniq lines with its max value of column2

expected output

Code:
aaaa,52C
bbbb,58C
cccc,60C

tks
# 2  
Old 05-07-2013
Try below
Code:
 
 sort -n -t, -k 2,2 filename|awk -F, '{A[$1]=$2}END{for (i in A){print i","A[i]}}'

# 3  
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..
# 4  
Old 05-07-2013
Radius,
Your specification is extremely vague. Is the 2nd field to be treated as
  1. a string (9C > 60C),
  2. a number followed by a character to be ignored (60C > 9C),
  3. a temperature in degrees (1C > 32F), or
  4. something else (???)?
If it is a temperature, are some values ever given in Fahrenheit or Kelvin? Does the sort need to convert Centigrade, Fahrenheit, and Kelvin to a common scale and sort by increasing temperatures independent of scale used? Or, will all temperatures be reported in Centigrade?

If it is something else, what?

So far, vidyadhar85's sort command assumes #2 and MadeInGermany's awk script assumes #1.
# 5  
Old 05-07-2013
Don Cragun's 4. item could be hexadecimal as well...
Yes the spec needs to be refined!
Assuming lexicographic/string sort, and avoiding the loss of order occurring when using for (i in Arr) - well, had the input file had any order, that is - , this might satisfy your request:
Code:
$ sort file | awk -F, 'NR>1 && TMP1 != $1 {print TMP0} {TMP0=$0; TMP1=$1} END {print TMP0}'
aaaa,52C
bbbb,58C
cccc,60C

 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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

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

7. 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

8. 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

9. 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
Login or Register to Ask a Question