简体   繁体   English

如何使用字符串和随机生成的数字替换文件中的标头值?

[英]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.

相关问题 如何用新的标头和尾部字符串替换目录中所有文件的标头和尾部,但最多只能确定一个字符? - How do I replace the header and trailer of all files in a directory with a new header and trailer string, but only up to a certain character? 如何将文件的行随机插入另一个文本文件? - How do I insert lines of a file randomly into another text file? 如何计算整个文件中字符串的出现次数? - How do I count the number of occurrences of a string in an entire file? file.replace(&#39;abcd&#39;)也替换为&#39;abcde&#39;如何仅替换精确值? - file.replace('abcd') also replaces 'abcde' How do I only replace exact value? 我如何在 bash 中 grep 和替换字符串 - How do I grep and replace string in bash 如何使用Shell脚本替换.txt文件中未确定位置的字符串? - How do I replace a string at an undetermined location in a .txt file with a Shell script? 使用bash,如何查找所有包含特定字符串的文件并将其替换为现有文件? - Using bash, how do I find all files containing a specific string and replace them with an existing file? 使用sed,我想替换文件中的字符串值 - Using sed, i want to replace the the string value in a file 如何在文本文件中逐行替换特定行? - How can I replace a specific line by line number in a text file? 如何替换文件中的值 - how to replace value in a file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM