根据两列之间的差异对文件进行排序

根据两列之间的差异对文件进行排序

我有一个名为 Artists.dat 的列表。我想根据艺术家去世时的年龄对他们进行排序。然后为每个打印以下内容。

“名字”“姓氏”活了“$4-$3”年。

例如,Dorotha Tanning 活了 102 岁。 (按$4-$3排序)

Dorothea,  Tanning, 1910, 2012
Frida,     Kahlo, 1907, 1954
Gertrude,  Abercrombie, 1909, 1977
Leonora,   Carrington, 1917, 2011
Max,       Ernst, 1891, 1976
Remedios,  Varo, 1908, 1963
Rene,      Magritte, 1898, 1967
Salvador,  Dali, 1904, 1989
Yves,      Tanguy, 1900, 1955

答案1

如果您有最新版本的 GNUawk

gawk -F', *' '
  {a[$1" "$2]=$4-$3}
  END {
    PROCINFO["sorted_in"]="@val_num_desc"
    for (i in a) print i, "lived for", a[i], "years"
  }
' Artists.dat

答案2

虽然 Steeldriver 的解决方案更加优雅,但我也提供了这个。

awk -F"," '{print $1, $2, "lived for "$4-$3" years."}' Artists.dat|tr -s " "| sort -nrk5

答案3

这也是 python 的一个成果——

/artists.py
Dorothea Tanning lived for 102 years
Leonora Carrington lived for 94 years
Salvador Dali lived for 85 years
Max Ernst lived for 85 years
Rene Magritte lived for 69 years
Gertrude Abercrombie lived for 68 years
Remedios Varo lived for 55 years
Yves Tanguy lived for 55 years
Frida Kahlo lived for 47 years


cat artists.py 
#!/usr/bin/python
d = {}
for line in open("artists.dat", "r"):
  parts = line.strip().split(",")
  name = parts[0].strip() + " " + parts[1].strip()
  age_died = int(parts[3]) - int(parts[2])    
  d[name] = age_died

for i in sorted(d, key=d.get, reverse=True):
  print("%s lived for %d years" %(i, d[i]))

相关内容