[英]How to print n number of characters appearing after a searched character in BASH?
我有一個這樣的文本文件:
Once upon a time, there lived a rabbit who lived in the forest.
One day, rabbit found bear.
Bear said, "Hello!"
給定字母,例如“ e”,我需要輸出所有e + the next 2 characters
,例如,這將輸出以下內容:
e u
e,
ere
e l
ed
ed
est
e d
ear
ear
ell
每當搜索到的字符出現在BASH文件中時,如何打印搜索到的字符以及接下來的2個字符?
可以使用perl
嗎?
perl -lne 'print "e",$_ for $_ =~ /(?<=e)(..)/g' file
$ perl -lne 'print "e",$_ for $_ =~ /(?<=e)(..)/g' file
e u
e,
ere
e l
ed
ed
e f
est
e d
ear
ear
ell
要包含重疊的字符串,可以使用以下命令:
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
如果您的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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.