LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-22-2016, 05:44 AM   #1
timus221
LQ Newbie
 
Registered: May 2016
Posts: 7

Rep: Reputation: Disabled
Min and max


Hello I have problem.
Do You know how to find minimum and maximum in each line if i have :

9111 1320
6548 76
1649 567 5700 4596 4586 6428
8625
2714 1241 6907 5996 8139 6357 9922

8134 9727 3553
3945 8573
1273
8719 1366 1076 8420 6859
1637 2327 6554 7458
1399 4370 9612 5217
6911 2235
504
2770 396
4149 151 6900 2107 1354
3550 3324 8695 8543 2258 7039
2630 4006
726 1256 7255
2214 1866 1935
5592 193 7491 2053 6353
5731 916 981 8463 5749
9557 8166

2959 6813 4701
 
Old 05-22-2016, 05:49 AM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Hi!

Yes, I do. And there are several ways to do it, but what have you tried this far?

Best regards,
HMW
 
Old 05-22-2016, 06:00 AM   #3
timus221
LQ Newbie
 
Registered: May 2016
Posts: 7

Original Poster
Rep: Reputation: Disabled
For example :
cat numbers.txt|sort -n numbers.txt
cat numbers.txt|awk -F " " '{print $0}'|sort -n numbers.txt

but it doesn't work
 
Old 05-22-2016, 06:06 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Not sure why you would think that sorting would work? You will not eliminate any of the data not required.

Your original question asked for the min & max per line, whilst sorting in this way would work on a column. Please show your suggested output for your current input data?

The addition of the awk in your second script will yield no difference whatsoever as you are simply printing the whole line
 
Old 05-22-2016, 06:16 AM   #5
timus221
LQ Newbie
 
Registered: May 2016
Posts: 7

Original Poster
Rep: Reputation: Disabled
Yea I want min & max per line but i have no i idea how to do that,i am newbie yet.

For my current idea: cat numbers.txt|sort -n numbers.txt
the output is:
504
726 1256 7255
1273
1399 4370 9612 5217
1637 2327 6554 7458
1649 567 5700 4596 4586 6428
2214 1866 1935
2630 4006
2714 1241 6907 5996 8139 6357 9922
2770 396
2959 6813 4701
3550 3324 8695 8543 2258 7039
3945 8573
4149 151 6900 2107 1354
5592 193 7491 2053 6353
5731 916 981 8463 5749
6548 76
6911 2235
8134 9727 3553
8625
8719 1366 1076 8420 6859
9111 1320
9557 8166

but i want something like that or similar :
min1:1320 max1:9111
min2:76 max2:6549
....

Last edited by timus221; 05-22-2016 at 06:19 AM.
 
Old 05-22-2016, 06:23 AM   #6
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
I'm sure you can do this in bash, but for these kind of things I tend to use Python:
Code:
$ ./min_max.py 
Max 9111, Min 1320
Max 6548, Min 76
Max 6428, Min 567
Max 8625, Min 8625
Max 9922, Min 1241
Max 9727, Min 3553
Max 8573, Min 3945
Max 1273, Min 1273
Max 8719, Min 1076
Max 7458, Min 1637
Max 9612, Min 1399
Max 6911, Min 2235
Max 504, Min 504
Max 2770, Min 396
Max 6900, Min 151
Max 8695, Min 2258
Max 4006, Min 2630
Max 7255, Min 726
Max 2214, Min 1866
Max 7491, Min 193
Max 8463, Min 916
Max 9557, Min 8166
Max 6813, Min 2959
With Python, you can use min and max on a list.
See here for more info:
http://thelivingpearl.com/2013/10/24...ist-in-python/

Best regards,
HMW

PS. I am sure there is a killer oneliner in awk just waiting to be done by grail... DS.
PSS. My python script is just 8 lines long, so it's really quick and easy once you know what to do. DSS.

Last edited by HMW; 05-22-2016 at 06:25 AM.
 
Old 05-22-2016, 06:33 AM   #7
timus221
LQ Newbie
 
Registered: May 2016
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thank You,but i would prefer do this in bash,but unfortunately i can't,so please guys could You give me a solution or hint ?
 
