您现在的位置是:网站首页> 编程资料编程资料
Linux shell 获得字符串所在行数及位置的方法_linux shell_
2023-05-26
311人已围观
简介 Linux shell 获得字符串所在行数及位置的方法_linux shell_
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 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
您可能感兴趣的文章:
相关内容
- 一篇教会你写90%的shell脚本(入门小结)_linux shell_
- linux shell 根据进程名获取pid的实现方法_linux shell_
- shell 使用指定的分割符来分割字符串的方法_linux shell_
- linux shell 中判断文件、目录是否存在的方法_linux shell_
- linux shell中“.” 和 “./”执行的区别详解_linux shell_
- 详解Shell脚本中调用另一个Shell脚本的三种方式_linux shell_
- 详解adb shell 常用命令_linux shell_
- 详解shell脚本[] [[]] -n -z 的含义解析_linux shell_
- Shell $0, $#, $*, $@, $?, $$和命令行参数的使用_linux shell_
- shell 操作钉钉机器人实现告警提醒的方法_linux shell_
