iis服务器助手广告广告
返回顶部
首页 > 资讯 > 操作系统 >Linux shell 获得字符串所在行数及位置的方法
  • 831
分享到

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

Linuxshell获得字符串行数shel字符串位置 2022-06-04 23:06:30 831人浏览 泡泡鱼
摘要

01 获取字符串所在的行数 方式一:用grep -n [root@root]# cat test apple bit create delect exe flow Good [root@root]# cat te

01 获取字符串所在的行数

方式一:用grep -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

方式二:用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' '{pgvuZMLarJrint $1}';
uellevcmp
[rogvuZMLarJot@root]# echo 'uellevcmpottcap' | awk -F 'ott' '{print $1}' | wc -c
10

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


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

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

--结束END--

本文标题: Linux shell 获得字符串所在行数及位置的方法

本文链接: https://www.lsjlt.com/news/20507.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作