Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
10-27-2005, 08:26 AM
|
#1
|
Member
Registered: Sep 2003
Posts: 103
Rep:
|
how to find duplicate strings in vertical column of strings
Hello,
So I have a directory full of files. Sometimes I have the same file with sligthly different names:
rome.004101.recov10.T2_McAtNLO_top500._01397.AOD.pool.root
rome.004101.recov10.T2_McAtNLO_top500._01397.AOD.pool.root.6
So I need to remove one of them. Now if this had happened once or twice this is easy to do...but it happened 800 times. So I need somehting clever to do it for me. So far I can get the number (e.g. 1397 in the above example) of each file via:
[hodgkinson@atlasdata1 T2_McAtNLO_top500]$ ll -t | gawk '{print $NF}' | sed -e 's/._/._ /g' | sed -e 's/.AOD/ .AOD/g' | gawk '{print $(NF-1)}'
03998
03999
04000
03990
03991
01397
01397
etc
etc
So now I need to find any duplicated strings and then remove one of the files in this case. The second part I can do, but how to make the list of duplicated number strings I am not sure. In theory all I need to do is read the string on line one and then compare it to all the other strings in the column and if it matches dump the string to another file. I dont know how to do this comparison using bash though...does anyone else?
Thanks,
Mark
|
|
|
10-27-2005, 11:02 AM
|
#2
|
Member
Registered: Oct 2005
Location: Chicago, USA
Distribution: Slackware & Fedora
Posts: 66
Rep:
|
Use the 'sort' command with the -u flag for unique:
Code:
ll -t | gawk '{print $NF}' | sed -e 's/._/._ /g' | sed -e 's/.AOD/ .AOD/g' | gawk '{print $(NF-1)}' | sort -u
|
|
|
10-27-2005, 11:07 AM
|
#3
|
Member
Registered: Sep 2003
Posts: 103
Original Poster
Rep:
|
Quote:
Originally posted by naf
Use the 'sort' command with the -u flag for unique:
Code:
ll -t | gawk '{print $NF}' | sed -e 's/._/._ /g' | sed -e 's/.AOD/ .AOD/g' | gawk '{print $(NF-1)}' | sort -u
|
This tells me the list of numbers after removing any duplicates. I want it to tell me which numbers are duplicated though...I looked in the sort manual and dont see that it can give me this list. Can it?
Thanks,
Mark
|
|
|
10-27-2005, 11:48 AM
|
#4
|
Member
Registered: Oct 2005
Location: Chicago, USA
Distribution: Slackware & Fedora
Posts: 66
Rep:
|
Well, there is nothing that comes to mind. Perhaps you can create an awk script. Or expand your shell script.
Alternatively, you can create a mini program duplicates.c:
Code:
#include <stdio.h>
int main( int argc, char **argv )
{
const long max = 256;
char buffers[2][max];
char *active, *previous;
char *pointer;
buffers[0][0] = buffers[1][0] = '\0';
for( active = &(buffers[0][0]), previous = &(buffers[1][0]); fgets( active, max, stdin ); )
if( ! strcmp( active, previous ) )
fprintf( stdout, "%s", active );
else
pointer = active, active = previous, previous = pointer;
return 0;
}
then
Code:
gcc duplicates.c -o duplicates
Then use it as:
Code:
ll -t | gawk '{print $NF}' | sed -e 's/._/._ /g' | sed -e 's/.AOD/ .AOD/g' | gawk '{print $(NF-1)}' | sort | duplicates
|
|
|
10-27-2005, 02:56 PM
|
#5
|
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Rep: 
|
Try this:
1. Boil your raw "ls" down to the list you actually want to check for duplicates:
atlasdata1 T2_McAtNLO_top500]$ ll -t | gawk '{print $NF}' | sed -e 's/._/._ /g' | sed -e 's/.AOD/ .AOD/g' ... > list.txt
LIST.TXT == "03998
03999
04000
03990
03991
01397
01397
etc
etc
2. Now just compare the "sorted" list with the "sorted|uniq" list:
a) sort list.txt > 1
b) sort list.txt|uniq > 2
c) diff 1 2
2d1
< 01397
<= VOILA! "01397" IS A DUPLICATE!
'Hope that helps .. PSM
|
|
|
10-29-2005, 04:49 PM
|
#6
|
Member
Registered: May 2005
Posts: 378
Rep:
|
Duplicate post, sorry.
Last edited by eddiebaby1023; 10-29-2005 at 04:51 PM.
|
|
|
10-29-2005, 04:49 PM
|
#7
|
Member
Registered: May 2005
Posts: 378
Rep:
|
The problem is that markhod isn't trying to remove identical lines, merely ones that are identical up to a point. I'd use perl and save each line to compare it with the next (assuming the shortest line is the one to keep) and skip the ones that match. When you find one that doesn't match, output it and save that as the one to use for matching. I'll leave the implementation as an exercise for the reader. Ask again if you get really stuck and I'll engage my brain for you.
|
|
|
11-02-2005, 05:04 AM
|
#8
|
Member
Registered: Sep 2003
Posts: 103
Original Poster
Rep:
|
Quote:
Originally posted by paulsm4
Try this:
1. Boil your raw "ls" down to the list you actually want to check for duplicates:
atlasdata1 T2_McAtNLO_top500]$ ll -t | gawk '{print $NF}' | sed -e 's/._/._ /g' | sed -e 's/.AOD/ .AOD/g' ... > list.txt
LIST.TXT == "03998
03999
04000
03990
03991
01397
01397
etc
etc
2. Now just compare the "sorted" list with the "sorted|uniq" list:
a) sort list.txt > 1
b) sort list.txt|uniq > 2
c) diff 1 2
2d1
< 01397
<= VOILA! "01397" IS A DUPLICATE!
Thanks - that does the trick!
'Hope that helps .. PSM
|
|
|
|
All times are GMT -5. The time now is 07:01 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|