0
我試圖從文件中除去重複項。內容是數字和名字,名字可以是(例如重複的名字):ABC ABCxxyy ABC123 ABClmn等等...(所以在這裏我只想在我的文件中使用ABC)。爲了得到這個,我寫了下面的代碼。目前它使用文件讀/寫。我想要使用數組來更改此代碼,但無法計算。從bash數組中刪除重複項並保存到文件
下面是當前的代碼:
for h in `cat name.list`
do
count=`grep -c $h name.list`
if (($count >= 1))
then
echo $h >> name.list.new #building the new list
grep -v $h name.list > name.list.tmpcopy #rebuilding the name.list file...
mv name.list.tmpcopy name.list
fi
done
我試過,但我得到了相同的原始清單作爲輸出:
while read line
do
array+=("$line")
done < name.list
#loop thru the array:...
for ((i=0; i < ${#array[*]}; i++))
do
h=${array[i]}
match=$(echo "${array[@]:0}" | tr " " "\n" | grep -c $h)
if (($match >= 1))
then
# remove all matched names from array..... Longest match from front of string(s)
array=${array[@]##$h}
#save the current name to new array
array3[${#array3[*]}]=$h
fi
done
for ELEMENT in "${array3[@]}"
do
echo $ELEMENT
done > name.list.new
謝謝。完美的作品,你可以一步一步解釋嗎?這樣我可以在未來的解決方案中使用該學習? thx再次! – rajeev 2013-03-27 15:43:59
@rajeev查看更新的答案。 – 2013-03-27 17:52:14