2

Im starting with shell script programming in linux and i need help to solve the following problem:

I need to read from a file.txt the following information

lastName,Name |age | gender | antiquity | profession | response time
Homes,Louis 34 male 12 leader 4
House,Jonathan 26 male 4 designer 7
Smith,Peter 36 male 10 architect 8  
Prat,Zoe 40 female 14 programmer 2
Evans,Bethany 30 female 8 programmer 12

with the information I need:

  • Profession of the two oldest professionals.
  • Average time of the two professionals who have less response time
  • Age and gender of the older professional.

Tried with the following code, but it does not work:

#!/bin/bash
while read line
    do
    antigüedad=$(echo $line|cut -d" " -f4)
    if [[$antiquity -gt $greaterAge]]
        then
            greaterAge=$antiquity
            moreOld=$line
    fi
done < data.txt

How could I solve this?

codeforester
  • 39,467
  • 16
  • 112
  • 140
Franco Torres
  • 253
  • 1
  • 6

1 Answers1

1

You don't need to read the lines and then worry about extracting the fields. You can directly read the fields into separate variables since you have a delimited file:

while read -r name age gender antiquity profession response_time; do
  # your logic here
  # you need a space after `[[` and before `]]` in `[[ ... ]]` condition
done < <(sed 1d file.txt)

See this post for more details:

codeforester
  • 39,467
  • 16
  • 112
  • 140