LinuxQuestions.org
Help answer threads with 0 replies.
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-11-2005, 05:24 PM   #1
embsupafly
Member
 
Registered: Nov 2002
Location: ARIZONA
Distribution: Ubuntu
Posts: 44

Rep: Reputation: 15
Help: Perl or Bash Script help


Hi,

I know this can be done in either perl or bash, but I just don't have the knowledge on how:

We have a text file with multiple entries on each line like this:

American_Airlines
American_Airlines
American_Airlines
American_Airlines
American_Airlines
American_Eagle
American_Express
American_Express
American_Express_Premier
Arizona
Arizona_State
Arizona_State_University

But it continues on with thousands of entries. What we need to do is go throught the file with a script and if it finds a duplicate entry, it attaches an _2, _3, _4 and so forth so there are no duplicate entries but it still preserves each entry, just attaches a unique extention afterwards, so the sample entries above would now look like this:

American_Airlines
American_Airlines_2
American_Airlines_3
American_Airlines_4
American_Airlines_5
American_Eagle
American_Express
American_Express_2
American_Express_Premier
Arizona
Arizona_State
Arizona_State_University

Any ideas would be greatly appreciated! Thanks
 
Old 08-11-2005, 06:01 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,986
Blog Entries: 11

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
save the following as awk_rocks.awk
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
      }
    }
  }
}
The run the stuff below
Code:
chmod u+x awk_rocks.awk
./awk_rocks.awk list


Cheers,
Tink

Last edited by Tinkster; 08-11-2005 at 06:20 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
embedded bash in perl script paul_mat Linux - Software 3 11-21-2005 01:46 PM
Embedding perl in bash script MikHud Programming 4 02-02-2005 06:26 AM
Need help with perl/bash script to parse PicBasic file cmfarley19 Programming 13 11-18-2004 05:06 PM
writing a bash/ perl script on debian phr00ta Programming 4 01-29-2004 01:38 PM
How to run a bash command in the background from perl script professorfrink Programming 3 11-13-2003 03:02 PM


All times are GMT -5. The time now is 12:15 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration