awk with (if condition)

I'm unable to apply if condition to extract tsalary < 7000 and also want to sort data without disturbing header
some commands which i used here are:-

awk '{if(tsalary($4) < 7000)}' employee
tail -5 employee|awk '{print $4}' employee
tail -5 |sort -r employee

Name	tDesignation	tDepartment	tSalary
Thomas	Manager		Sales		        5000
Jason	Developer	       Technology	        5500
Sanjay	Sysadmin	       Technology	        7000
Nisha	Manager	        Marketing      	9500
Vicky	DBA		       Technology	         6000

Something like this?

[~/tmp] 0 $ awk  '$4 <= 7000 {print}' "file.txt" | grep -v tSalary  | sort -k 4

Thomas	Manager		Sales		5000
Jason	Developer	Technology	5500
Vicky	DBA		Technology	6000
Sanjay	Sysadmin	Technology	7000

Hope this helps

EDIT:
Had < > the wrong way

2 Likes

yes! this command is very useful but in some cases it also give header along with the extracted data
awk '$5 >= 6000' "employee" | grep -v tsalary | sort -k 5

500 Vicky DBA Technology 6000
300 Sanjay Sysadmin Technology 7000
400 Nisha Manager Marketing 9500
id Name tDesignation tDepartment tSalary

Dont do BOLD for code.
Do

CODE

for code.

And proper writing helps.... tSalary....
Or use grep -i ...

And btw...
Your code would not show the 'header' on occasion, but always...
Unless your headings differ from file to file...

2 Likes

Try this for leading header and sorted data:

awk 'NR == 1; $4 < 7000 {print | "sort -k4"}' file
Name	tDesignation	tDepartment	tSalary
Thomas	Manager	Sales	5000
Jason	Developer	Technology	5500
Vicky	DBA	Technology	6000

I slightly modified your code...
Was not my first attempt, just one of the... lets do this variant with the current variable... moments...

I have no idea WHY this works, because I wanted to EXclude the 'Name'-line with != and =! just to be sure:

awk '$1 == Name; $4 < 7000 {print | "sort -k4"}' file.txt
Thomas	Manager		Sales		5000
Jason	Developer	Technology	5500
Vicky	DBA		Technology	6000

EDIT:
Figured:

awk '$1 ==! "Name"; $4 < 7000 {print | "sort -k4"}' file.txt
Thomas	Manager		Sales		5000
Jason	Developer	Technology	5500
Vicky	DBA		Technology	6000

But I still dont understand why $1 == Name removes the Name/tSalary-line

2 Likes

Try this:

awk '$1 == "Name"; $4 < 7000 {print | "sort -k4"}' file
Name    tDesignation    tDepartment    tSalary
Thomas    Manager    Sales    5000
Jason    Developer    Technology    5500
Vicky    DBA    Technology    6000

Name unquoted is an uninitialized variable name defaulting to "". Any empty $1 in sight?

2 Likes

And that is why I dont understand why $1 == Name removes the "Name" line and shows the remaining values...
Because $1 is not empty -> it contains 'Name'... (and then the names...)

Cant wrap my head around that.
(can you 'follow' my confusion?)

And the TO did/does not want the header-line shown, that is why I settled for $1 ==! "Name"

It doesn't. It would remove any line with an emtpy $1, of which there is none. You can leave it out entirely. The $4 < 7000 removes the header line. $4 > 7000 would not; we'd need a different criterion, then.

Hmm - that ==! is a construct that I can't get my head around. It obviously is a correct syntax - no complaints by the interpreter. So the == must be separated from the ! which then logically negates the NAME variable's contents and results in a 1 or 0 which in turn is tried to be matched against $1 . And, so it is. Add a line starting with a 0 or 1 and see what's going to happen.

4 Likes