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.
|
 |
08-21-2005, 02:26 PM
|
#1
|
Member
Registered: Nov 2002
Location: ARIZONA
Distribution: Ubuntu
Posts: 44
Rep:
|
SOLVED: AWK problem
*******************************************************
STATUS: SOLVED
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:
test
test
test
test
test
test
tested
testing
And running the awk script would create this:
test_1
test_2
test_3
test_4
test_5
test_6
tested
testing
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.
|
|
|
08-21-2005, 03:12 PM
|
#2
|
Senior Member
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524
Rep:
|
Quote:
Is there a way to do this with sed?
|
yes, because sed is turing complete.
see http://homepages.tesco.net/~J.deBoyn...o-answers.html
the following python (2.3) script will do it:
Code:
#!/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,
hth --Jonas
|
|
|
08-21-2005, 03:21 PM
|
#3
|
Member
Registered: Nov 2002
Location: ARIZONA
Distribution: Ubuntu
Posts: 44
Original Poster
Rep:
|
Jonas,
I appreciate your time and your generosity to help, I have never used python before but I gave your code snippet a go and I got the following error:
File "./python.py", line 15
print "%s_%d" % (line[:-1], i)
^
SyntaxError: invalid syntax
is it saying that print is invalid syntax, or is it something else on that line that is causing the problem?
P.S. I liked you link about asking questions that require a Yes/No answer, its great!
|
|
|
08-21-2005, 03:23 PM
|
#4
|
Member
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197
Rep:
|
Re: AWK problem
Quote:
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:
test
test
test
test
test
test
tested
testing
And running the awk script would create this:
test_1
test_2
test_3
test_4
test_5
test_6
tested
testing
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.
The following one-liner should do the job:
Code:
$ awk '{if(n[$1] == ""){n[$1]=1;} else {n[$1]+=1;} printf("%s_%d\n", $1, n[$1]);}' data.txt
|
|
|
08-21-2005, 03:33 PM
|
#5
|
Member
Registered: Nov 2002
Location: ARIZONA
Distribution: Ubuntu
Posts: 44
Original Poster
Rep:
|
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?
|
|
|
08-21-2005, 03:57 PM
|
#6
|
Member
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197
Rep:
|
Quote:
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:
Code:
$ awk 'BEGIN {while (("uniq -d data.txt" | getline s) > 0) {n[s]=1}} {if(n[$1] != ""){print $1"_"n[$1]++;} else {print $1}} ' data.txt
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?
|
|
|
08-21-2005, 04:02 PM
|
#7
|
Member
Registered: Nov 2002
Location: ARIZONA
Distribution: Ubuntu
Posts: 44
Original Poster
Rep:
|
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.
|
|
|
08-21-2005, 04:41 PM
|
#8
|
Senior Member
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524
Rep:
|
Quote:
Jonas,
I appreciate your time and your generosity to help, I have never used python before but I gave your code snippet a go and I got the following error:
Code:
File "./python.py", line 15
print "%s_%d" % (line[:-1], i)
^
SyntaxError: invalid syntax
is it saying that print is invalid syntax, or is it something else on that line that is causing the problem?
|
My best guess is that you probably screwed up some whitespace. Save a copy of http://jonaskoelker.homeunix.org/~jonas/numuniq.txt
Quote:
P.S. I liked you link about asking questions that require a Yes/No answer, its great!
|
Your welcome--I'm really glad you liked it 
I guess you'll like http://www.catb.org/~esr/faqs/smart-questions.html too.
hth --Jonas
Last edited by jonaskoelker; 08-21-2005 at 04:52 PM.
|
|
|
08-21-2005, 04:46 PM
|
#9
|
Member
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197
Rep:
|
Quote:
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.
|
Right, then this should work:
Code:
$ awk 'BEGIN {while (("sort data.txt | uniq -d" | getline s) > 0) {n[s]=1}} {if(n[$1] != ""){print $1"_"n[$1]++;} else {print $1}} ' data.txt
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?
|
|
|
08-21-2005, 05:53 PM
|
#10
|
Member
Registered: Nov 2002
Location: ARIZONA
Distribution: Ubuntu
Posts: 44
Original Poster
Rep:
|
Jonas and Carl,
Perfecto on both, I greatly appreciate both of your guy's assistance with this matter, any suggestions on how I can express my gratitude?
|
|
|
08-21-2005, 06:17 PM
|
#11
|
Senior Member
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524
Rep:
|
Quote:
How can I express my gratitude?
|
You can do what I do: edit the original post, adding a header saying that the problem is solved, where the solution is, and who helped you.
For an example, see http://www.linuxquestions.org/questi...hreadid=351315
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  .
--Jonas
|
|
|
08-21-2005, 06:45 PM
|
#12
|
Member
Registered: Nov 2002
Location: ARIZONA
Distribution: Ubuntu
Posts: 44
Original Poster
Rep:
|
Done, I edited the first post. It's a great Idea, I will start doing this everytime.
|
|
|
08-21-2005, 06:54 PM
|
#13
|
Senior Member
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524
Rep:
|
Quote:
It's a great idea, I will start doing this everytime.
|
Credits go to Harishankar--I learned it from him.
|
|
|
08-21-2005, 07:43 PM
|
#14
|
Member
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197
Rep:
|
Quote:
Originally posted by embsupafly
Jonas and Carl,
Perfecto on both, I greatly appreciate both of your guy's assistance with this matter, any suggestions on how I can express my gratitude?
|
I think you just did. 
|
|
|
All times are GMT -5. The time now is 09:51 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
|
|