1.题目
2.参考答案
系统环境
[[email protected] ~]# uname -r2.6.32-504.el6.x86_64 [[email protected] ~]# cat /etc/redhat-releaseCentOS release 6.6 (Final) [[email protected] ~]# a=‘a/b/c‘
方法1-cut
[[email protected] ~]# echo $a | cut -c5c
方法2-tr替换
[[email protected] ~]# echo $a | tr "a/b/" " " <==四个空格c
方法3-tr+tail方法
[[email protected] ~]# echo $a | tr "/" "\n"|tail -1c ```language
方法4-tr删除
[[email protected] ~]# echo $a | tr -d ‘a/b/‘c
方法5-rev+head
[[email protected] ~]# echo $a | rev |head -c1c
方法6-rev+cut
[[email protected] ~]# echo $a | rev |cut -c1c
方法7-grep-o参数
[[email protected] ~]# echo $a | grep -o "[a-z]$"c
方法8-grep正则排除
[[email protected] ~]# echo $a | grep -o "[^ab/]"c
方法9-sed替换
[[email protected] ~]# echo $a | sed ‘s#.*/##‘c
方法10-sed命令的1对1替换
[[email protected] ~]# echo $a | sed ‘y#a/b/# #‘ <==四个空格c
方法11-sed命令反向引用01
[[email protected] ~]# echo $a | sed -r ‘s#....(.)#\1#‘c
方法12-sed命令反向引用02
[[email protected] ~]# echo $a | sed -r ‘s#.*([a-z]$)#\1#’
c
方法13-awk分隔符(菜刀)
[[email protected] ~]# echo $a | awk -F/ ‘{print $NF}‘c
方法14-awk-gsub函数-查找替换
[[email protected] ~]# echo $a | awk ‘{gsub(".*/","",$0);print}‘c [[email protected] ~]# echo $a | awk ‘{gsub(".*/","",$0)}1‘c
方法15-awk-split函数-切割1
[[email protected] ~]# echo $a | awk ‘{split($0,array,"/");print array[3]}‘c
方法16-awk-split函数-切割2
[[email protected] ~]# echo $a | awk ‘{split($0,array,"a/b/");print array[2]}‘c
方法17-awk-sub替换
[[email protected] ~]# echo $a | awk ‘{sub(/[^c]+/,"");print}‘c
方法18-gsub替换-替换为空格或回车
[[email protected] ~]# echo $a | awk ‘{gsub("/","\n");print $3}‘c
方法19-awk-substr函数-cut截取字符
[[email protected] ~]# echo $a | awk ‘{print substr($0,5,1) }‘c
方法20-awk-修改RS记录分隔符
[[email protected] ~]# echo $a | awk ‘BEGIN{RS="[/\n]"}NR==3‘c [[email protected] ~]# echo $a | awk -vRS="[/\n]" ‘NR==3‘c
方法21-cut指定分隔符
[[email protected] ~]# echo $a|cut -d/ -f3c
方法22-shell字符串截取
[[email protected] ~]# echo ${a:4}c
方法23-shell字符串替换
[[email protected] ~]# echo ${a##*/} c
方法24-tr+awk
[[email protected] ~]# echo $a |tr "/" "\n"|awk ‘NR==3‘c
方法25-shell字符串截取
[[email protected] ~]# echo ${a:4:1} c
方法26-shell字符串替换
[[email protected] ~]# echo ${a##a/b/}c
QQ群回答
[[email protected] ~]# basename $a # 把a/b/c当作系统路径处理 c [[email protected] ~]# echo $a | grep -o ‘[[:alpha:]]$‘ c
3.统计信息
今天是每日一题陪伴大家的第55天,期待你的进步。
对于题目和答案的任何疑问,请在博客评论区留言。
往期题目索引
http://lidao.blog.51cto.com/3388056/1914205
时间: 2024-10-21 17:41:49