您的位置:首页 > 运维架构 > Shell

bash字符串截取

2014-08-06 09:56 253 查看
转载来自http://xpku.blog.163.com/blog/static/239650020098185637674/shell字符串的截取的问题:一、Linux shell 截取字符变量的前8位,有方法如下:1.expr substr “$a” 1 82.echo $a|awk ‘{print substr(,1,8)}’3.echo $a|cut -c1-84.echo $5.expr $a : ‘\(.\\).*’6.echo $a|dd bs=1 count=8 2>/dev/null二、按指定的字符串截取1、第一种方法:${varible##*string} 从左向右截取最后一个string后的字符串${varible#*string}从左向右截取第一个string后的字符串${varible%%string*}从右向左截取最后一个string后的字符串${varible%string*}从右向左截取第一个string后的字符串“*”只是一个通配符可以不要例子:$ MYVAR=foodforthought.jpg$ echo ${MYVAR##*fo}rthought.jpg$ echo ${MYVAR#*fo}odforthought.jpg2、第二种方法:${varible:n1:n2}:截取变量varible从n1到n2之间的字符串。可以根据特定字符偏移和长度,使用另一种形式的变量扩展,来选择特定子字符串。试着在 bash 中输入以下行:$ EXCLAIM=cowabunga$ echo ${EXCLAIM:0:3}cow$ echo ${EXCLAIM:3:7}abunga这种形式的字符串截断非常简便,只需用冒号分开来指定起始字符和子字符串长度。三、按照指定要求分割:比如获取去掉后缀名的前缀ls -al | cut -d “.” -f1比如获取后缀名ls -al | cut -d “.” -f2here string可以看成是here document的一种定制形式. 除了COMMAND <<<$WORD, 就什么都没有了,de<$WORDde<将被扩展并且被送入de<COMMANDde<的stdin中.
1 String="This is a string of words."
2
3 read -r -a Words <<< "$String"
4 #  "read"命令的-a选项
5 #+ 将会把结果值按顺序的分配给数组中的每一项.
6
7 echo "First word in String is:    ${Words[0]}"   # This
8 echo "Second word in String is:   ${Words[1]}"   # is
9 echo "Third word in String is:    ${Words[2]}"   # a
10 echo "Fourth word in String is:   ${Words[3]}"   # string
11 echo "Fifth word in String is:    ${Words[4]}"   # of
12 echo "Sixth word in String is:    ${Words[5]}"   # words.
13 echo "Seventh word in String is:  ${Words[6]}"   # (null)
14                                                  # $String的结尾.
15
16 # 感谢, Francisco Lobo的这个建议.
例子 17-13. 在一个文件的开头添加文本
1 #!/bin/bash
2 # prepend.sh: 在文件的开头添加文本.
3 #
4 #  Kenny Stauffer所捐助的脚本例子,
5 #+ 本文作者对这个脚本进行了少量修改.
6
7
8 E_NOSUCHFILE=65
9
10 read -p "File: " file   #  'read'命令的-p参数用来显示提示符.
11 if [ ! -e "$file" ]
12 then   # 如果这个文件不存在, 那就进来.
13   echo "File $file not found."
14   exit $E_NOSUCHFILE
15 fi
16
17 read -p "Title: " title
18 cat - $file <<<$title > $file.new
19
20 echo "Modified file is $file.new"
21
22 exit 0
23
24 # 下边是'man bash'中的一段:
25 # Here String
26 #  here document的一种变形,形式如下:
27 #
28 #   <<<word
29 #
30 #  word被扩展并且被提供到command的标准输入中.
2016年5月15日   再转载一篇:http://www.cnblogs.com/chengmo/archive/2010/10/02/1841355.html
在做shell批处理程序时候,经常会涉及到字符串相关操作。有很多命令语句,如:awk,sed都可以做字符串各种操作。 其实shell内置一系列操作符号,可以达到类似效果,大家知道,使用内部操作符会省略启动外部程序等时间,因此速度会非常的快。 一、判断读取字符串值
表达式含义
${var}变量var的值,与$var相同
${var-DEFAULT}如果var没有被声明,那么就以$DEFAULT作为其值 *
${var:-DEFAULT}如果var没有被声明,或者其值为空, 那么就以$DEFAULT作为其值 *
${var=DEFAULT}如果var没有被声明,那么就以$DEFAULT作为其值 *
${var:=DEFAULT}如果var没有被声明,或者其值为空, 那么就以$DEFAULT作为其值 *
${var+OTHER}如果var声明了,那么其值就是$OTHER, 否则就为null字符串
${var:+OTHER}如果var被设置了,那么其值就是$OTHER, 否则就为null字符串
${var?ERR_MSG}如果var没被声明,那么就打印$ERR_MSG *
${var:?ERR_MSG}如果var没被设置,那么就打印$ERR_MSG *
${!varprefix*}匹配之前所有以varprefix开头进行声明的变量
${!varprefix@}匹配之前所有以varprefix开头进行声明的变量
加入了“*” 不是意思是: 当然, 如果变量var已经被设置的话, 那么其值就是$var.[chengmo@localhost ~]$ echo ${abc-'ok'}ok[chengmo@localhost ~]$ echo $abc[chengmo@localhost ~]$ echo ${abc='ok'}ok[chengmo@localhost ~]$ echo $abcok 如果abc 没有声明“=" 还会给abc赋值。[chengmo@localhost ~]$ var1=11;var2=12;var3=[chengmo@localhost ~]$ echo ${!v@}var1 var2 var3[chengmo@localhost ~]$ echo ${!v*}var1 var2 var3 ${!varprefix*}与${!varprefix@}相似,可以通过变量名前缀字符,搜索已经定义的变量,无论是否为空值。 二、字符串操作(长度,读取,替换)
表达式含义
${#string}$string的长度
${string:position}在$string中,从位置$position开始提取子串
${string:position:length}在$string中,从位置$position开始提取长度为$length的子串
${string#substring}从变量$string的开头,删除最短匹配$substring的子串
${string##substring}从变量$string的开头,删除最长匹配$substring的子串
${string%substring}从变量$string的结尾,删除最短匹配$substring的子串
${string%%substring}从变量$string的结尾,删除最长匹配$substring的子串
${string/substring/replacement}使用$replacement,来代替第一个匹配的$substring
${string//substring/replacement}使用$replacement,代替所有匹配的$substring
${string/#substring/replacement}如果$string的前缀匹配$substring,那么就用$replacement来代替匹配到的$substring
${string/%substring/replacement}如果$string的后缀匹配$substring,那么就用$replacement来代替匹配到的$substring
说明:"* $substring”可以是一个正则表达式. 1.长度[web97@salewell97 ~]$ test='I love china'[web97@salewell97 ~]$ echo ${#test}12${#变量名}得到字符串长度 2.截取字串[chengmo@localhost ~]$ test='I love china'[chengmo@localhost ~]$ echo ${test:5}e china[chengmo@localhost ~]$ echo ${test:5:10}e china${变量名:起始:长度}得到子字符串 3.字符串删除[chengmo@localhost ~]$ test='c:/windows/boot.ini'[chengmo@localhost ~]$ echo ${test#/}c:/windows/boot.ini[chengmo@localhost ~]$ echo ${test#*/}windows/boot.ini[chengmo@localhost ~]$ echo ${test##*/}boot.ini[chengmo@localhost ~]$ echo ${test%/*}c:/windows[chengmo@localhost ~]$ echo ${test%%/*}${变量名#substring正则表达式}从字符串开头开始配备substring,删除匹配上的表达式。${变量名%substring正则表达式}从字符串结尾开始配备substring,删除匹配上的表达式。注意:${test##*/},${test%/*} 分别是得到文件名,或者目录地址最简单方法。4.字符串替换[chengmo@localhost ~]$ test='c:/windows/boot.ini'[chengmo@localhost ~]$ echo ${test/\//\\}c:\windows/boot.ini[chengmo@localhost ~]$ echo ${test//\//\\}c:\windows\boot.ini ${变量/查找/替换值} 一个“/”表示替换第一个,”//”表示替换所有,当查找中出现了:”/”请加转义符”\/”表示。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: