我有一张我想过滤的单词列表:只有那些以连字符开头或结尾的词,而不是中间带有连字符的单词。也就是说,过滤诸如"a-“或"-cefalia”之类的条目,而不是"castellano-manchego“。
我尝试了许多选项,我发现最相似的东西是grep -E '*\-' minilemario.txt,但是它过滤了所有的连字符。你能给我一个解决办法吗?
a
a-
aarónico
aaronita
amuzgo
an-
-án
ana
-ana
ana-
anabaptismo
anabaptista
blablá
bla-bla-bla
blanca
castellano
castellanohablante
castellano-leonés
castellano-manchego
castellanoparlante
cedulario
cedulón
-céfala
cefalalgia
cefalálgico
cefalea
-cefalia
cefálica
cefálico
cefalitis
céfalo
-céfalo
cefalópodo
cefalorraquídeo
cefalotórax
cefea
ciabogar
cian
cian-
cianato
cianea
cianhídrico
cianí
ciánico
cianita
ciano-
cianógeno
cianosis
cianótico
cianuro
ciar
ciática
ciático
zoo
zoo-
zoófago发布于 2014-03-20 12:20:45
使用grep,例如:
grep -E '^-|-$' filename得到以-开头和结尾的单词。和
grep -v -E '^-|-$' filename若要排除以-开头和结尾的单词,请执行以下操作。
^和$分别表示行的开始和结束。您使用的是'*\-',它将与-后面的任何内容相匹配(它并不表示-位于行尾)。
发布于 2014-03-20 12:38:11
这里是巴什唯一的解决方案。详情请参阅评论:
#!/usr/bin/env bash
# Assign the first argument (e.g. a textfile) to a variable
input="$1"
# Bash 4 - read the data line by line into an array
readarray -t data < "$input"
# Bash 3 - read the data line by line into an array
#while read line; do
# data+=("$line")
#done < "$input"
# For each item in the array do something
for item in "${data[@]}"; do
# Line starts with "-" or ends with "-"
[[ "$item" =~ ^-|-$ ]] && echo "$item"
done这将产生以下输出:
$ ./script input.txt
a-
an-
-án
-ana
ana-
-céfala
-cefalia
-céfalo
cian-
ciano-
zoo-https://stackoverflow.com/questions/22532790
复制相似问题