2017-10-18 32 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) 

看到這個職位的詳細信息: