Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 442619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:05:23+00:00 2026-05-12T21:05:23+00:00

To prefix unique words with UNIQUE: inside a file I’ve tried to use a

  • 0

To prefix unique words with “UNIQUE:” inside a file I’ve tried to use a perl regex command like:

perl -e 'undef $/;while($_=<>){s/^(((?!\b\3\b).)*)\b(\w+)\b(((?!\b\3\b).)*)$/\1UNIQUE:\3\4/gs;print $_;}' demo

On a demo file containing:

watermelon banana
apple pear pineapple orange mango
strawberry cherry
kiwi pineapple lemon cranberry watermelon
orange plum cherry
kiwi banana plum
mango cranberry apple
lemon

The output is:

watermelon banana
apple pear pineapple orange mango
strawberry cherry
kiwi pineapple lemon cranberry watermelon
orange plum cherry
kiwi banana plum
mango cranberry apple
UNIQUE:lemon

Unfortunately, the \3 backreference doesn’t seem to be handled if used in advance.

Is there another way to achieve this with another regex or with other usual commands available on a Linux box? (grep, sed, awk,…)

Many thanks

EDIT:
Unfortunately, many of the solutions works for the provided case only which was incomplete, my apologies for that, it should also work on a text like:

{watermelon || banana}
apple = ( pear pineapple orange mango )
strawberry cherry
kiwi = pineapple = lemon = cranberry = watermelon
orange - plum = cherry
kiwi = banana + plum
mango = cranberry && apple
lemon

If it simplifies the problem, words may be prefixed with something like $ or @.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T21:05:23+00:00Added an answer on May 12, 2026 at 9:05 pm

    I see you are already using Perl. When you want to count something using a hash is always a nice approach…

    #!/usr/bin/perl -w
    use strict;
    
    my %hash = ();
    my $str;
    
    while(<>) {
        $str .= $_;
        $_ =~ s/\W+/ /g;
        map {$hash{$_}++} split ' ', $_;
    }
    
    for (keys %hash){
        my $word = $_;
        if($hash{$word}==1) {
            $str =~ s/\($word)/UNIQUE:$word/g;
        }
    };
    
    print "$str\n";
    

    which will output:

    {watermelon || banana}
    apple = ( UNIQUE:pear pineapple orange mango )
    UNIQUE:strawberry cherry
    kiwi = pineapple = lemon = cranberry = watermelon
    orange - plum = cherry
    kiwi = banana + plum
    mango = cranberry && apple
    lemon
    

    Using an regexp is probably going to be hard. You need to run through the entire file twice. One pass to count all occurrences of words and one pass to mark-up the unique words.

    The above snippet read the input once, but keeps the entire original text in $str – obviously a bad idea if the input was large.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 251k
  • Answers 251k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In the below code, I'm using a single function that… May 13, 2026 at 9:28 am
  • Editorial Team
    Editorial Team added an answer You can make the query more specific step by step… May 13, 2026 at 9:28 am
  • Editorial Team
    Editorial Team added an answer I would consider using NHibernate, you can hook it up… May 13, 2026 at 9:28 am

Related Questions

To prefix unique words with UNIQUE: inside a file I've tried to use a
Html element contains complex & unique id, composed from namespace as prefix and incremented
I'm loading some HTML via Ajax with this format: <div id=div1> ... some content
I don't know if this is the place to ask about algorithms. But let's
I am trying to add a spatial index to a table column named Location

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.