更快的方式做搜索和文本逗号分隔线更换? [英] Faster way to do search and replace on a comma delimited line of text?

查看:83
本文介绍了更快的方式做搜索和文本逗号分隔线更换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想做一个搜索和替换在每一行,形成SQL语句的CSV文件。我想出了这个perl脚本...

I have a CSV file in which I want to do a search and replace on each line to form SQL statements. I have come up with this perl script ...

#!/bin/bash
perl -pi -e "s/(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*)/REPLACE INTO student (ID, SIS_ID, STUDENT_NUM, USER_ID, OTHER_USER_ID) VALUES (REPLACE(uuid(), '-', ''), '\$24', '\$26', '\$2', '\$27');/g" $1 

然而,在一个行文件,这需要约15秒至运行。正如你可以在线路thousnads的文件可想而知,这需要时间。

However, on a one line file, this takes about 15 seconds to run. As you can imagine on a file of thousnads of lines, this takes hours.

有另一种方式我可以写上面,将加快搜索和替换?我没有结婚到Perl的。我使用的是Mac 10.9.5,bash shell的。

Is there another way I can write the above that would speed up the search and replace? I'm not married to Perl. I'm using Mac 10.9.5, bash shell.

推荐答案

尝试更换每个 这个(。*):
([^,] +)

Try replacing each (.*) with this: ([^,]+)

我相信这会减少执行时间。

I am sure it will reduce the execution time.

这篇关于更快的方式做搜索和文本逗号分隔线更换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