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