Linux shell 获得字符串所在行数及位置的方法
2019-08-19 07:19
1356 查看
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' '{print $1}'; uellevcmp [root@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
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
相关文章推荐
- JavaScript使用indexOf获得子字符串在字符串中位置的方法
- php根据指定位置和长度获得子字符串的方法
- 时间DateTime类型转换为字符串String格式方法汇总 获得最后一次编译时间
- linux shell 字符串操作详解 (长度,读取,替换,截取,连接,对比,删除,位置 )
- JQuery 获得绝对,相对位置的坐标方法--非常重要
- 找出字符串数组中不重复的数据并输出所在位置
- php如何实现不借助IDE快速定位行数或者方法定义的文件和位置
- linux shell 字符串操作详解 (长度,读取,替换,截取,连接,对比,删除,位置 )
- python 巧用正则寻找字符串中的特定字符的位置方法
- Linux shell 获得字符串长度
- linux shell脚本编程笔记(四): 获取字符串长度的七种方法
- android EditText插入字符串到光标所在位置(表情开发用到,记下)
- [置顶] 在字符串中查找一个特定的字符第一次出现的位置,并返回字符所在位置;如果没有则返回NULL。
- python在TXT文件中按照某一字符串取出该字符串所在的行方法
- 程序员面试金典: 9.11 排序与查找 11.1编写一个方法,对字符串数组进行排序,将所有变位词排在相邻的位置。
- JQuery 获得绝对,相对位置的坐标方法
- EditText插入表情(字符串)到光标所在位置
- linux shell对行数进行统计的方法
- IntelliJ 有的时候移动滚动条后会自动回到光标所在位置的解决方法
- Nodejs使用mysql模块之获得更新和删除影响的行数的方法