菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
279
0

Linux shell 获得字符串所在行数及位置的方法

原创
05/13 14:22
阅读数 16713

方式一:用grep -n

www.cppcns.com
[roohttp://www.cppcns.comt@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

方式二:用sed -n '/查询的字符串/=' 文件

[root@root]# cat test
apple
bit
create
delect
exe
flow
good
[root@root]# 
[root@root]# sed -n '/exe/=' test
5

02 获取字符串中字符所在的位置
编程客栈

方式一:用awk -F 和 wc -c 组合

[root@root]# echo 'uellevcmpottcap' | awk -F 'ott' '{print $1}';
uellevcmp
[root@root]# echo 'uellevcmpottcap' | awk -F 'ott' '{print $1}' | wc -c
10
http://www.cppcns.com

方式二:用awk 'BEGIN{print index("'${str}'","'${str1}'") }'

[root@root]# str='uellevcmpottcap';str1='ott';awk 'BEGIN{print index("'${str}'","'${str1}编程客栈'") }'
10

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

本文标题: Linux shell 获得字符串所在行数及位置的方法
本文地址: http://www.cppcns.com/os/linux/268873.html

发表评论

0/200
279 点赞
0 评论
收藏
为你推荐 换一批