Skip to content
Advertisement

How to get information from file with condition in Shell script without using “awk” [closed]

i have file

10 -     William English 80 100 404
10 abc   William Math    50 100 501
10 xyzsd William IT      60 100 550
10 -     William Sec     60 100 401
11 -     John    English 90 100 400
11 -     John    Math    75 100 404
11 ali   John    IT      85 100 550 
11 -     John    Sec     60 100 401

conditions : if the 2nd column is – then 2nd condition check last column value beginning in 4 then count the Name (William and John) Output :

3 John
2 William

Advertisement

Answer

Try:

awk -F ' ' '{if ($2 == "-" && $NF ~ /^4/) print $3}' <file_name> | uniq -c

Explanation: We are using if statement in awk to check the two conditions:

  • If 2nd element is equals to ‘-‘
  • If last element begins with 4

If above two conditions are true, we are printing name, stored in 3rd element

uniq -c will count occurrences of the names

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement