Pages

Thursday, 10 May 2012

How To Remove Duplicate Text Lines in a file


Linux command uniq,sort

Use the following syntax:
uniq {file-name}




Here is a sample c file called hello.c:

#include < stdio.h>
void main()
{
        printf("hello\n");
        printf("hello\n");
}

When we execute the command uniq hello.c
the output is
#include< stdio.h>
void main()

{
         printf("hello\n");
}
This will have one line and then  remove the duplicate
To remove all the duplicates use -u option.
-u will remove all the duplicate entries

so uniq hello.c will result
#include< stdio.h>
void main()
{
    printf("hello\n");

}


The right method is sort the file first and then remove the duplicates.
The syntax is 
     sort {filename} | uniq -u
When we execute the command sort hello.c | uniq -u
the output is
{
}
#include< stdio.h>
void main()


In both cases the original file is not modified

No comments:

Post a Comment