使用sed或等效命令向文件的每一行添加新的uuid(Add new uuid to each line of a file using sed or equivalent command)

编程入门 行业动态 更新时间:2024-10-27 12:34:51
使用sed或等效命令向文件的每一行添加新的uuid(Add new uuid to each line of a file using sed or equivalent command)

我有一个多行文本文件,我想用它来创建需要UUID的SQL语句。 我试图想出一种使用sed或其他shell命令实用程序生成SQL的方法。

Example input: A B C Example Output: insert into table values ('7CC92727-D743-45E0-BE57-9FEB2B73BD18','A'); insert into table values ('94E03071-F457-48DD-86E2-AF26309A192D','B'); insert into table values ('1F17525C-9215-4FC4-A608-4FA43C0B2B14','C');

我可以使用uuidgen命令来生成新的UUID,但到目前为止还没有找到与sed一起使用该命令的方法。

更新:感谢答案,我能够想出一个在cygwin上为我工作的sed命令。

在没有引用用于didatic目的的SQL值的情况下表达:

sed 's/.*/echo "insert into table values (`uuidgen | tr -d \r`,&)"/e' file.txt

用引号:

sed 's/.*/echo "insert into table values '\''(`uuidgen | tr -d \r`'\'','\''&'\'')"/e' file.txt

I've got a multi line text file that I want to use to create an SQL statement that requires a UUID. I'm attempting to come up with a way to generate the SQL using sed or some other shell command utility.

Example input: A B C Example Output: insert into table values ('7CC92727-D743-45E0-BE57-9FEB2B73BD18','A'); insert into table values ('94E03071-F457-48DD-86E2-AF26309A192D','B'); insert into table values ('1F17525C-9215-4FC4-A608-4FA43C0B2B14','C');

I can use the uuidgen command to generate new UUID's but so far haven't found a way to use that command with sed.

Update: Thanks to the answers I was able to come up with a sed command that worked for me on cygwin.

Expressed without quoting the SQL values for didatic purposes:

sed 's/.*/echo "insert into table values (`uuidgen | tr -d \r`,&)"/e' file.txt

With the quotes:

sed 's/.*/echo "insert into table values '\''(`uuidgen | tr -d \r`'\'','\''&'\'')"/e' file.txt

最满意答案

使用While循环的非常易读的Bash解决方案

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

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扩展 ,可能在您的平台上不可用。

GNU sed手册描述了如下标志:

这个命令允许将命令从shell命令输入到模式空间。 如果进行替换,则会执行在模式空间中找到的命令,并将模式空间替换为其输出。 尾随的换行符被抑制; 如果要执行的命令包含一个空字符,则结果未定义。 这是一个GNU sed扩展。

测试

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

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示例输出:

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解决方案更清晰更强大。 但是,这两种解决方案都明确地处理原始问题中提供的夹具数据,所以您应该选择哪一种最适合您的真实数据。

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

A Less-Readable Sed Solution

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

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.

The GNU sed manual describes the flag as follows:

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.

Testing

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

A B C

Bash Sample Output:

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 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');

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.

更多推荐

本文发布于:2023-08-04 23:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1424167.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令   文件   uuid   sed   Add

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!