The shell gets the number of lines and position of the string

01 Obtain the number of lines in the string


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
Copy the code
Method two: useSed -n '/ Query string /=' file
[root@root]# cat test
apple
bit
create
delect
exe
flow
good
[root@root]# 
[root@root]# sed -n  '/exe/=' test
5
Copy the code

02 Obtain the position of the character in the string


Method 1: Useawk -Fwc -ccombination
[root@root]# echo 'uellevcmpottcap' | awk -F 'ott' '{print $1}';
uellevcmp
[root@root]# echo 'uellevcmpottcap' | awk -F 'ott' '{print $1}' | wc -c
10
Copy the code
Way 2:awk 'BEGIN{print index("'${str}'","'${str1}'") }'
[root@root]# str='uellevcmpottcap'; str1='ott'; awk'BEGIN{print index("'${str}'","${str1}'")}'
10
Copy the code