gpt4 book ai didi

regex - 在特定字符串之间替换文本

转载 作者:行者123 更新时间:2023-11-29 09:38:25 25 4
gpt4 key购买 nike

我正在尝试编写一个脚本来帮助我进行语言学实验。该实验向受试者展示文本短语,他们需要逐字阅读这些短语。例如,假设我有以下短语:

The girl was upset with her boyfriend.

我需要将这个短语分成小部分,以便只向将要参加实验的受试者展示这些小部分。向受试者显示短语的软件采用以下输入:

The ---- --- ----- ---- --- ----------
--- girl --- ----- ---- --- ----------
--- ---- was ----- ---- --- ----------
--- ---- --- upset ---- --- ----------
--- ---- --- ----- with --- ----------
--- ---- --- ----- ---- her ----------
--- ---- --- ----- ---- --- boyfriend.

请注意,完整的短语绝不是输入。我需要将小部分提供给软件,以便它在计算机屏幕上显示短语。此外,屏幕上没有出现的单词必须改为破折号,长度与原始单词相同。

我正在考虑使用其中一种 bash 工具(如 sed、grep、awk 等)来解决我的问题。例如,我可以将原始短语写成

The | girl | was | upset | with | her | boyfriend.

复制七次,每次复制,我不需要的词都用破折号表示。请注意,单词始终位于两个“|”之间,以便于识别它们。

(事实上,有时我需要替换的不仅仅是单词。例如,我可能不得不一次替换“The girl”)

关于如何实现这个的任何想法?

最佳答案

看看这个 awk 单行代码,如果有帮助的话:

awk '{for(i=1;i<=NF;i++){t=$0;w=$i;gsub(/\S/,"-");$i=w;print;$0=t}}' file

用你的例子测试:

kent$  cat f
The girl was upset with her boyfriend.
Yes @Kent, you are right. – grandeabobora 6 mins ago

kent$ awk '{for(i=1;i<=NF;i++){t=$0;w=$i;gsub(/\S/,"-");$i=w;print;$0=t}}' f
The ---- --- ----- ---- --- ----------
--- girl --- ----- ---- --- ----------
--- ---- was ----- ---- --- ----------
--- ---- --- upset ---- --- ----------
--- ---- --- ----- with --- ----------
--- ---- --- ----- ---- her ----------
--- ---- --- ----- ---- --- boyfriend.
Yes ------ --- --- ------ - ------------- - ---- ---
--- @Kent, --- --- ------ - ------------- - ---- ---
--- ------ you --- ------ - ------------- - ---- ---
--- ------ --- are ------ - ------------- - ---- ---
--- ------ --- --- right. - ------------- - ---- ---
--- ------ --- --- ------ – ------------- - ---- ---
--- ------ --- --- ------ - grandeabobora - ---- ---
--- ------ --- --- ------ - ------------- 6 ---- ---
--- ------ --- --- ------ - ------------- - mins ---
--- ------ --- --- ------ - ------------- - ---- ago

关于regex - 在特定字符串之间替换文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18645209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com