这是我在
中读到的文件Ford:Mondeo:1997:Blue:5
Vauxhall:Vectra:1999:White:2
Seat:Ibiza:1996:Red:4
Ford:Puma:2000:Black:1
Ford:Fiesta:1995:Red:2
Ford:Focus:2001:Black:0
Subaru:Impreza:1998:Green:1
Vauxhall:Corsa:1999:White:5
Honda:Accord:2004:Red:1
PEUGEOT:206:2000:Black:1
Ford:Fiesta:2000:Red:2
Rover:200:1993:Green:7
Ford:Mondeo:2001:Blue:0
Vauxhall:Cavalier:1995:White:2
Volkswagen:Beetle:2001:Blue:0
Ford:Escort:1995:Green:3
Skoda:Fabia:2006:Red:1
Vauxhall:Nova:1994:Black:8
Rover:Metro:1996:Burgundy:3
Renault:Megane:2006:Blue:4
Peugeot:406:1999:Silver:1
McLaren:F1:1997:Black:2
Skoda:Octavia:2003:Silver:2
Peugeot:206:2000:Red:1
使用awk我想重新格式化输出,所以我得到了这个
Car: Ford Mondeo, made in 1997, no previous owners.
Car: Ford Mondeo, made in 2000, 1 previous owner(s).
Car: Ford Mondeo, made in 2001, 2 previous owner(s).
这是我到目前为止的命令
awk -F: { printf "Car:%s %s, made in %s, no previous owners.", $1=="Ford" && $2=="Mondeo" && $3 < 2002 && $3 > 1996 } inputfile
2000年没有福特蒙迪欧在原始名单上制造。
答案 0 :(得分:1)
可能有更优雅的解决方案,但这对我有用。只需在最后添加文件路径即可。
awk -F ":" '{ printf "Car: "$1" "$2", made in " $3", "; if($5 == "0") {printf "no previous owners.\n"} else {printf $5" previous owner(s).\n"}}'
修改强> 我刚刚意识到你可能只是在寻找特别是福特蒙迪欧的汽车(你的问题并不完全清楚)。在这种情况下,这个awk命令应该工作。将先前的命令输出到grep可能会更容易理解。
awk -F ":" '{ if ($1 == "Ford" && $2 == "Mondeo") {printf "Car: "$1" "$2", made in " $3", "} else {next}; if($5 == "0") {printf "no previous owners.\n"} else {printf $5" previous owner(s).\n"}}'