[SOLVED] How to compare negative and decimal numbers
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.
#!/bin/sh
a="-3.123"
b="4.2"
case $(echo "a=$a ;b=$b ;r=-1 ;if(a==b)r=0 ;if(a>b)r=1 ;r" | bc)
in
0) echo $a and $b are equal ;;
1) echo $a is bigger then $b ;;
*) echo $a is less then $b ;;
esac
Not to mention that you got the shell test wrong anyway. ">" is a string comparison operator. It compares the ascii values of the two sides. You need to use options like "-gt" when comparing integer values.
Don't forget to quote your variables too. Although it isn't a problem with integer inputs like this, it would break if you used a string with a space in it.
However, if you're using bash and portability isn't an issue, it's recommended to use ((..)) instead when working with numbers, and [[..]] when you have strings, filenames, or complex tests.
A file is having last field of each line as a number (eg. -3.213 or 2.1 or whatever else) and I want to compare this number with 4.2 which I am now able to compare using the above commands.
But Can I delete the line instantly (in place) from this file which is having number less than 4.2. How? Please suggest.
A file is having last field of each line as a number (eg. -3.213 or 2.1 or whatever else) and I want to compare this number with 4.2 which I am now able to compare using the above commands.
But Can I delete the line instantly (in place) from this file which is having number less than 4.2. How? Please suggest.
Too my knowledge you cannot do this directly (in place), but using a temporary file it will work:
Code:
$ cat infile
just a sample line -5
just a sample line 4.1
Just a sample line 4.2
just a sample line 4.3
$ awk '{ if ($NF < 4.2 ) next ; else print }' infile > tempfile
$ cat tempfile
just a sample line 4.2
just a sample line 4.3
$ mv tempfile infile
You're welcome! Just a little add-on. I remember an old post by radulov (the greatest awk master here at LQ) suggesting another method that probably works even with non-GNU awk implementation (I cannot test it right now). It was:
this one passes the content of infile to a subshell as standard input, so that the standard output can be safely redirected to the file itself. A bit more tricky, but maybe useful in some cases.
If last field is like below, then how to compare and delete line:
$ cat infile
just a sample line with number=-5
just a sample line with number=4.1
Just a sample line with number=4.2
just a sample line with number=4.3
When posting sample data for testing parsers that may be sensitive to formatting, it is a good idea to post the data in [CODE][/CODE] tags to preserve the formatting.
--- rod.
Is the above exactly how you did this? I'm asking because on my side it works as expected (I get a mail with separated lines). The output separators that awk uses aren't changed and lines should be put one below the other, not one after the other.
What is the output of the following (bold) commands:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.