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

  • SEARCH
  • Home
  • 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

Related Questions

I'd like to remove the dbo prefix from the Object Explorer so I can
I don't necessarily want to use UUIDs since they are fairly long. The file
I have a file upload form on my website and would like to add
Is there a case where gensym's feature of appending unique numbers to the prefix
Html element contains complex & unique id, composed from namespace as prefix and incremented
I need a quick and easy way to store files with unique file names
There is an extensive collection of unique words in on the Haskell repository, cabal,
// // Prefix header for all source files of the 'Bar_scan' target in the
I am using language prefix in my app: Router::connect('/:language/:controller/:action/*', array(), array('language' => '[a-z]{3}')); When
How can I prefix bound values in TextBlock controls in a StackPanel without using

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.