[英]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.