【发布时间】:2018-01-02 17:40:35
【问题描述】:
假设我有 100 行的文件。文件中有很多行重复,只有一行不重复。
我想找到只显示一次的行。有没有一个命令或者我必须建立一些复杂的循环如下?
到目前为止我的代码:
#!/bin/bash
filename="repeat_lines.txt"
var="$(wc -l <$filename )"
echo "length:" $var
#cp ex4.txt ex4_copy.txt
for((index=0; index < var; index++));
do
one="$(head -n $index $filename | tail -1)"
counter=0
for((index2=0; index2 < var; index2++));
do
two="$(head -n $index2 $filename | tail -1)"
if [ "$one" == "$two" ]; then
counter=$((counter+1))
fi
done
echo $one"is "$counter" times in the text: "
done
【问题讨论】: