代码之家  ›  专栏  ›  技术社区  ›  Jay Gray

如何运行GnuWin32 sed和脚本文件?

sed
  •  0
  • Jay Gray  · 技术社区  · 7 年前

    我在跑步 GnuWin32 在下面 Windows 10

    我正在尝试运行以下程序 sed 一个使用Gnu Bash外壳的衬里:

    sed -f <(sed -E 's_(.+)\t(.+)_s/\1/\2/g_' C:/dictionary.txt) C:/content.txt
    

    这个 file substitute sed 语句将字典条目转换为sed表达式。这个 main sed 将其用于内容替换。

    这一点在 How to awk to read a dictionary and replace words in a file?

    dictionary.txt 看起来像这样:

    aluminium<tab>aluminum
    analyse<tab>analyze
    white spirit<tab>mineral spirits
    stag night<tab>bachelor party
    savoury<tab>savory
    potato crisp<tab>potato chip
    mashed potato<tab>mashed potatoes
    

    content.txt 看起来像这样:

    The container of white spirit was made of aluminium.
    We will use an aromatic method to analyse properties of white spirit.
    No one drank white spirit at stag night.
    Many people think that a potato crisp is savoury, but some would rather eat mashed potato.
    ...
    more sentences
    

    在windows 10下的GnuBash shell中运行GnuWin32/sed时,我收到以下错误消息: syntax error near unexpected token <(s

    如何重新制定脚本以在windows 10下的GnuWin32/sed下运行?


    感谢 https://stackoverflow.com/users/2836621/mark-setchell 和 https://stackoverflow.com/users/5403468/tiw 当使用 cygwin64

    0 回复  |  直到 7 年前
        1
  •  2
  •   Tyl    7 年前

    一种方法是先将内部sed输出写入临时文件,使用它,然后删除它:

    sed -r "s_(.+)\t(.+)_s/\1/\2/g_" C:/dictionary.txt>tmp_script.sed
    sed -f tmp_script.sed C:/content.txt
    del tmp_script.sed
    

    另一种方式是,根据马克·塞切尔的评论,再加上一些小调整, 具有 cygwin 安装 ,
    bash和batch上的这项工作:

    sed -r "s_(.+)\t(.+)_s/\1/\2/g_" C:/dictionary.txt | sed -f /dev/stdin C:/content.txt
    
    推荐文章