Any 'shortcut' to doing this search for duplicate and print max


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Any 'shortcut' to doing this search for duplicate and print max
# 1  
Old 02-09-2016
Any 'shortcut' to doing this search for duplicate and print max

Hi,

I have a file that contains multiple records of the same database.
I need to search for the maximum size of the database. At the moment, I am doing as below:

Sample generated file to parse is as below. With the caret (^) delimiter, field 1 is the database name, 2 is the database ID and the field4 is the database size.

Code:
[oraclescripts]$ cat /tmp/x.txt
- [ test11 ] - TEST11^2609333750^ARCHIVELOG^157184688128
- [ test11 ] - TEST11^3637562595^ARCHIVELOG^163462512640
- [ qual11 ] - QUAL11^901361709^ARCHIVELOG^11422138368
- [ qual11 ] - QUAL11^4014910711^ARCHIVELOG^14071889920

Excerpt of the script that I use to parse /tmp/x.txt is as below:

Code:
 
[oraclescripts]$ cat /tmp/x.ksh
#!/bin/ksh
 awk '{ print $6 }' /tmp/x.txt | awk -F"^" '{ print $1 }' | sort | uniq > /tmp/x.00
 while read line
do
   grep "$line" /tmp/x.txt | sort -nt"^" -k 4 | tail -1
done < /tmp/x.00

Sample run of the script is as below:

Code:
[oraclescripts]$ /tmp/x.ksh
- [ qual11 ] - QUAL11^4014910711^ARCHIVELOG^14071889920
- [ test11 ] - TEST11^3637562595^ARCHIVELOG^163462512640

Then I've been informed that ideally, what constitute a unique database is a concatenation of field1+ field2.

So if the file to parse, /tmp/x1.txt, is as below. There is actually two databases named TEST11, each having a different database IDs and two databases named QUAL11.

Code:
- [ test11 ] - TEST11^2609333750^ARCHIVELOG^157184688128
- [ test11 ] - TEST11^2609333750^ARCHIVELOG^170184688128
- [ test11 ] - TEST11^3637562595^ARCHIVELOG^163462512640
- [ qual11 ] - QUAL11^901361709^ARCHIVELOG ^11422138368
- [ qual11 ] - QUAL11^4014910711^ARCHIVELOG^14071889920

So the script that I am using now then is the one below:

Code:
 
 $ cat /tmp/x1.ksh
#!/bin/ksh
 awk '{ print $6 }' /tmp/x1.txt | awk -F"^" '{ print $1"^"$2 }' | sort | uniq > /tmp/x1.00
 while read line
do
   grep "$line" /tmp/x1.txt | sort -nt"^" -k 4 | tail -1
done < /tmp/x1.00

Sample run of x1.ksh looks good.

Code:
$ /tmp/x1.ksh
- [ qual11 ] - QUAL11^4014910711^ARCHIVELOG^14071889920
- [ qual11 ] - QUAL11^901361709^ARCHIVELOG ^11422138368
- [ test11 ] - TEST11^2609333750^ARCHIVELOG^170184688128
- [ test11 ] - TEST11^3637562595^ARCHIVELOG^163462512640

At the moment, I am getting the desired output but just want to know if there is a shortcut way of doing it.

BTW, need to run the script in KSH in Solaris and Linux.

Thanks in advance.
# 2  
Old 02-09-2016
How about
Code:
sort -t^ -k2,2n -k4,4nr file | awk '!T[$2]++' FS=^
- [ qual11 ] - QUAL11^901361709^ARCHIVELOG ^11422138368
- [ test11 ] - TEST11^2609333750^ARCHIVELOG^170184688128
- [ test11 ] - TEST11^3637562595^ARCHIVELOG^163462512640
- [ qual11 ] - QUAL11^4014910711^ARCHIVELOG^14071889920

This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-26-2016
Quote:
Originally Posted by RudiC
How about
Code:
sort -t^ -k2,2n -k4,4nr file | awk '!T[$2]++' FS=^
- [ qual11 ] - QUAL11^901361709^ARCHIVELOG ^11422138368
- [ test11 ] - TEST11^2609333750^ARCHIVELOG^170184688128
- [ test11 ] - TEST11^3637562595^ARCHIVELOG^163462512640
- [ qual11 ] - QUAL11^4014910711^ARCHIVELOG^14071889920

Hi,

I tried your advise and it does work although I need to use nawk on the Solaris 8/9 one.

If you don't mind, may I know what the
Code:
awk '!T[$2]++' FS=^

