在此文本文件中从最早到最年轻的雇员输出的命令行?

时间:2019-02-06 04:03:38

标签: command-line

2233|charles harris  |g.m.     |sales     |12/12/52|90000   
9876|bill johnson    |director |production|03/12/50|130000  
5678|robert dylan    |d.g.m.   |marketing |04/19/43|85000  
2365|john woodcock   |director |personnel |05/11/47|120000  
5423|barry wood      |chairman |admin     |08/30/56|160000  
1006|gordon lightfoot|director |sales     |09/03/38|140000  
6213|michael lennon  |g.m.     |accounts  |06/05/62|105000  
1265|p.j. woodhouse  |manager  |sales     |09/12/63|90000  
4290|neil o'bryan    |executive|production|09/07/50|65000  
2476|jackie wodehouse|manager  |sales     |05/01/59|110000  
6521|derryk o'brien  |director |marketing |09/26/45|125000  
3212|bill wilcocks   |d.g.m.   |accounts  |12/12/55|85000  
3564|ronie trueman   |executive|personnel |07/06/47|75000  
2345|james wilcox    |g.m.     |marketing |03/12/45|110000  
0110|julie truman    |g.m.     |marketing |12/31/40|95000

请原谅我格式不正确,这是文本文件包含的我的问题是如何对所有关键字段5(即日期)进行排序,例如| 12/31/40 |。 MM / DD / YY以及其余日期从命令行的最旧到最年轻?分隔符是这个|

1 个答案:

答案 0 :(得分:0)

这是一个选择:

cat file.txt \
  | awk 'BEGIN {FS="|"}{ split($5,date,/\//); $5 = date[3] "/" date[2] "/" date[1]; print $1 "|" $2 "|" $3 "|" $4 "|" $5 "|" $6 }' \
  | sort -k5,5 -t"|" \
  | awk 'BEGIN {FS="|"}{ split($5,date,/\//); $5 = date[3] "/" date[2] "/" date[1]; print $1 "|" $2 "|" $3 "|" $4 "|" $5 "|" $6}'

说明:

  1. cat:整理文件
  2. awk:将日期格式固定为YY / MM / DD
  3. 排序:按固定日期列排序
  4. awk:恢复列日期格式