gpt4 book ai didi

grep - 提取所选字母的相邻字符

转载 作者:行者123 更新时间:2023-12-05 08:19:47 26 4
gpt4 key购买 nike

我有这个文本文件:

# 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.

关于grep - 提取所选字母的相邻字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67068325/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com