Print the first line and check the condition in the awk command… here is a solution to the problem.
Print the first line and check the condition in the awk command
The file content is
safwan@hello:~/linx$ cat 5.txt
Name age address email
safwan 25 India [email protected]
Ajmal 23 India [email protected]
Apply conditions
awk '$2==25 {print}' 5.txt
The output is
safwan@hello:~/linx$ awk '$2==25 {print}' 5.txt
safwan 25 India [email protected]
But I still need the first line
I want the output to be like this
Name age address email
safwan 25 India [email protected]
Solution
You can evaluate NR == 1
or your actual situation to get the header row that contains the data row:
awk 'NR == 1 || $2 == 25' 5.txt
Also, you don’t need to use {print}
because it’s the default action in awk
.