How to check if a field exceeds a certain number if this field has a $ sign in front of the front?

For a file with a name employee.txtin the format (Firstname, Lastname, Salary) with a space as a field separator:

Foo Bar $1,000
First Last $5,550
Abc Def $3,000
Qaru $6000
Help Please $4700

I want to print lines that have a third field over 4000 dollars. Desired conclusion:

First Last $5,550
Qaru $6000
Help Please $4700

I am not sure how to check if the field is greater than 4000, for example, with $and ,in it.

I tried to use sedto replace $and ,gaps, and then redirect to awk, but I want to output kept $and ,like the original.

Can this be done with awk?

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

How it works

  • v=$3

    v.

  • gsub(/[$,]/, "", v)

    v,

  • v+0>4000

    , v 4000.

4000 5000:

$ awk '{v=$3; gsub(/[$,]/, "", v)} v+0>4000 && v+0<5000' employee.txt 
Help Please $4700
+3

, bash ( awk ):

while IFS=' ' read -r first last cash; do
    valid_cash=${cash//[$,]/}
    (( valid_cash > 4000 )) && echo "$first $last $cash"
done < employee.txt
+1
awk 'gensub(/[$,]/,"","g",$NF) > 4000' input_file

(g) The default awk action is print.

0
source

Source: https://habr.com/ru/post/1691438/


All Articles