开发者

Replace all UUID strings with new UUID strings

开发者 https://www.devze.com 2023-03-22 16:31 出处:网络
I have a text file contains some different UUID strings in it, I want to replace all the U开发者_运维知识库UID strings by new different UUID strings, how can I do that easily in Bash?Assuming the uuid

I have a text file contains some different UUID strings in it, I want to replace all the U开发者_运维知识库UID strings by new different UUID strings, how can I do that easily in Bash?


Assuming the uuid format contains only hexadecimals with hyphens between them and these number of chars between each hypen: 8, 4, 4, 4, 12. I'm also assuming they are on their own line.

awk '
{
    if ($0 ~ /^[a-fA-F0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}$/)
    {
        system("uuidgen")
    }
    else
    {
        print $0
    }
}' file-with-uuids > new-file-with-uuids

# To override the old file with the new:
mv -f new-file-with-uuids file-with-uuids


How about:

sed -i "s/$OLDUUID/$NEWUUID/g" file.txt 

can't tell from your description, if you have multiple (different) UUIDs to replace of these, you may need to chain such operations.


Way late to this, but if anyone else is looking to replace all UUIDs in a file with new ones, this awk command should help:

awk '/[a-fA-F0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}/{"uuidgen" | getline uuid; close("uuidgen"); gsub(/[a-fA-F0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}/, uuid)};{print}' myfile > mynewfile

For each line that contains a uuid, it will generate a new uuid and then substitute the existing uuid for the newly generated one.

0

精彩评论

暂无评论...
验证码 换一张
取 消