Search Duplicates, Print Line #


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search Duplicates, Print Line #
# 1  
Old 03-24-2010
Java Search Duplicates, Print Line #

Masters,

I have a text file in the following format.
Code:
  vrsonlviee	RVEBAALSKE
lyolzteglx	UUOSIWMDLR
pcybtapfee	DKGFJBHBJO
ozhrucfeau	YQXATYMGJD
cjwvjolrcv	YDHALRYQTG
mdukphspbc	CQZRIOWEUB
nbiqomzsgw	DYSUBQSSPZ
xovgvkneav	HJFQQYBLAF
boyyzdmzka	BVTVUDHSCR
vrsonlviee	TGTKUCUYMA
pcybtapfee	CQZRIOWEUB

I want to find duplicates in Col 2 and the get their line number.
I also want a solution to remove them using those line numbers.
The reason for choosing the line number is to make sure that I want to remove the line I chose from the duplicates, taking account of the variable in Col1.
Awk or sed egrep preferred.

Thanks
# 2  
Old 03-24-2010
You can find the duplicates with something like:
Code:
awk 'A[$2]++ { print NR }' file

You don't need the line number to remove them, when you can use:
Code:
awk '!A[$2]++' file

# 3  
Old 03-24-2010
Some one who knows AWK will provide a much better solution, but I can at least provide a solution.
Code:
# # get the list of duplicates in column 2
awk '{print $2}' file | sort | uniq -c | sort -n | awk '$1>1 {print $2}' > list_dups

# # for each duplicate in column 2 grep the entries from the file with line numbers
for x in $(< list_dups); do grep -n $x file;done

# # output
6:mdukphspbc    CQZRIOWEUB
11:pcybtapfee   CQZRIOWEUB

# # now remove the duplicate on line 6
sed '6d' file > file2

# # output after removing line 6
cat file2
  vrsonlviee    RVEBAALSKE
lyolzteglx      UUOSIWMDLR
pcybtapfee      DKGFJBHBJO
ozhrucfeau      YQXATYMGJD
cjwvjolrcv      YDHALRYQTG
nbiqomzsgw      DYSUBQSSPZ
xovgvkneav      HJFQQYBLAF
boyyzdmzka      BVTVUDHSCR
vrsonlviee      TGTKUCUYMA
pcybtapfee      CQZRIOWEUB

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

String search and print next all lines in one line until blank line

Dear all I want to search special string in file and then print next all line in one line until blank lines come. Help me plz for same. My input file and desire op file is as under. i/p file: A1/EXT "BSCABD1_21233G1" 757 130823 1157 RADIO X-CEIVER ADMINISTRATION BTS EXTERNAL FAULT ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

2. Shell Programming and Scripting

Search string and print the above line and below lines?.

if the first string matches then print the previous line and current line and also print the following lines if the other string search matches. Input ------ TranTime 2012 10 12 The Record starts here Accountnumber: 4632473431274 TxnCode 323 TranID 329473242834 ccsdkcnsdncskd... (7 Replies)
Discussion started by: laknar
7 Replies

3. Shell Programming and Scripting

search for a date and print the contents below the line

Hi, We have a script which takes the backup of some files and writes the output into a log file for each run on a daily basis. Following is the extract from the log file. Date:20120917 ********************************************************** * BACKUP ACTIVITY STARTED ... (5 Replies)
Discussion started by: svajhala
5 Replies

4. Shell Programming and Scripting

Search words in a line and print next 15 lines.

I have a text file ( basically a log file) and i have 2 words (alpha, beta), Now i want to search these two words in one line and then print next 15 lines in a temp file. there would be many lines with alpha and beta But I need only last occurrence with "alpha" and "beta" and next 15 lines. ... (4 Replies)
Discussion started by: kashif.live
4 Replies

5. Shell Programming and Scripting

Search from one file and print the next line

Hi, I have a file that contain more than a 1000 entries like this in one file P400000278 P400000446 P400000659 P400000789 I want to search in file that looks like this >P400000278 Adenosine 3'-phospho 5'-phosphosulfate transporter MVNPWKDYVKLSTVLMGSHGLTKGSLAFLNYPAQIMFKSAKVLPVMVMGAFVPGL... (5 Replies)
Discussion started by: Feeqa
5 Replies

6. Shell Programming and Scripting

Search in specific position and print the whole line

I have two files abc.dat and sant.dat (Big file 60k rows) for every line's 1,4 of abc.dat need to seach if this is present on 28,4 of sant.dat every line. if its present the output needs to go to bde.dat Example: contents abc.dat aaaa bbbb cccc dddd contents sant.dat this is... (4 Replies)
Discussion started by: ssantoshss
4 Replies

7. Shell Programming and Scripting

Search a string and print the rest of line

Hi Guys, I need to search a string and print the rest of the lines... input: 8 0 90 1 0 59 20 2488 96 30006dde372 S ? 0:00 /etc/opt/SUNWconn/atm/bin/atmsnmpd -n output: 00 /etc/opt/SUNWconn/atm/bin/atmsnmpd -n Actually i don even need the first "00".. any suggestions is appreciated..... (13 Replies)
Discussion started by: mac4rfree
13 Replies

8. Shell Programming and Scripting

awk search column, print line

Hello. I've been banging my head against walls trying to search a comma delimited file, using awk. I'm trying to search a "column" for a specific parameter, if it matches, then I'd like to print the whole line. I've read in multiple texts: awk -F, '{ if ($4 == "string") print $0 }'... (2 Replies)
Discussion started by: Matthias03
2 Replies

9. Shell Programming and Scripting

Print the line within the search pattern

Hi Guys, I had file as typedef struct { char TrailerType1; char TrailerTxt1; }Trailer; typedef struct { char PfigMoneyType; char PfigMoneyvalue; }PfigMoney; i need to print the lines within the search pattern. if i give the search pattern as... (3 Replies)
Discussion started by: manosubsulo
3 Replies

10. Shell Programming and Scripting

Search for text and print the next line

Hi, I want to write a small script to search for a text in file and when its found I want to print the next line. I try to write that script but I could not manage it, I just write the following script the find the exact line but I want the next line. $ sed -n -e '/Form not/p' test.txt... (2 Replies)
Discussion started by: alijassim
2 Replies
Login or Register to Ask a Question