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

时间:2019-03-01 15:09:56

标签: sed

我在GnuWin32下运行Windows 10

我正在尝试使用Gnu Bash shell运行以下sed单线:

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-setchellhttps://stackoverflow.com/users/5403468/tiw,该解决方案在使用cygwin64时有效

1 个答案:

答案 0 :(得分:2)

一种方法是先将内部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

另一种方法是基于Mark Setchell先生的评论,加上一些小的调整,安装了cygwin
在bash和批处理上都可以完成这项工作:

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