SHELL字符串操作

bash Shell提供了多种字符串处理的命令:

  • awk命令
  • expr命令

字符串长度

  • ${#..}
  • expr length
  • awk的length(s)

    实例:

    string=”hello world”

    ${#string}

    expr length “$string”

    注意:双引號是必须的。由于字符串有空格

匹配子串

格式:expr match $string $substring

作用:在string的开头匹配substring,返回匹配到的长度,在string开头匹配不到substring则返回0,substring能够是正則表達式

string=”welcome to our world”

命令 返回值
expr match “$string” w.* 20
expr match “$string” ou.* 0

公共字符的索引

格式:expr index $string $sunstring

作用:在字符串string上匹配substring中字符第一次出现的字符

string=”welcome to our world”

命令 返回值
expr index “$string” our 5
expr index “$string” d 20
expr index “$string” s 0

执行发现。expr index的功能是寻找两个串之间的第一个公共字符

截取子串

  • 从左截取

    • ${string:position}
    • ${string:position:length}
  • 从右截取
    • ${string: -position}(冒号后面有一个空格)
    • ${string:(position)}
    • ${string: -position:length}
    • ${string:(position):length}

expr substr

格式:expr substr $string $position $length

与${}的差别:${}的position从0開始给string标号;expr sutstr的position从1開始给string标号

string=”welcome to our world”

命令 返回值
echo ${string:1:8} elcome t
expr substr “$string” 2 8 elcome t

正則表達式截取子串

使用正則表達式仅仅能抽取string开头处或结尾处的子串。

- expr match $string ‘\($substring\)’

- expr $string : ‘\($substring\)’

命令 返回值
expr match “$another” “[0-9]*” 8
expr match “$another” “\([0-9]*\)” 20091114
expr “$another” : “\([0-9]*\)” 20091114

注意:冒号两側有空格

删除子串

  • ${string#substring}

    删除string开头处与substring匹配的最短子串
  • ${string##substring}

    删除string开头处与substring匹配的最长子串
  • ${string%substring}

    删除string结尾处与substring匹配的最短子串
  • ${string%%substring}
  • 删除string结尾处与substring匹配的最长子串

substring并不是正則表達式

20091114 Reading Hadoop

命令 结果
echo “${another#2*1}” 114 Reading Hadoop
echo “${another##2*1}” 4 Reading Hadoop
echo “${another%a*p}” 20091114 Reading H
echo “${another%%a*p}” 20091114 Re

替换子串

  • ${string/substring/replacement}

    仅替换第一次与substring相匹配的子串
  • ${string//substring/replacement}

    替换全部与substring相匹配的子串
  • ${string/#substring/replacement}

    替换string开头处与substring相匹配的子串
  • ${string/%substring/replacement}

    替换string结尾处与substring相匹配的子串

string=”20001020year20050509month”

命令 结果
echo ${string/200/201} 20101020year20050509month
echo ${string/200/201} 20101020year20150509month
echo ${string/r*h/} 20001020yea
echo ${string/#2000/2010} 20101020year20050509month
echo ${string/%month/MONTH} 20001020year20050509MONTH

shell中的字符串操作的更多相关文章

  1. shell中的字符串操作和数学运算

    字符串操作   变量赋值: 说明:变量值可以用单引号.双引号.或者不加任何引号来赋值给变量 变量名="变量值" 变量名='变量值' 变量名=变量值 例如:str="hel ...

  2. shell中的字符串操作——字符串的切割

    default.yaml {default_baseurl: 'http://10.113.10.68:8082'} test.sh a=`cat default.yaml` t=":&qu ...

  3. linux shell学习(字符串操作)--01

    http://blog.csdn.net/shuanghujushi/article/details/51298672 在bash shell的使用过程中,经常会遇到一些字符串string的操作,下面 ...

  4. (转)Shell中获取字符串长度的七种方法

    Shell中获取字符串长度的七种方法 原文:http://blog.csdn.net/jerry_1126/article/details/51835119 求字符串操作在shell脚本中很常用,下面 ...

  5. shell中截取字符串的方法总结

    shell中截取字符串的方法有很多种, ${expression}一共有9种使用方法. ${parameter:-word} ${parameter:=word} ${parameter:?word} ...

  6. SQL点滴33—SQL中的字符串操作

    原文:SQL点滴33-SQL中的字符串操作 计算字符串长度len()用来计算字符串的长度 select sname ,len(sname) from student 字符串转换为大.小写lower() ...

  7. Python中的字符串操作总结(Python3.6.1版本)

    Python中的字符串操作(Python3.6.1版本) (1)切片操作: str1="hello world!" str1[1:3] <=> 'el'(左闭右开:即是 ...

  8. Oracle中有关字符串操作的语法

    Oracle中有关字符串操作的语法 Oracle提供了丰富的字符串函数 lpad()函数 lpad()函数用于左补全字符串.在某些情况下,预期的字符串为固定长度,而且格式统一,此时可以考虑使用lpad ...

  9. 一句python,一句R︱python中的字符串操作、中文乱码、NaN情况

    一句python,一句R︱python中的字符串操作.中文乱码.NaN情况 先学了R,最近刚刚上手Python,所以想着将python和R结合起来互相对比来更好理解python.最好就是一句pytho ...

随机推荐

  1. 简单机器学习人脸识别工具face-recognition python小试,一行代码实现人脸识别

    摘要: 1行代码实现人脸识别,1. 首先你需要提供一个文件夹,里面是所有你希望系统认识的人的图片.其中每个人一张图片,图片以人的名字命名.2. 接下来,你需要准备另一个文件夹,里面是你要识别的图片.3 ...

  2. Nearest Neighbor Search

    ## Nearest Neighbor Search ## Input file: standard input Output file: standard output Time limit: 1 ...

  3. Redis学习篇(六)之ZSet类型及其操作

    ZADD 作用: 将元素及其分数添加到集合中 语法: ZADD key score membre [score member] 当集合元素已经存在时,再次添加会更新其分数 当score是 +inf 时 ...

  4. android studio 继续汉化 编译项目 菜单

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha Edit Flavors...

  5. FFTW3学习笔记3:FFTW 和 CUFFT 的使用对比

    一.流程 1.使用cufftHandle创建句柄 2.使用cufftPlan1d(),cufftPlan3d(),cufftPlan3d(),cufftPlanMany()对句柄进行配置,主要是配置句 ...

  6. 简单DP+暴力 POJ 1050

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 45915   Accepted: 24282 Desc ...

  7. 内功心法 -- java.util.LinkedList<E> (7)

    写在前面的话:读书破万卷,编码如有神--------------------------------------------------------------------下文主要对java.util ...

  8. CROC 2016 - Elimination Round (Rated Unofficial Edition) A. Amity Assessment 水题

    A. Amity Assessment 题目连接: http://www.codeforces.com/contest/655/problem/A Description Bessie the cow ...

  9. YII 关联查询

    通过外键自己关联自己

  10. git用法资料

    上网看到一篇不错的GIT教程,与大家共享(图片上传实在太麻烦),请见具体地址: http://www.liaoxuefeng.com/wiki/0013739516305929606dd1836124 ...