对于bash中的每一行,将n个字符的字符串替换为n个字符的随机字符串
我有一个名为tmp.txt的文件。它的字符“a-z”在制表符分隔的文件的一侧由空格分隔,另一侧由数字“0-9”分隔。对于每一行,我想随机替换1-3个字符。我的tmp.txt如下所示:对于bash中的每一行,将n个字符的字符串替换为n个字符的随机字符串,bash,random,replace,Bash,Random,Replace,我有一个名为tmp.txt的文件。它的字符“a-z”在制表符分隔的文件的一侧由空格分隔,另一侧由数字“0-9”分隔。对于每一行,我想随机替换1-3个字符。我的tmp.txt如下所示: s h e h a d y o u r 0.9472 0.2074 0.4878 0.2227 0.4998 0.2841 0.5323 0.4254 0.539 0.4981 d o n t a s k m e t o c a r r y 0.9741 0.0999 0.338 0.0572 0.4514 0
s h e h a d y o u r 0.9472 0.2074 0.4878 0.2227 0.4998 0.2841 0.5323 0.4254 0.539 0.4981
d o n t a s k m e t o c a r r y 0.9741 0.0999 0.338 0.0572 0.4514 0.223 0.5036 0.3835 0.4844 0.6306
e v e n t h e n 0.8549 0.1265 0.5248 0.2713 0.622 0.2011 0.4334 0.4137 0.4788 0.5435
到目前为止,我已经写了这么多剧本:
cat tmp.txt | while IFS= read -r line;
do
for i in {1..3};
do
a=$(tr -dc 'a-z0-9' | head -c $i);
b=$(head /dev/urandom | tr -dc 'a-z0-9' | head -c $i);
sed -i 's/$a/$b/g';
done;
done
sed似乎没有找到$line,因为我得到:
sed: no input files
sed: no input files
sed: no input files
我以为我在这个循环中仍然在read-r行内,但似乎我错了。有人知道我做错了什么吗
提前感谢不要为此使用
sed
。尤其是,不要对每行输入分别运行三次:命令替换速度慢,外部命令调用速度更慢
#!/usr/bin/env bash
case $BASH_VERSION in ''|[0-3].*|4.0*) echo "ERROR: Bash 4.1+ required" >&2; exit 1;; esac
# Open a continuous stream of acceptable random characters
exec {random_chars_fd}< <(tr -dc 'a-z0-9' </dev/urandom)
while IFS= read -r line; do
for ((i=0; i<3; i++)); do
# filter for alpha and numeric characters in our input line
possible_chars=${line//[![:alnum:]]}
# pick a random position in the filtered string, and take the character it contains
char_to_replace=${possible_chars:$(( RANDOM % ${#possible_chars} )):1}
# now, read one character from our stream of random inputs
read -n 1 replacement <&$random_chars_fd
# and replace all instances of the randomly-selected character in our input with the
# randomly-selected output character.
line=${line//"$char_to_replace"/"$replacement"}
done
printf '%s\n' "$line" # print our new version of the line
done <tmp.txt
#/usr/bin/env bash
“|[0-3].|4.0*)echo中的$BASH_版本错误:BASH 4.1+必需”>&2;出口1;;以撒
#打开可接受的随机字符的连续流
exec{random_chars_fd}<根本不使用sed
。尤其是,不要对每行输入分别运行三次:命令替换很慢,外部命令调用更慢
#!/usr/bin/env bash
case $BASH_VERSION in ''|[0-3].*|4.0*) echo "ERROR: Bash 4.1+ required" >&2; exit 1;; esac
# Open a continuous stream of acceptable random characters
exec {random_chars_fd}< <(tr -dc 'a-z0-9' </dev/urandom)
while IFS= read -r line; do
for ((i=0; i<3; i++)); do
# filter for alpha and numeric characters in our input line
possible_chars=${line//[![:alnum:]]}
# pick a random position in the filtered string, and take the character it contains
char_to_replace=${possible_chars:$(( RANDOM % ${#possible_chars} )):1}
# now, read one character from our stream of random inputs
read -n 1 replacement <&$random_chars_fd
# and replace all instances of the randomly-selected character in our input with the
# randomly-selected output character.
line=${line//"$char_to_replace"/"$replacement"}
done
printf '%s\n' "$line" # print our new version of the line
done <tmp.txt
#/usr/bin/env bash
“|[0-3].|4.0*)echo中的$BASH_版本错误:BASH 4.1+必需”>&2;出口1;;以撒
#打开可接受的随机字符的连续流
exec{random\u chars\u fd}<您使用的是哪个版本的sed?请参阅:您还没有为sed
提供任何输入文件,如何为sed提供任何选项。@Mihir如果您想将line
输入到sed
,并根据其输出重新分配变量,line=$(sed-e“s/$a/$b/g”您使用的是哪个版本的sed?请参阅:您还没有向sed
提供任何输入文件,如何向sed提供任何选项。@Mihir如果要将line
输入到sed
,并根据其输出重新分配变量,line=$(sed-e“s/$a/$b/g”我得到一个语法错误:第5行出现意外重定向:exec{random\u chars\u fd}sh somescript
,或者使用#!/bin/sh
shebang?那不是bash。顺便说一句,捕捉这种情况正是我在行中使用case$bash\u版本开始回答这个问题的原因。我遇到了一个语法错误:redirecti在第5行上:exec{random_chars_fd}sh somescript
,或者使用#!/bin/sh
shebang?那不是bash.BTW,捕捉这种情况正是我在行中用case$bash\u版本开始回答这个问题的原因。