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.
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.
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'
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.