Don't you think its bit obvious? You are just generating random string once and storing it in ran
variable and using it for all the lines!
Using getline
into a variable from a pipe
awk '{
str_generator = "tr -dc '[:alnum:]' </dev/urandom | head -c 6"
str_generator | getline random_str
close(str_generator)
print "name " random_str " - " $0
}' file
When you use command | getline var
, the output of command is sent through a pipe to getline()
and into the variable var
.
Also note when a pipe is opened for output, awk
remembers the command associated with it, and subsequent writes to the command are appended to the previous writes. We need to make an explicit close()
call of the command to prevent that.
If the nested single-quotes in the str_generator
are causing a problem, replace with its octal equivalent(\047
)
awk '{
str_generator = "tr -dc \047[:alnum:]\047 </dev/urandom | head -c 6"
str_generator | getline random_str
close(str_generator)
print "name " random_str " - " $0
}' file
tr -dc '[:alnum:]' </dev/urandom | head -c 6
, it would simpler and more computationally efficient to usepwgen -s 6 1
, or better yetpwgen -s 6 $(wc -l myfile)
will give you exactly all the random strings you need, in one shot. – user1404316 Mar 07 '18 at 14:48