我正在尝试从文件中清除重复项。内容是数字和名称,名称可以是(例如重复名称):ABC ABCxxyy ABC123 ABClmn 等...(所以这里我只希望 ABC 在我的文件中)。为此,我编写了以下代码。目前它使用文件读/写。我想使用数组更改此代码,但无法计算。
这是当前代码:
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