提取所选字母的相邻字符

sha*_*nuo 2 awk grep sed

我有这个文本文件:

# cat letter.txt
this
is
just
a
test
to
check
if
grep
works
Run Code Online (Sandbox Code Playgroud)

字母“e”出现在 3 个单词中。

# grep e letter.txt
test
check
grep
Run Code Online (Sandbox Code Playgroud)

有没有办法返回打印在所选字符左侧的字母?

expected.txt
t
h
r
Run Code Online (Sandbox Code Playgroud)

Rav*_*h13 5

使用 中显示的示例awk,请您尝试以下操作。

awk '/e/{print substr($0,index($0,"e")-1,1)}' Input_file
Run Code Online (Sandbox Code Playgroud)

说明:为以上添加详细说明。

awk '             ##Starting awk program from here.
/e/{              ##Looking if current line has e in it then do following.
  print substr($0,index($0,"e")-1,1)
                  ##Printing sub string from starting value of index e-1 and print 1 character from there.
}
' Input_file      ##Mentioning Input_file name here.
Run Code Online (Sandbox Code Playgroud)