如何将仅由字母组成的整个文本拆分为二元组。
例如:
奇偶
od -> de
dd -> ev
这是我到目前为止所拥有的,但它没有产生预期的结果。
[some source] | tail -n +30 | sed 's/[^A-Za-z\n]//g' | sed 's/\([A-Za-z]\)\([A-Za-z]\)\([A-Za-z]\)\([A-Za-z]\)\([A-Za-z]\)\{\0,1\}/\1\2 -> \3\4, \2\3 -> \4\5, /g' | sed 's/,/,\n/g'
答案1
尝试下一个sed
脚本:
内容infile
:
odd even
one test of bigrams
内容script.sed
:
## Inside square brackets there are two characters: space and tab.
## The instruction deletes them of the line.
s/[ ]*//g
## Label 'b'.
:b
## Copy line to 'hold space'.
h
## Get first bigram.
s/\(..\)\(..\).*/\1 -> \2/
## If last substitution succeed, continue to label 'a'.
ta
## Here last substitution failed: It means that line has less than four
## characters to extract a bigram, so read next line.
b
## Label 'a'
:a
## Print.
p
## Copy 'hold space' into 'pattern space'.
g
## Delete first character.
s/^.//
## Goto label 'b' to repeat loop.
tb
运行脚本:
sed -nf script.sed infile
结果:
od -> de
dd -> ev
de -> ve
ev -> en
on -> et
ne -> te
et -> es
te -> st
es -> to
st -> of
to -> fb
of -> bi
fb -> ig
bi -> gr
ig -> ra
gr -> am
ra -> ms
答案2
这可能对你有用:
echo -e "od\ndd\nde\nve" |
sed '1{x;s/^/oddevenodd/;x};G;/^\(..\)\n.*\1\(..\).*/s//\1 -> \2/'
od -> de
dd -> ev
de -> ve
ve -> no
你是这个意思吗?
echo -e "odd even\nthis and that" |
sed 's/ //g;s/^\(..\)\(.*\)/\1\2\1/;h;:a;s/^\(..\)\(..\).*/\1 -> \2/p;g;/^..../{s/^..//;h;ba};d'
od -> de
de -> ve
ve -> no
th -> is
is -> an
an -> dt
dt -> ha
ha -> tt