ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.