Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
Extract a word between brackets and replace it in a sentence

so let's say i have this sentence

It's {raining|snowing|cold} outside

What I want is to randomly extract a word between the brackets, which i did with awk -vRS="}" -vFS="|" '{print $2}' (still working to extract them randomly). The output usually is the second word, in our case snowing.

Thing is that the output is only snowing, and the actual output i want is something like It's snowing outside, so how do I extract any word from the brackets and replace with only one word.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Since you need to rewrite a string by finding and replacing a part of it, regex is suitable

use warnings;
use strict;
use feature 'say';

sub pick_one { 
    my ($pattern) = @_; 
    my @choices = split /\|/, $pattern; 
    return $choices[int rand @choices]; 
}

my $sentence = q(It's {raining|snowing|cold} outside); 

$sentence =~ s/\{ ( [^}]+ ) \}/pick_one($1)/ex; 

say $sentence; 

That /e modifier makes it evaluate the replacement side as code, and the produced value is then used as the replacement. So there we run a sub in which the choosing happens. Having this in a sub is a good way for later refinements/changes, implemented in the sub.

An element of the array @choices is selected using rand. An expression for its upper bound is evaluated in the scalar context so we can directly use the @choices array since then its length ends up being used.

over 4 years ago · Santiago Trujillo Report

0

echo "It's {raining|snowing|cold} outside" |\
  perl -pe 's/\{(.*?)\}/(split("[|]",$1)[rand(3)]/e'

or for arbitrary number of weather conditions:

echo "It's {raining|snowing|cold} outside" |\
  perl -pe 's/\{(.*?)\}/@a=split("[|]",$1); $a[rand(@a)]/e'
over 4 years ago · Santiago Trujillo Report

0

Using any awk in any shell on every Unix box:

$ cat tst.awk
BEGIN { srand() }
match($0,/\{[^}]+}/) {
    wordList = substr($0,RSTART+1,RLENGTH-2)
    numWords = split(wordList,words,/[|]/)
    wordNr = int(rand() * numWords + 1)
    $0 = substr($0,1,RSTART-1) words[wordNr] substr($0,RSTART+RLENGTH)
}
{ print }

$ awk -f tst.awk file
It's snowing outside

$ awk -f tst.awk file
It's cold outside

See rand() (and srand()) at https://www.gnu.org/software/gawk/manual/gawk.html#Numeric-Functions for details on it's randomness.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!