2017-10-18 64 views
2

即时通讯开始在Linux中的shell脚本编程,我需要帮助解决以下问题:与shell脚本数据管理

我需要从file.txt的以下信息读取

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 

与我需要的信息:

  • 专业的两个最老的专业人士。
  • 两位专业人员平均时间较短,反应时间较短
  • 年龄和性别较老的专业人员。

试着用下面的代码,但它不工作:

#!/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 

我怎么能解决这个问题?

回答

1

您不需要读取这些行,然后担心提取字段。您可以直接读取场成独立的变量,因为你有一个分隔符的文件:

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) 

看到这个职位的详细信息: