how to remove duplicate line in a text file in linux
- User
how to remove duplicate line in a text file in linux
- Assistant
To remove duplicate lines in a text file using Linux, you can use various methods. Here are a few approaches:
-
Using "sort" and "uniq" command: Open the terminal and run the following command:
sort -u inputFile.txt -o outputFile.txtThis command sorts the lines in the input file (inputFile.txt), removes duplicates, and saves the result in the output file (outputFile.txt).
-
Using "awk" command: Open the terminal and run the following command:
awk '!seen[$0]++' inputFile.txt > outputFile.txtThis command uses awk to process each line. It keeps track of lines it has seen already using the array seen[], and only prints lines that have not been encountered before.
-
Using "perl" command: Open the terminal and run the following command:
perl -ne 'print if ! $count{$_}++' inputFile.txt > outputFile.txtThis command utilizes Perl to read each line. It maintains a count of each line encountered in the %count hash and only prints lines that have a count of 1.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.