Current location - Quotes Website - Signature design - How to delete duplicate lines in perl script?
How to delete duplicate lines in perl script?

Assume your file is oss.txt,

The first step is to save the following script as script.pl, %hash?=?();

while(<>)?{?#?For each line of the input file

chomp();?#?Remove the end-of-line newline character

$_?=~?s /\\/\//g;?#?Replace \ with /. If you want to distinguish between slashes and backslashes, please remove this line of code

$hash{$_}=1; ?#?Mark the line that has appeared

}

my?@lines?=?keys(%hash);?#?Get all marked lines

$"?=?"\n";?#?The delimiter that defines the output is a newline character

print("@lines");?#?Output

The second step is to execute the following command on the command line. After the execution is completed, the result.txt will be a file without repeated lines.

perl?script.pl?oss.txt?>?result.txt

For example, the content listed in your question, the filtered result is

oss/src/platform/graph

oss/src/include

If slashes and backslashes are distinguished, the result is, oss\src\include

oss\src\platform\ graph

oss/src/platform/graph

oss/src/include