提取所选字母的相邻字符
我有这个文本文件:
# cat letter.txt
this
is
just
a
test
to
check
if
grep
works
字母“e”出现在 3 个单词中。
# grep e letter.txt
test
check
grep
有没有办法返回打印在所选字符左侧的字母?
expected.txt
t
h
r
回答
使用 中显示的示例awk,请您尝试以下操作。
awk '/e/{print substr($0,index($0,"e")-1,1)}' Input_file
说明:为以上添加详细说明。
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.