Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a field is greater than a certain number if that field has a $ sign in front?

Tags:

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?

like image 775
5areductase Avatar asked Dec 28 '17 00:12

5areductase


People also ask

How do I do an IF THEN formula in Excel?

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,"")

How do you do a greater than or equal to sign in Excel?

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.

How do you check if a value is greater than another value 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.

Can IF statement have 2 conditions in Excel?

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.

How do you find values greater than less than a specific value in Excel?

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.

Can you use ifs and and together in Excel?

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.


1 Answers

$ awk '{v=$3; gsub(/[$,]/, "", v)} v+0>4000' employee.txt 
First Last $5,550
Stack Overflow $6000
Help Please $4700

How it works

  1. v=$3

    Save the third field as variable v.

  2. gsub(/[$,]/, "", v)

    Remove the problematic characters from v,

  3. v+0>4000

    Print lines for which v is greater than 4000.

Printing a range of values

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
like image 155
John1024 Avatar answered Sep 23 '22 12:09

John1024