Linux shell to get the number and position of the character string

shell to get the number and position of the character string

The number of rows 01 to get the string is located


Method 1: Usegrep -n
[root@root]# cat test
apple
bit
create
delect
exe
flow
good
[root@root]# cat test | grep -n exe
5:exe
[root@root]# cat test | grep -n exe | awk -F ":" '{print $1}'
5
Second way: withsed -n '/查询的字符串/=' 文件
[root@root]# cat test
apple
bit
create
delect
exe
flow
good
[root@root]# 
[root@root]# sed -n  '/exe/=' test
5

02 get position in the string of characters located


One way: with awk -Fand wc -ccomposition
[root@root]# echo 'uellevcmpottcap' | awk -F 'ott' '{print $1}';
uellevcmp
[root@root]# echo 'uellevcmpottcap' | awk -F 'ott' '{print $1}' | wc -c
10
Second way : withawk 'BEGIN{print index("'${str}'","'${str1}'") }'
[root@root]# str='uellevcmpottcap';str1='ott';awk 'BEGIN{print index("'${str}'","'${str1}'") }'
10

Guess you like

Origin www.cnblogs.com/xiaolincoding/p/11366274.html