我是shell脚本的新手,希望使用shell脚本从表中获取特定的详细信息。
需求:-
输入文件:
model fuel type economy
swift petrol 18
swift diesel 25
i20 petrol 22
i20 diesel 15
alto petrol 20
alto diesel Null
输出文件:
model petrol diesel
swift 18 25
i20 22 15
alto 20 Null
我想在一个衬垫中使用正则表达式来实现这一点。需要帮忙。
你可以使用awk
:
$ awk 'NR>1{ # Skip first line
a[$1][$2]=$3 # Store file content into an array
}
END{ # After the file is parsed
OFS="\t"; # Set tabulation as output field separator
print "model","petrol","diesel";
for(i in a){ # Go through all array elements
print i,a[i]["petrol"],a[i]["diesel"] # Print content from the array
}
}' file
model petrol diesel
alto 20 Null
swift 18 25
i20 22 15