2

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

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”)

关于如何做到这一点的任何想法?

4

2 回答 2

6

如果有帮助,请参阅此 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
于 2013-09-05T20:33:14.223 回答
1

纯 bash 解决方案:

#!/bin/bash

data='The girl was upset with her boyfriend.'
dashed="${data//[^ ]/-}"

IFS=' ' read -ra dataArray <<< "$data"
IFS=' ' read -ra dashedArray <<< "$dashed"
for ((i=0; i < ${#dataArray[@]}; i++)); do
    if ((i == 0)); then
        echo "${dataArray[i]} ${dashedArray[@]:i+1}"
    else
        echo "${dashedArray[@]:0:i} ${dataArray[i]} ${dashedArray[@]:i+1}"
    fi
done

没有 if 语句的更复杂的解决方案(如果你能理解它,你就是那个男人!):

#!/bin/bash

data='The girl was upset with her boyfriend.'
dashed="${data//[^ ]/-}"

IFS=' ' read -ra dashedArray <<< "$dashed"
IFS=' ' read -ra dataArray <<< "$data"
size=${#dataArray[@]}
for ((i=0; i < size; i++)); do
    echo "${dashedArray[@]:0:i}${dashedArray[size-i]+ }${dataArray[i]} ${dashedArray[@]:i+1}"
done
于 2013-09-05T20:50:24.277 回答