假设我有 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
Run Code Online (Sandbox Code Playgroud)