Old 05-22-2016, 06:47 AM   #8
dunne
Member
 
Registered: May 2014
Distribution: OpenBSD
Posts: 67

Rep: Reputation: 36

A simple piece of awk should do the trick:
Code:
awk '
{
    for (i = 1; i <= NF; i++) {
	if (i == 1)
	    min = max = $i
	else if ($i < min)
	    min = $i
	else if ($i > max)
	    max = $i
    }
    print min, max
}'
 
Old 05-22-2016, 06:59 AM   #9
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,613

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
The bash would look something similar.
Loop through the file, for each line slurp the line and break it into a list words, find the smallest and largest words in the list, and write out your answer for that line.

Now, can you code?
 
1 members found this post helpful.
Old 05-22-2016, 08:51 AM   #10
timus221
LQ Newbie
 
Registered: May 2016
Posts: 7

Original Poster
Rep: Reputation: Disabled
I am not good in bash,I'm just started learning, so not specially
 
Old 05-22-2016, 09:03 AM   #11
dunne
Member
 
Registered: May 2014
Distribution: OpenBSD
Posts: 67

Rep: Reputation: 36
Quote:
Originally Posted by timus221 View Post
I am not good in bash,I'm just started learning, so not specially
A bash wrapper for the bit of awk above is trivial. Save it to a file, add
Code:
#!/bin/bash
as the first line, make the file executable, and you are good to go.
 
Old 05-22-2016, 09:07 AM   #12
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by timus221 View Post
I am not good in bash,I'm just started learning, so not specially
But here is a template:
Quote:
Originally Posted by wpeckham
Loop through the file, for each line slurp the line and break it into a list words, find the smallest and largest words in the list, and write out your answer for that line.
Also, that is the approach I took in my Python script. You can use the same approach with Bash.

First things first, do you know how to read a file, line by line in Bash? If not:
https://is.gd/A6RKTL

My suggestion is that you start there, and get back with your follow-up questions.

Best regards,
HMW
 
1 members found this post helpful.
Old 05-22-2016, 09:23 AM   #13
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
...actually, this turned out to be more trivial in Bash (7 lines including blank ones) than I first thought. A rough script produces this:
Code:
$ ./min_max.sh 
Min: 1320 Max: 9111
Min: 76 Max: 6548
Min: 567 Max: 6428
Min: 8625 Max: 8625
Min: 1241 Max: 9922
Min:  Max: 
Min: 3553 Max: 9727
Min: 3945 Max: 8573
Min: 1273 Max: 1273
Min: 1076 Max: 8719
Min: 1637 Max: 7458
Min: 1399 Max: 9612
Min: 2235 Max: 6911
Min: 504 Max: 504
Min: 396 Max: 2770
Min: 151 Max: 6900
Min: 2258 Max: 8695
Min: 2630 Max: 4006
Min: 726 Max: 7255
Min: 1866 Max: 2214
Min: 193 Max: 7491
Min: 916 Max: 8463
Min: 8166 Max: 9557
Min:  Max: 
Min: 2959 Max: 6813
Apart from the while loop (for reading the file) no looping or if/else is needed, just two pipelines.
Hint:
Code:
echo "99 20 333" | tr ' ' '\n' | sort -n | head -1
Best regards,
HMW

Last edited by HMW; 05-22-2016 at 09:28 AM.
 
Old 05-22-2016, 09:43 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I managed 2 awk scripts, bash and ruby. All have a fairly similar idea to the examples shown
 
Old 05-22-2016, 09:54 AM   #15
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Using perl's sort function
Code:
perl -ane 'print "Min: $a[0], Max: $a[-1]\n" if @a=sort {$a <=> $b}@F' data.txt
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
average max min bash bishop2001 Programming 2 01-28-2016 07:52 AM
Trying to get max and min for each entry in a table kmkocot Linux - Newbie 3 11-28-2015 07:43 PM
min-max-x buttons to left? danbuter Bodhi 6 02-15-2011 07:06 AM
No Max/Min/Close Button in Ubuntu 9.10 newbieinubuntu Ubuntu 2 12-31-2009 03:01 AM
Min/Max buttons? andykap Linux - General 3 12-15-2003 12:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration