Given a file called employee.txt
in the format (Firstname, Lastname, Salary) with space as the field separator:
Foo Bar $1,000
First Last $5,550
Abc Def $3,000
Stack Overflow $6000
Help Please $4700
I want to print lines that have its third field greater than $4,000. Desired output:
First Last $5,550
Stack Overflow $6000
Help Please $4700
I am unsure how to check if a field is greater than 4000, for instance, with the $
and ,
in it.
I've tried using sed
to substitute $
and ,
with blanks then redirect to awk
but I want the output to contain $
and ,
like the original.
Is this doable with awk
?
Use the IF function, one of the logical functions, to return one value if a condition is true and another value if it's false. For example: =IF(A2>B2,"Over Budget","OK") =IF(A2=B2,B4-A4,"")
The “greater than or equal to” symbol (>=) is written in Excel by typing the “greater than” (>) sign followed by the “equal to” (=) operator. The operator “>=” is placed between two numbers or cell references to be compared. For example, type the formula as “=A1>=A2” in Excel.
We can use the IF Function to check if a cell has a value that is greater than the specified criteria. The IF function returns TRUE if the condition is met and FALSE if otherwise.
The IF function allows you to make a logical comparison between a value and what you expect by testing for a condition and returning a result if True or False. So an IF statement can have two results. The first result is if your comparison is True, the second if your comparison is False.
Select a blank cell which you will place the lookup result, type this formula =MAX(IF(A1:B6<E1,A1:B6)), and press Shift + Ctrl + Enter keys. In the formulas, A1:B6 is the range you want to find value, E1 is the value you look up.
IF OR AND formula in Excel To check various combinations of multiple conditions, you are free to combine the IF, AND, OR and other functions to run the required logical tests.
$ awk '{v=$3; gsub(/[$,]/, "", v)} v+0>4000' employee.txt
First Last $5,550
Stack Overflow $6000
Help Please $4700
v=$3
Save the third field as variable v
.
gsub(/[$,]/, "", v)
Remove the problematic characters from v
,
v+0>4000
Print lines for which v
is greater than 4000.
To print all lines with a value greater than 4000 and less than 5000:
$ awk '{v=$3; gsub(/[$,]/, "", v)} v+0>4000 && v+0<5000' employee.txt
Help Please $4700
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With