优化多个SED替代shell脚本 [英] Optimize shell script for multiple sed replacements

查看:267
本文介绍了优化多个SED替代shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含这是由 SED 用于文件替换字符串替换对(其中约100)的列表中的文件。

I have a file containing a list of replacement pairs (about 100 of them) which are used by sed to replace strings in files.

在对去这样的:

old|new
tobereplaced|replacement
(stuffiwant).*(too)|\1\2

和我目前的code是:

and my current code is:

cat replacement_list | while read i
do
    old=$(echo "$i" | awk -F'|' '{print $1}')    #due to the need for extended regex
    new=$(echo "$i" | awk -F'|' '{print $2}')
    sed -r "s/`echo "$old"`/`echo "$new"`/g" -i file
done

我不禁觉得有进行替代的更优化的方式。我试图扭转在遍历文件的第一行运行但竟然是贵得多。

I cannot help but think that there is a more optimal way of performing the replacements. I tried turning the loop around to run through lines of the file first but that turned out to be much more expensive.

是否有加快这个脚本的其他方法吗?

Are there any other ways of speeding up this script?

修改

感谢所有的快速反应。让我选择一个答案之前,尝试提出的各种建议。

Thanks for all the quick responses. Let me try out the various suggestions before choosing an answer.

有一点需要澄清:我还需要SUBEX pressions /组功能。例如,一个替代我可能需要的是:

One thing to clear up: I also need subexpressions/groups functionality. For example, one replacement I might need is:

([0-9])U|\10  #the extra brackets and escapes were required for my original code

在改进一些细节(待更新):

Some details on the improvements (to be updated):


  • 方法:处理时间

  • 原创剧本:0.85s

  • 剪切而不是 AWK :0.71s

  • anubhava的方法:0.18s

  • chthonicdaemon的方法:为0.01s

  • Method: processing time
  • Original script: 0.85s
  • cut instead of awk: 0.71s
  • anubhava's method: 0.18s
  • chthonicdaemon's method: 0.01s

推荐答案

您可以使用 SED 来产生正确格式化的 SED 输入:

You can use sed to produce correctly -formatted sed input:

sed -e 's/^/s|/; s/$/|g/' replacement_list | sed -r -f - file

这篇关于优化多个SED替代shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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