Maximum of the values from the output a query


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Maximum of the values from the output a query
# 1  
Old 01-30-2015
Computer Maximum of the values from the output a query

Following is the output of a command ran inside the script:
Code:
2015-01-29-05-38-02 5 2015-01-29-05-38-02 5 2015-01-29-05-38-02 5 2015-01-29-05-38-02 5 2015-01-29-05-38-02 5 2015-01-29-05-38-03 5 2015-01-29-05-38-03 5 2015-01-29-05-38-03 5 2015-01-29-05-38-03 5 2015-01-29-05-38-03 5 2015-01-29-05-38-03 5 2015-01-29-05-38-03 4 2015-01-29-05-38-03 3 2015-01-29-05-38-03 2 2015-01-29-05-38-03 2 2015-01-29-05-38-04 1

first is Timestamp and second is some X,
How can I get the maximum of X without using tmp files and then awking.

I am using bash shell on linux machine.

Last edited by Franklin52; 01-30-2015 at 07:34 AM.. Reason: Please use code tags
# 2  
Old 01-30-2015
Can you add Code tags?

Do you want max of X for each timestamp or for entire input?
This User Gave Thanks to anbu23 For This Post:
# 3  
Old 01-30-2015
hey anbu23,

I need max(X) irrespective of Timestamp.

Thank you.
# 4  
Old 01-30-2015
Can you please add code tags to your input data
# 5  
Old 01-30-2015
Hello Devendra,

Could you please try following and let me know if this helps you.
Code:
awk '{for(i=1;i<=NF;i++){if(i%2==0){A[j]=A[j]>$i?A[j]:$i} if(i%2!=0){j=$i}}} END{for(u in A){print u OFS A[u]}}'  Input_file

Output will be as follows.
Code:
2015-01-29-05-38-04 1
2015-01-29-05-38-02 5
2015-01-29-05-38-03 5

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 01-30-2015
Try also
Code:
awk '!(NR%2) {if ($1 > MAX) MAX=$1} END {print MAX}' RS=" " file
5

This User Gave Thanks to RudiC For This Post:
# 7  
Old 01-30-2015
Hey Ravinder,

your code didn't give the expected result,
Expected output is : only "5"
what it gave is:
Code:
2015-01-29-05-38-02 5
2015-01-29-05-38-03 5
2015-01-29-05-37-50 5
2015-01-29-05-38-31 0
2015-01-29-05-38-04 1
2015-01-29-05-37-51 5
2015-01-29-05-37-52 5
2015-01-29-05-37-53 5
2015-01-29-05-37-54 5
2015-01-29-05-37-55 5
2015-01-29-05-37-56 5
2015-01-29-05-37-57 5
2015-01-29-05-37-48 5
2015-01-29-05-37-58 5
2015-01-29-05-37-49 5
2015-01-29-05-37-59 5
2015-01-29-05-38-00 5
2015-01-29-05-38-01 5

---------- Post updated at 04:18 PM ---------- Previous update was at 04:14 PM ----------

RudiC,

Your code gave me empty string.
I think RS dint work.

Thank you.

---------- Post updated at 04:34 PM ---------- Previous update was at 04:18 PM ----------

Hello all,

I have tried with the following which worked for me.
Code:
awk 'BEGIN {max = 0} {if ($2>max) max=$2} END { print max }' file

thank you all
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting values after the maximum value in a txt file

Hello, I'm new to scripting and I need to write a bash script. Here is example of file on which I'm working: 0.3092381 0.3262799 0.3425480 0.3578379 0.3719490 0.3846908 0.3958855 0.4053738 0.4130160 0.4186991 0.4223357 0.4238688 ... (14 Replies)
Discussion started by: jeo_fb
14 Replies

2. UNIX for Beginners Questions & Answers

To extract values after the maximum value in a txt file

Hello, I'm new to scripting and I need to write a bash script. Here is example of file on which I'm working: 0.3092381 0.3262799 0.3425480 0.3578379 0.3719490 0.3846908 0.3958855 0.4053738 0.4130160 0.4186991 0.4223357 ... (1 Reply)
Discussion started by: jeo_fb
1 Replies

3. Shell Programming and Scripting

Find minimum and maximum values based on column with associative array

Hello, I need to find out the minimum and maximum values based on specific column, and then print out the entire row with the max value. Infile.txt: scf6 290173 290416 . + X_047241 T_00113118-1 scf6 290491 290957 . + X_047241 T_00113118-2 scf6 290898 290957 . + X_047241 T_00113119-3 scf6... (2 Replies)
Discussion started by: yifangt
2 Replies

4. Shell Programming and Scripting

Output minimum and maximum values for replicates ID

Hi All I hope that someone could help me! I have an input file like this, with 4 colum(ID, feature1, start, end): a x 1 5 b x 3 10 b x 4 9 b x 5 16 c x 5 9 c x 4 8 And my output file should be like this: a x 1 5 b x 3 16 c x 4 9 What I would like to do is to output for each ID... (2 Replies)
Discussion started by: giuliangiuseppe
2 Replies

5. Shell Programming and Scripting

Query the table and return values to shell script and search result values from another files.

Hi, I need a shell script, which would search the result values from another files. 1)execute " select column1 from table_name" query on the table. 2)Based on the result, need to be grep from .wft files. could please explain about this.Below is the way i am using. #!/bin/sh... (4 Replies)
Discussion started by: Rami Reddy
4 Replies

6. Shell Programming and Scripting

Print minimum and maximum values using awk

Can I print the minimum and maximum values of values in first 4 columns ? input 3038669 3038743 3037800 3038400 m101c 3218627 3218709 3217600 3219800 m290 ............. output 3037800 3038743 m101c 3217600 3219800 m290 (2 Replies)
Discussion started by: quincyjones
2 Replies

7. Shell Programming and Scripting

AWK, Perl or Shell? Unique strings and their maximum values from 3 column data file

I have a file containing data like so: 2012-01-02 GREEN 4 2012-01-02 GREEN 6 2012-01-02 GREEN 7 2012-01-02 BLUE 4 2012-01-02 BLUE 3 2012-01-02 GREEN 4 2012-01-02 RED 4 2012-01-02 RED 8 2012-01-02 GREEN 4 2012-01-02 YELLOW 5 2012-01-02 YELLOW 2 I can't always predict what the... (4 Replies)
Discussion started by: rich@ardz
4 Replies

8. Shell Programming and Scripting

Query Oracle tables and return values to shell script that calls the query

Hi, I have a requirement as below which needs to be done viz UNIX shell script (1) I have to connect to an Oracle database (2) Exexute "SELECT field_status from table 1" query on one of the tables. (3) Based on the result that I get from point (2), I have to update another table in the... (6 Replies)
Discussion started by: balaeswari
6 Replies

9. Shell Programming and Scripting

add the output of a query to a variable to be used in another query

I would like to use the result of a query in another query. How do I redirect/add the output to another variable? $result = odbc_exec($connect, $query); while ($row = odbc_fetch_array($result)) { echo $row,"\n"; } odbc_close($connect); ?> This will output hostnames: host1... (0 Replies)
Discussion started by: hazno
0 Replies

10. UNIX for Dummies Questions & Answers

Searching partial columns and returning maximum as output

Hello, I am just getting starting with awk and wondering if anyone could help with the following problem. I have a large file of data, 50,000 rows x 6 columns. I would like to search in blocks of 500 rows for a maximum value in a specific column and compile an output file that prints the... (0 Replies)
Discussion started by: xb_analysis
0 Replies
Login or Register to Ask a Question