Question Detail
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.
Question Answer
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.
An interesting solution is to (ab)use Perl’s glob
function to generate a list of possible combinations. The caveat is that you cannot use glob meta characters in the string. For example:
use strict;
use warnings;
use Data::Dumper;
use feature 'say';
my $str = "It's {freaking|fudging} {raining|snowing|cold} outside";
$str =~ s#({[^}]+})#$1 =~ s/\|/,/gr#ge; # replace | with , inside curly braces
my @list = glob $str;
print Dumper \@list;
say "Our random string: " . $list[ int rand(@list) ];
Output:
$VAR1 = [
'It\'s freaking raining outside',
'It\'s freaking snowing outside',
'It\'s freaking cold outside',
'It\'s fudging raining outside',
'It\'s fudging snowing outside',
'It\'s fudging cold outside'
];
Our random string: It's fudging cold outside
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'
With jq it is a one-liner:
echo "It's {raining|snowing|cold} outside" | \
jq -rR --argjson rand $RANDOM 'gsub("{(?<words>[^}]+)}"; .words | split("|") | .[$rand % length])'
echo "It's {raining|snowing|cold} outside"|awk -F"[{|}]" -v w="$(shuf -i 2-4 -n 1)" '{print $1$w$NF}'
In plain bash
, without using any external command:
#!/bin/bash
while read -r line; do
if [[ $line =~ (.*)\{([^}]*)}(.*) ]]; then
IFS='|' read -ra words <<< "${BASH_REMATCH[2]}"
line=${BASH_REMATCH[1]}${words[RANDOM%${#words[*]}]}${BASH_REMATCH[3]}
fi
printf '%s\n' "$line"
done < file