LinuxQuestions.org
Help answer threads with 0 replies.
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 11-03-2012, 11:41 AM   #1
dotmil
LQ Newbie
 
Registered: Jan 2006
Location: Abilene, TX
Posts: 4

Rep: Reputation: 0
Scripting number evaluation


I've been trying to come up with a way to do this in a Bash script and haven't been able to get the expected results. I'm hoping someone much more experienced than myself has a pointer.

In a nutshell, I'm reading from a file with 2 space separated columns, column1 is user names, column2 is an integer of 1 to 6 digits long. What I need to do is evaluate the number and perform one action if it is greater than $NUM. The username must also be associated with the results.

In other words, if the value in column 2 is > $NUM, then output the complete line. If less than $NUM, just continue on to the next line. Any suggestions on how to do this in a sane and scaleable way if the list to be evaluated is quite long?
 
Old 11-03-2012, 12:16 PM   #2
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
If I'm understanding you correctly, your data looks something like this:

Code:
John_Doe 5
Jane_Smith 6
Peter_Jones 7
I'm not sure I'd use a bash script to handle a truly long file, there are probably other languages other than bash script language that might be faster, but if your data looks something like the above, why wouldn't a bash script like the following work?

Code:
if [  $#  -eq  1  ]
    then

        comparator="$1"

        while read user_name number
            do
                if [  $number  -gt  $comparator  ]
                    then
                        echo "$user_name $number"
                fi
            done

    else

        echo "Usage: ${0} number_for_comparison"
fi
 
Old 11-03-2012, 12:54 PM   #3
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
I would use awk:

Code:
x=6
awk -v num=$x '{ if ($2 > num) print}' test.txt
 
1 members found this post helpful.
Old 11-04-2012, 12:25 AM   #4
dotmil
LQ Newbie
 
Registered: Jan 2006
Location: Abilene, TX
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks guys. The awk solution fit best for me for now. Awesome :-)
 
Old 11-04-2012, 06:31 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Just to provide some more information on the awk solution, you can represent the data in 3 different ways using the same example:
Code:
# Original from H_TeXMeX_H
awk -v num=6 '{ if ($2 > num) print}' test.txt

# Using the if expression to give brace access
awk -v num=6 '$2 > num{print}' test.txt

# And lastly to use default action of a true evaluated expression to print
awk -v num=6 '$2 > num' test.txt
 
1 members found this post helpful.
  


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
Scripting, counting the number of readable, writable, and executable items Kratos22 Programming 9 11-29-2010 01:43 AM
[SOLVED] shell scripting: How can I increment a file by number dv502 Programming 11 08-27-2010 08:43 AM
scripting, number of subdirectories in a subdirectory disruptive Programming 2 06-04-2008 04:33 PM
bash scripting : printing variable with the number within $i amdGTintel Programming 2 01-18-2007 01:30 AM

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

All times are GMT -5. The time now is 12:32 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