LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 08-21-2005, 02:26 PM   #1
embsupafly
Member
 
Registered: Nov 2002
Location: ARIZONA
Distribution: Ubuntu
Posts: 44

Rep: Reputation: 15
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.
 
Old 08-21-2005, 03:12 PM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
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
 
Old 08-21-2005, 03:21 PM   #3
embsupafly
Member
 
Registered: Nov 2002
Location: ARIZONA
Distribution: Ubuntu
Posts: 44

Original Poster
Rep: Reputation: 15
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!
 
Old 08-21-2005, 03:23 PM   #4
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
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
 
Old 08-21-2005, 03:33 PM   #5
embsupafly
Member
 
Registered: Nov 2002
Location: ARIZONA
Distribution: Ubuntu
Posts: 44

Original Poster
Rep: Reputation: 15
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?
 
Old 08-21-2005, 03:57 PM   #6
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
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?
 
Old 08-21-2005, 04:02 PM   #7
embsupafly
Member
 
Registered: Nov 2002
Location: ARIZONA
Distribution: Ubuntu
Posts: 44

Original Poster
Rep: Reputation: 15
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.
 
Old 08-21-2005, 04:41 PM   #8
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
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.
 
Old 08-21-2005, 04:46 PM   #9
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
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?
 
Old 08-21-2005, 05:53 PM   #10
embsupafly
Member
 
Registered: Nov 2002
Location: ARIZONA
Distribution: Ubuntu
Posts: 44

Original Poster
Rep: Reputation: 15
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?
 
Old 08-21-2005, 06:17 PM   #11
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
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
 
Old 08-21-2005, 06:45 PM   #12
embsupafly
Member
 
Registered: Nov 2002
Location: ARIZONA
Distribution: Ubuntu
Posts: 44

Original Poster
Rep: Reputation: 15
Done, I edited the first post. It's a great Idea, I will start doing this everytime.
 
Old 08-21-2005, 06:54 PM   #13
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
Quote:
It's a great idea, I will start doing this everytime.
Credits go to Harishankar--I learned it from him.
 
Old 08-21-2005, 07:43 PM   #14
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
script problem while using awk and passing varibales ulto Programming 6 04-23-2004 11:58 AM
using awk wedgeworth Linux - Newbie 9 02-20-2004 07:48 AM
awk problem jpostma Programming 7 10-22-2003 06:07 AM
sed/awk problem player_2 Programming 9 08-26-2003 06:09 PM
awk problem alaios Programming 4 05-04-2003 09:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration