![](/img/trans.png)
[英]How to delete duplicated rows on one file based on a common field between two files with AWK?
[英]How to filter a file based on two separate field data by using awk?
输入文件由 3 个字段组成,以“|”分隔如下:
TeamId|TeamName|TotalPlayers
TeamId 由唯一编号组成。 TeamName 由多个英超联赛球队和 TotalPlayers 字段中相应的球员组成。
其中2条记录如下:(这些记录属于可见测试用例之一)
103|Manchester United|12
105|Manchester City|13
代码要求:
我必须 output 以曼彻斯特开头并且拥有最多球员的 TeamName。 如果没有球队从曼彻斯特开始,那么它不应该是任何 output。 即在上述测试案例中,output 应该是曼城。
我的解决方案:
awk 'BEGIN{FS = "|";OFS = ",";}{if($2 ~ /^Manchester/){print $2, $3}}' | sort -n -k2 | awk -F , '(NR==1){print $1}'
这为正常测试用例提供了预期的 output,但隐藏的测试用例失败了。 我可以对此或任何其他更简单的方法进行哪些更改以实现相同的...
还推荐任何我可以通过解决来练习这些 unix 编码问题的网站。
我必须 output 以曼彻斯特开头并且拥有最多球员的 TeamName。 如果没有球队从曼彻斯特开始,那么它不应该是任何 output。 即在上述测试案例中,output 应该是曼城。
$ cat file
TeamId|TeamName|TotalPlayers
103|Manchester United|12
105|Manchester City|13
$ awk -F'|' '$2~/^Manchester/ && $3 >max{max=$3; team=$2}END{if(team)print team}' file
Manchester City
您能否尝试使用 GNU awk
中的示例进行跟踪、编写和测试。
awk '
BEGIN{
FS="|"
}
FNR>1 && $2~/^Manchester/{
arr[$NF]=(arr[$NF]?arr[$NF] ORS:"")$2
max=(max>$NF?max:$NF)
}
END{
if(max!=""){
num=split(arr[max],val,ORS)
if(num>1){
for(i=1;i<=num;i++){
print val[i],max
}
}
else{ print arr[max],max }
}
}
' Input_file
说明:为上述添加详细说明。
awk ' ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section of program from here.
FS="|" ##Setting FS as | here.
}
FNR>1 && $2~/^Manchester/{ ##Checking condition if line number is more than 1 then do following.
arr[$NF]=(arr[$NF]?arr[$NF] ORS:"")$2 ##Creating array arr with index of last field and keep appending its value with new line in case similar max objects found to print them all.
max=(max>$NF?max:$NF) ##Creating max by checking if value of 2nd field if its greater than $2 then keep it else assign its value as $2.
}
END{ ##Starting END block of this program from here.
if(max!=""){ ##Checking condition if max is NOT NULL then do following.
num=split(arr[max],val,ORS) ##Splitting arr[max] value into val array with delimiter of ORS here.
if(num>1){ ##if num(total number of elements in arr) is greater than 1 then do following.
for(i=1;i<=num;i++){ ##Start a loop till value of num here.
print val[i],max ##Printing value of val with index i and max here.
}
}
else{ print arr[max],max } ##Else printing value of arr[max] and max only 1 time.
}
}
' Input_file ##Mentioning Input_file name here.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.