且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 sed 或等效命令向文件的每一行添加新的 uuid

更新时间:2023-12-04 10:09:22

使用 While 循环的可读性很强的 Bash 解决方案

您可以使用默认的 REPLY 变量将文件读入 Bash 循环.例如:

A Very Readable Bash Solution Using a While-Loop

You can read the file into a Bash loop using the default REPLY variable. For example:

while read; do
    echo "insert into table values ('$(uuidgen)','$REPLY');"
done < /tmp/foo

可读性较差的 Sed 解决方案

sed -e 's/.*/echo "insert into table values (\\"$(uuidgen)\\",\\"&\\");"/e' \
    -e "s/\"/'/g" /tmp/foo

while 循环明显比 sed 替代方案更具可读性,因为必须在替换字符串中转义引号.sed 解决方案也相当脆弱,因为它在 sed 表达式中评估文件的内容,当存在某些元字符时可能会导致错误.最后,这个特殊的 sed 解决方案依赖于 /e 标志,它是一个 GNU sed 扩展,可能在您的平台上不可用.

The while-loop is significantly more readable than the sed alternative, because of the necessity to escape quotes in your replacement string. The sed solution is also rather brittle because of the fact that it is evaluating the contents of your file inside a sed expression, which may cause errors when certain meta-characters are present. And finally, this particular sed solution relies on the /e flag, which is a GNU sed extension that may not be available on your platform.

GNU sed 手册对标志的描述如下:

The GNU sed manual describes the flag as follows:

此命令允许将来自 shell 命令的输入通过管道传输到模式空间.如果进行了替换,则会执行在模式空间中找到的命令,并用其输出替换模式空间.尾随换行符被抑制;如果要执行的命令包含空字符,则结果未定义.这是一个 GNU sed 扩展.

This command allows one to pipe input from a shell command into pattern space. If a substitution was made, the command that is found in pattern space is executed and pattern space is replaced with its output. A trailing newline is suppressed; results are undefined if the command to be executed contains a nul character. This is a GNU sed extension.

测试

两个脚本都针对 /tmp/foo 进行了测试,其中包含以下夹具数据:

Testing

Both scripts were tested against /tmp/foo, which contained the following fixture data:

A
B
C

Bash 示例输出:

insert into table values ('fe0ca930-456b-4265-810c-219eb93c4c73','A');
insert into table values ('34b088eb-3dc0-46fa-85ca-efaf3f0c0f4b','B');
insert into table values ('5d271207-99fe-4ca2-8420-3b8ca774e99b','C');

GNU sed 示例输出:

GNU sed Sample Output:

insert into table values ('4c924b78-dc70-441d-928e-638fec9f3ea1','A');
insert into table values ('29f424d4-6e33-4646-a773-cd0e96ebb874','B');
insert into table values ('39534c05-6853-4390-a6b6-4a19fad296b1','C');

结论

Bash 解决方案似乎比 sed 解决方案更清晰、更健壮.但是,这两种解决方案显然都适用于原始问题中提供的夹具数据,因此您应该选择最适合您的实际数据.

Conclusion

The Bash solution seems clearer and more robust than the sed solution. However, both solutions clearly work on the fixture data provided in the original question, so you should pick whichever one works best for you on the real data.