means? To be more specific the
HTML Code:
'!T[$2]++'
part.

Thanks.
# 4  
Old 02-26-2016
You know awk works on pattern {action} pairs with the default action being {print}.
!T[$2]++ is a pattern (The logical value of empty or zero strings is FALSE, the logical negation will make it TRUE), which reads
if T[$2] is empty, print, increment T[$2] (so any further references will "fail" = print on first occurrence only).
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print a row with the max number in a column

Hello, I have this table: chr1_16857_17742 - chr1 17369 17436 "ENST00000619216.1"; "MIR6859-1"; - 67 chr1_16857_17742 - chr1 14404 29570 "ENST00000488147.1"; "WASH7P"; - 885 chr1_16857_18061 - chr1 ... (5 Replies)
Discussion started by: coppuca
5 Replies

2. Shell Programming and Scripting

Find Max value of line and print

I need to find the max value of all columns except the 1st column and print the answer along with the 1st column. Input 123xyz 0 0 1 2 0 0 0 0 0 0 0 234xyz 0 0 0 0 0 0 0 0 0 0 0 345xyz 0 0 1 0 0 0 ... (8 Replies)
Discussion started by: ncwxpanther
8 Replies

3. Shell Programming and Scripting

Print root number between min and max ranges

Hi to all, Please help on the following problem, I'm not where to begin, if awk or shell script. I have pairs of ranges of numbers and I need to find the root or roots of ranges based on min Range and Max ranges Example #1: If min range is 120000 and max ranges 124999, it means that are... (5 Replies)
Discussion started by: Ophiuchus
5 Replies

4. Shell Programming and Scripting

Search pattern on logfile and search for day/dates and skip duplicate lines if any

Hi, I've written a script to search for an Oracle ORA- error on a log file, print that line and the .trc file associated with it as well as the dateline of when I assumed the error occured. In most it is the first dateline previous to the error. Unfortunately, this is not a fool proof script.... (2 Replies)
Discussion started by: newbie_01
2 Replies

5. Shell Programming and Scripting

Sum value in a row and print the max

I have the input file in attached. I want the output file : Date , Time , Max_Bearer 11/01/2013 , 23:00 , 1447.894167 11/02/2013 , 00:00 , 1429.266667 11/03/2013 , 00:00 , 712.3175 11/04/2013 , 22:00 , 650.9533333 11/05/2013 , 23:00 , 665.9558333 11/06/2013 , 23:00 , 659.8616667... (2 Replies)
Discussion started by: justbow
2 Replies

6. Shell Programming and Scripting

Median and max of duplicate rows

Hi all, plz help me with this, I want to to extract the duplicate rows (column 1) in a file which at least repeat 4 times. then I want to summarize them by getting the max , mean, median and min. The file is sorted by column 1, all the repeated rows appear together. If number of elements is... (5 Replies)
Discussion started by: ritakadm
5 Replies

7. Shell Programming and Scripting

Print min and max value from two column

Dear All, I have data like this, input: 1254 10125 1254 10126 1254 10127 1254 10128 1254 10129 1255 10130 1255 10131 1255 10132 1255 10133 1256 10134 1256 10135 1256 10137... (3 Replies)
Discussion started by: aksin
3 Replies

8. Shell Programming and Scripting

print max number of 2 columns - awk

Is it possible to print max number of 2 columns - awk note: print max if the integer is positive and print min if the integer is negative input a 1 2 b 3 4 c 5 1 d -3 -5 d -5 -3 output a 2 b 4 c 5 d -5 d -5 (4 Replies)
Discussion started by: quincyjones
4 Replies

9. Shell Programming and Scripting

Search max value in a column in a file instead of sort

Hi Everyone, 1.txt 00:00:00 0 0 0 0 0 0 0 00:00:01 0 0 0 2 1 33 2 00:00:02 5 0 0 0 0 0 0 00:00:03 0 4 0 0 0 0 0... (5 Replies)
Discussion started by: jimmy_y
5 Replies

10. Shell Programming and Scripting

awk to print mon and max values of ranges

HI all I'm trying to write an awk script to print the min and max value in a range(s) contained in another file - the range values are in $2 EG 114,7964,1,y,y,n 114,7965,1,y,y,n 114,7966,1,y,y,n 114,7967,1,y,y,n 114,7969,1,y,y,n 114,7970,1,y,y,n 114,7971,1,y,y,n 114,7972,1,y,y,n... (3 Replies)
Discussion started by: Mudshark
3 Replies
Login or Register to Ask a Question