首页 > 解决方案 > 如何使用 linux Find 命令查找和打印文档中的重复单词?

问题描述

我想知道变量名是否重复。所以,我想找到重复的单词。另外,是否可以输出重复的字线?
我在互联网上找到了它,但它不起作用。

 # grep test.php | awk ‘{print $3}’ | sort | uniq -dc

例子。

$color2=$_POST['color2'] ?? '';
$color1=$_POST['color1'] ?? '';
$color3=$_POST['color3'] ?? '';
$color5=$_POST['color5'] ?? '';

$color6=$_POST['color6'] ?? '';
$color3=$_POST['color3'] ?? '';
$color8=$_POST['color8'] ?? '';
$color9=$_POST['color9'] ?? '';

$color13=$_POST['color13'] ?? '';
$color10=$_POST['color10'] ?? '';
$color11=$_POST['color11'] ?? '';
$color12=$_POST['color12'] ?? '';

$color13=$_POST['color13'] ?? '';

标签: linuxtextfindcode-duplication

解决方案


使用以下代码创建脚本findDup.sh :

for n in $(seq 1 13)
do
        no_of_lines=$(grep -n color$n= test.php|wc -l)
        if  [ $no_of_lines -gt 1 ]
        then
                grep -n color$n= test.php
                echo "--------"
        fi
done

当您在包含您的test.php的目录中运行它时,您将获得带有行号的重复行。

例子:

$ ./findDup.sh
3:$color3=$_POST['color3'] ?? '';
6:$color3=$_POST['color3'] ?? '';
--------
9:$color13=$_POST['color13'] ?? '';
13:$color13=$_POST['color13'] ?? '';
--------

您可以在上面的脚本中将限制从 13 更改为您想要的任何内容。


推荐阅读