[英]How do I replace the header value in a file with a string and a randomly generated number?
I Have files in a directory that look like: 我有一个目录中的文件,如下所示:
aaa bbbbb cccc value1=xxxxxvalue2=yyyy
body body body
body body body
body body body
aaa bbbbb cccc value1=xxxx
How do I replace everything between "value1=" and "value2=" in the header with a string (sevens.eighteen. below) and a randomly generated number of length 20 (11234567890123456789 below)? 如何使用字符串(sevens.eighteen。下面)和随机生成的长度为20的数字(下面的11234567890123456789)替换标题中“value1 =”和“value2 =”之间的所有内容?
ex: aaa bbbbb cccc value1=sevens.eighteen.11234567890123456789=yyyy 例如:aaa bbbbb cccc value1 = sevens.eighteen.11234567890123456789 = yyyy
I have tried variations of the below, but I am not getting what I want. 我尝试了以下的变体,但我没有得到我想要的东西。
sed -i '1 s/^.*value=/yoursubstitution value=/; $ s/^.*value=/yoursubstitution value=/'
Here are results I want to see in all of the files in that directory: 以下是我希望在该目录中的所有文件中看到的结果:
aaa bbbbb cccc value1=sevens.eighteen.11234567890123456789value2=yyyy
body body body
body body body
body body body
aaa bbbbb cccc value1=xxxx
With one file. 有一个文件。
# create a 20 digit random number with bash
for ((i=0;i<20;i++)); do num="${num}${RANDOM: -1:1}"; done
# replace string with GNU sed
sed -E '1 s/(value1=).*(value2=.*)/\1sevens.eighteen.'"$num"'\2/' file
Output: 输出:
aaa bbbbb cccc value1=sevens.eighteen.39489360437938728734value2=yyyy body body body body body body body body body aaa bbbbb cccc value1=xxxx
There are different ways to create a random digit: 创建随机数字的方法有很多种:
RAND_NR="$(tr -cd "[:digit:]" < /dev/urandom | head -c 20)"
Then you can replace the value using sed
: 然后你可以使用
sed
替换值:
sed "s/value1=.*value2=/value1=sevens.eighteen.${RAND_NR}value2=/" file
Double quotes in sed
to expand the variable RAND_NR. sed
双引号用于扩展变量RAND_NR。
The output: 输出:
aaa bbbbb cccc value1=sevens.eighteen.76744539817863807290value2=yyyy
body body body
body body body
body body body
aaa bbbbb cccc value1=xxxx
With GNU awk for the 3rd arg to match() and -i inplace
for "inplace" editing you just need this one command: 使用GNU awk为第3个arg匹配()和
-i inplace
进行“inplace”编辑,你只需要这个命令:
$ cat tst.awk
BEGIN { srand(seed) }
(FNR==1) && match($0,/(.*value1=).*(value2=.*)/,a) {
$0 = a[1] "sevens.eighteen." int(10000000000000000000 + rand()*90000000000000000000) a[2]
}
{ print }
$ awk -i inplace -v seed="$RANDOM" -f tst.awk *
For example: 例如:
$ awk -v seed="$RANDOM" -f tst.awk file
aaa bbbbb cccc value1=sevens.eighteen.77242153065798582272value2=yyyy
body body body
body body body
body body body
aaa bbbbb cccc value1=xxxx
$ awk -v seed="$RANDOM" -f tst.awk file
aaa bbbbb cccc value1=sevens.eighteen.59551444957943291904value2=yyyy
body body body
body body body
body body body
aaa bbbbb cccc value1=xxxx
Note that the above will only operate on the first line of each input file so even if the regexp matched on a different line it wouldn't be changed. 请注意,上述操作仅适用于每个输入文件的第一行,因此即使正则表达式在另一行上匹配,也不会更改。
I see you asked another identical question other than you want to do it for the first and last lines. 我看到你问了另一个相同的问题,而不是你想要的第一行和最后一行。 Just move the code that changes $0 to a function since you'll need to call it twice now and save/change the previous line rather than the current line so you have the last line to work on in ENDFILE (also a GNU awk extension):
只需将改变$ 0的代码移动到一个函数,因为你现在需要调用它两次并保存/更改前一行而不是当前行,这样你就可以在ENDFILE中使用最后一行(也是一个GNU awk扩展) :
$ cat file
aaa bbbbb cccc value1=xxxxxvalue2=yyyy
body body body
body body body
body body body
aaa bbbbb cccc value1=xxxx,value2=yyyy
$ cat tst.awk
BEGIN { srand(seed) }
NR==1 { fmt() }
NR > 1 { print prev }
{ prev = $0 }
ENDFILE { fmt(); print }
function fmt() {
if ( match($0,/(.*value1=).*(value2=.*)/,a) ) {
$0 = a[1] "sevens.eighteen." int(10000000000000000000 + rand()*90000000000000000000) a[2]
}
}
$ awk -v seed="$RANDOM" -f tst.awk file
aaa bbbbb cccc value1=sevens.eighteen.55152151176931606528value2=yyyy
body body body
body body body
body body body
aaa bbbbb cccc value1=sevens.eighteen.98796005800472494080value2=yyyy
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.