Register
It is currently Mon Nov 24, 2014 9:06 am

Script read highest value


All times are UTC - 6 hours


Post new topic Reply to topic  [ 11 posts ] 
Author Message
 PostPosted: Sat Jun 09, 2012 5:24 pm   

Joined: Sat Jun 09, 2012 4:22 pm
Posts: 1
Hi All,

Can someone help steer me to the simplest approach to write a script that reads the second column of a csv file and returns the highest value.

example file:

DATE,QUANTITY
Mon Apr 30 01:56:00 2012,42
Mon Apr 30 02:24:00 2012,72
Mon Apr 30 02:56:00 2012,102
Mon Apr 30 02:59:00 2012,132
Mon Apr 30 03:31:00 2012,222
Mon Apr 30 03:38:00 2012,192

TIA


Top
 Profile  
 PostPosted: Sun Jun 10, 2012 4:24 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 568
hi,

without using any command, how, step by step, this should be done?


Top
 Profile  
 PostPosted: Tue Jun 26, 2012 2:54 am   

Joined: Mon Nov 30, 2009 4:06 am
Posts: 16
Hmm the above post is a bit useless isn't it.
I'm not experienced yet but in psuedo you have to :-
navigate to the file
read the first pertinent line
move to the second column after the comma
read that value
send it to where you can work on it -an array I suppose
read the next line's second column
loop through to the end
then read the array and choose the highest value
maybe get the rest of the line data too
send it where you want it

Perhaps in the script a variable might hold the full line and look at the value in a loop and update to the highest after each pass of the loop

A bit mixed up but you will probably get the idea .
I'll bet there are some dam fine solutions on here and I look forward to reading them


Top
 Profile  
 PostPosted: Tue Jun 26, 2012 4:05 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 568
no need of an array.
just check if the second column read is greater than the variable that stores greatest value until now.
Code:
while IFS=',' read first second rest
do test $second -gt ${store:-0} && store=$second
done < file.in
echo $store
I didn't put the header into file.in.


Top
 Profile  
 PostPosted: Tue Jun 26, 2012 4:27 pm   

Joined: Mon Nov 30, 2009 4:06 am
Posts: 16
Code:
#!/bin/bash
#readcolumn.sh
while IFS=',' read first second rest
do test $second -gt ${store:-0} && store=$second
echo $first $second
done < file.in
echo $store


Output of this was
# ./readcolumn.sh
./readcolumn.sh: line 5: test: -gt: unary operator expected

worker 10
hirer 15
wages 9
slaves 10
clowns 39
39
[/code]
File was
Code:
worker ,10
hirer , 15
wages , 9
slaves ,10
clowns ,39
(whoops) forgot blank last line


So I expect its looking at "worker" and saying its not "unary"


Last edited by tytower on Wed Jun 27, 2012 12:48 am, edited 1 time in total.

Top
 Profile  
 PostPosted: Wed Jun 27, 2012 12:25 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 568
:-/ add a set -x at the beginning of the script; that will show what's happening.


Top
 Profile  
 PostPosted: Wed Jun 27, 2012 12:52 am   

Joined: Mon Nov 30, 2009 4:06 am
Posts: 16
# ./readcolumn.sh
+ IFS=,
+ read first second rest
+ test -gt 0
./readcolumn.sh: line 7: test: -gt: unary operator expected
+ echo

+ echo store
store
+ IFS=,
+ read first second rest
+ test 10 -gt 0
+ store=10
+ echo worker 10
worker 10
+ echo store 10
store 10
+ IFS=,
+ read first second rest
+ test 15 -gt 10
+ store=' 15'
+ echo hirer 15
hirer 15
+ echo store 15
store 15
+ IFS=,
+ read first second rest
+ test 9 -gt 15
+ echo wages 9
wages 9
+ echo store 15
store 15
+ IFS=,
+ read first second rest
+ test 10 -gt 15
+ echo slaves 10
slaves 10
+ echo store 15
store 15
+ IFS=,
+ read first second rest
+ test 39 -gt 15
+ store=39
+ echo clowns 39
clowns 39
+ echo store 39
store 39
+ IFS=,
+ read first second rest
+ echo store 39
store 39
#


Top
 Profile  
 PostPosted: Wed Jun 27, 2012 1:31 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 568
Quote:
(whoops) forgot blank last line
that's it.
if your file might have empty line (which a database shouldn't have), check that line is not empty to process test on values.


Top
 Profile  
 PostPosted: Fri Jun 29, 2012 4:20 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Use the correct syntax and it will work as expected.

Code:
max=0; while IFS=, read -r _ num _; do ((num > max)) && max=$num; done < file; echo $max


(( ... )) will only return an error code of 1 if you're trying to process non-integers.


Top
 Profile  
 PostPosted: Sat Jun 30, 2012 12:21 am   

Joined: Mon Nov 30, 2009 4:06 am
Posts: 16
Elegant thanks
could you explain what "read -r _ num _" does?
I assume the "_" moves the columns
How does it know the delimiter to look for ?

EDIT-- IFS is a list of characters so IFS="," does that and _ jumps to the next delimiter


Top
 Profile  
 PostPosted: Fri Jul 06, 2012 9:35 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 568
_ is a "dummy variable" that could be any other valid name.
I don't use this character because _ is a special parameter (see man bash /Special Parameters)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP