如何在BASH中打印在搜索的字符后出现的n个字符?

如何在BASH中打印在搜索的字符后出现的n个字符?,bash,grep,Bash,Grep,我有这样一个文本文件: Once upon a time, there lived a rabbit who lived in the forest. One day, rabbit found bear. Bear said, "Hello!" 给定一个字母,例如“e”,我需要输出对e+接下来2个字符的所有搜索,例如,这将打印以下内容: e u e, ere e l ed ed est e d ear ear ell 可以打印任何字符,包括标点符号或符号,但不能打印换行符 这是区分

我有这样一个文本文件:

Once upon a time, there lived a rabbit who lived in the forest.
One day, rabbit found bear.
Bear said, "Hello!"
给定一个字母,例如“e”,我需要输出对
e+接下来2个字符的所有搜索,例如,这将打印以下内容:

e u
e, 
ere
e l
ed 
ed 
est
e d
ear
ear
ell
  • 可以打印任何字符,包括标点符号或符号,但不能打印换行符
  • 这是区分大小写的。如果搜索了“e”,则不查找“e”

如何在每次搜索的字符出现在BASH的文件中时打印一个搜索的字符加上接下来的2个字符?

要包含重叠字符串,可以使用以下方法:

while IFS= read -r line
do
    while [ "${#line}" -gt 2 ]
    do
        if [ "${line:0:1}" = 'e' ]
        then
            echo "${line:0:3}"
        fi
        line="${line:1}"
    done
done

要包含重叠字符串,可以使用以下方法:

while IFS= read -r line
do
    while [ "${#line}" -gt 2 ]
    do
        if [ "${line:0:1}" = 'e' ]
        then
            echo "${line:0:3}"
        fi
        line="${line:1}"
    done
done

你能用perl吗

perl -lne 'print "e",$_ for $_ =~ /(?<=e)(..)/g' file

perl-lne'打印“e”,用$表示$/(?你能用
perl

perl -lne 'print "e",$_ for $_ =~ /(?<=e)(..)/g' file

perl-lne'打印“e”,如果grep支持-o,那么就很容易了

grep -o e..

如果您的grep支持-o,那么就很容易了

grep -o e..
艰难的道路

echo $(cat file.txt ) > temp
grep -o . temp | nl | grep e | cut -f1 | xargs -i -n1 echo "{}; {} + 2;" | bc | xargs -n2 echo | sed 's/ /-/' |  xargs -i{} -n 1 cut -c {} temp
艰难的道路

echo $(cat file.txt ) > temp
grep -o . temp | nl | grep e | cut -f1 | xargs -i -n1 echo "{}; {} + 2;" | bc | xargs -n2 echo | sed 's/ /-/' |  xargs -i{} -n 1 cut -c {} temp
这将不包括重叠字符串,如l0b0所说!这将不包括重叠字符串,如l0b0所说!
echo$(cat file.txt)>temp
:请严肃点。
echo$(cat file.txt)>temp
:请严肃点。