LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-06-2009, 07:48 AM   #1
tengblad
LQ Newbie
 
Registered: Apr 2009
Posts: 1

Rep: Reputation: 0
Bash: Insert output of a commando into an array and compare the values


Hello all,
I'm still sort of a beginner at bash scripting and need help with the following.

I have a set of commands that will output a list of values. The exact values, and number thereof, is not set and can and will vary each time I run the command.

What I want to do is to insert the output of this commando into an array, and them compare how many instances of each digit is present. If one digit appears more times then rest I want to print text to standard output.

An example.

cat ./hba.txt | grep '.:.:.:.' | awk -F: '{print $1}' | cut -c4-5
returns the following list:
Quote:
0
0
2
2
0
0
[..]
3
3
1
1
3
3
1
1


What I want to do is count how many times 0 appears, how many times 1 appears and how many times 2 and so on. I then want to compare these values and if one of these numbers appear more than the rest, I want to echo something.

With sort | uniq -c I added on to the end of the first line I can see that 0 appears twelve times, 1 eight times, 2 twelve times and 3 eight times:
Quote:
12 0
8 1
12 2
8 3
So, what I really need help with is finding a way to compare them!

Thanks in advance.
 
Old 04-06-2009, 11:28 PM   #2
grunt547
Member
 
Registered: Nov 2006
Distribution: FreeBSD, OpenBSD
Posts: 30

Rep: Reputation: Disabled
Well, you could pipe it through another sort:
Code:
sort | uniq -c | sort -rg
Then you'll get out a list in descending order by numerical value. If you wanted to limit your results, you could then pipe it through "head" or something.
 
Old 04-07-2009, 03:36 PM   #3
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

in summary: You want to write some text for the value with the highest number of occurences?

First I'd use one sed to prepare the file instead of 4 commands in a pipeline:
Code:
sed -n '/.:.:.:./{s/:.*//;s/^...\(..?\).*/\1/;p}' hba.txt
To get the maximum of occurences at the first line: The first sort in grunt547's command is not necessary. To add a comment on this line you can again use sed:
Code:
sed ... | uniq -c | sort -rg | sed '1s/$/ this is the maximum/'
Jan
 
  


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
Store multi-line output into an array in a Linux bash script steven.c.banks Linux - General 5 12-05-2010 02:08 PM
Compare Decimal Values rbautch Linux - General 10 04-22-2008 04:11 AM
How do I compare two values in separate files xmrkite Linux - Software 11 09-19-2007 11:36 PM
Problem displaying Bash array values. shinobi59 Programming 3 01-17-2006 05:45 PM
bash: put output from `ls` into an array samel_tvom Programming 18 07-31-2005 12:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:09 PM.

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