本文介绍了如何生成使用find和sed的随机数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何生成此code随机数?
我尝试使用$ RANDOM,但数量仍然是相同的。如何提高呢?

 找到。型的F -exec的sed -i的/<字段名= \\测试/<字段名= \\试$ RANDOM / G'{} \\;


解决方案

这是因为,当您运行<$ c中的 $ RANDOM 函数调用与它的结果取代$ C>找到命令,而不是当找到运行 SED 命令。

您可以做的是把 SED -i ... 部分的脚本,并通过此脚本找到代替。

例如,如果 subst.sh 包含:

 #!/斌/庆典
R = $ RANDOM
SED -iS /&LT;字段名= \\测试/&LT;字段名= \\试$ {R} / G$ @

然后

 找到。型的F -exec ./subst.sh {} \\;

应该做的伎俩,因为 $ RANDOM 将被评估为有文件多次。

(注意,随机数还是将跨越一个特定的文件是相同的,但是这将是明显的为不同的文件。)

how to generate random number in this code?I try use $RANDOM, but the number is still the same. How to improve it?

find . -type f  -exec sed -i 's/<field name=\"test/<field name=\"test$RANDOM/g' {} \;
解决方案

That happens because the $RANDOM function invocation is substituted with its result when you run the find command, not when find runs the sed command.

What you can do is put the sed -i ... part in a script, and pass this script to find instead.

For instance, if subst.sh contains:

#!/bin/bash
r=$RANDOM
sed -i "s/<field name=\"test/<field name=\"test${r}/g" $@

Then

find . -type f -exec ./subst.sh {} \;

should do the trick, because $RANDOM is going to be evaluated as many times as there are files.

(Note that the random number will still be the same across one particular file, but it will be distinct for different files.)

这篇关于如何生成使用find和sed的随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 19:23
查看更多