ProgrammingThis 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.
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.
I solved this with the help of jonaskoelker, he suggested using Python as a method of reaching a solution, see post #4 (using python) .
Another member, carl.waldbieser, had an equally effective solution using AWK, see post #6.
I personally liked the AWK solution better as I have never used Python, but both solutions do work, and it was kind of nice being exposed to a new language (Python). Thanks guys, If you want a free year of web hosting (LAMP, CPanel w/ Fantastico) let me know, I owe you both!
I have a flat text file with multiple entries , some of these entries are duplicates (some are duplicated once, some are duplicated up to 10-15 times)so I want to rename them by incrementing them with _$i at the end of each duplicate file name so the original file might have this:
I have an awk script listed below that does this, however after running it, it resorts the entire file in a different order than the file was prior to running the script, is there a reason for this and how do we stop it from resorting the entire file contents?
Here is the awk script:
Code:
#!/usr/bin/awk -f
{
n[$1]++
}
END{
for (item in n){
for (i=1; i <= n[item]; i++){
if(i==1){
print item
} else {
print item"_"i
}
}
}
}
Is there a way to do this with sed?
Last edited by embsupafly; 08-21-2005 at 06:30 PM.
#!/usr/bin/env python2.3
from sets import Set as set
from sys import stdin
lines = stdin.readlines()
count = {}
for line in lines: count[line] = 1 + count.get(line, 0)
duplicates = set()
for line in count:
if count[line] > 1:
duplicates.add(line)
number = {}
for line in lines:
if line in duplicates:
i = number[line] = 1 + number.get(line, 0)
print "%s_%d" % (line[:-1], i)
else:
print line,
Originally posted by embsupafly I have a flat text file with multiple entries , some of these entries are duplicates (some are duplicated once, some are duplicated up to 10-15 times)so I want to rename them by incrementing them with _$i at the end of each duplicate file name so the original file might have this:
I have an awk script listed below that does this, however after running it, it resorts the entire file in a different order than the file was prior to running the script, is there a reason for this and how do we stop it from resorting the entire file contents?
Here is the awk script:
Code:
#!/usr/bin/awk -f
{
n[$1]++
}
END{
for (item in n){
for (i=1; i <= n[item]; i++){
if(i==1){
print item
} else {
print item"_"i
}
}
}
}
Is there a way to do this with sed?
Thank you so much!
Your awk script is just working a little harder than it has to! The reason it re-sorts the file is because you first read the entire contents into memory, then you write the output. However, when you iterate over n, the order of the items in the associative array is indeteminate-- awk can basically store the items in whatever way it sees fit.
Carl, that AWK script puts an _1 at the end of each listing even if it is a unique line, is there a way to only append the numbering if it is a duplicate line?
If I use the Thank You button, does that donate to Linux Questions or to the Person I am trying to thank? Do they accept PayPal?
Originally posted by embsupafly Again, a thanks to Jonas and Carl.
Carl, that AWK script puts an _1 at the end of each listing even if it is a unique line, is there a way to only append the numbering if it is a duplicate line?
If I use the Thank You button, does that donate to Linux Questions or to the Person I am trying to thank? Do they accept PayPal?
My bad, I overlooked that detail. Trickier, but awk is still up to the task:
Here, I pull out all the duplicate lines in the file (I assume it is sorted-- if not, the command would have to be "sort data.txt | uniq -d" | getline ...). These are stored in your dup array with a starting value of 1.
Then as I iterate through the actual file, I check if the value was in the array. If it is, I add the trailing stuff and increase the count. Does that make sense?
The order of the file entries are not sorted in any fashion other than the order they are entered in the file, but they need to stay in the same order.
Originally posted by embsupafly The order of the file entries are not sorted in any fashion other than the order they are entered in the file, but they need to stay in the same order.
The output will stay in the same order. The part that reads
Code:
"sort data.txt | uniq -d" | getline s
actually reads from the same file, sorts the *output*, and passes the results to the [uniq -d] command which expects a sorted list to figure out the duplicates. These duplicate lines are stored in the awk associative array, n[].
The actual file is read in its *original* order, and processed that way. So in essence, this awk script reads the file twice, but outputs results only once, in the original order.
Is that more clear?
Of course, you're welcome to send me huge amounts of money and a digital camera/webcam with decente free (libre) drivers, write a free(libre) space/sci-fi RTS game, and go visit my website .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.