“字符串处理”的版本间差异
		
		
		
		
		
		跳到导航
		跳到搜索
		
				
		
		
	
 (以“*Linux shell 截取字符变量的前8位,有方法如下: #expr substr “$a” 1 8 #echo $a|awk ‘{print substr(,1,8)}’ #echo $a|cut -c1-8 #echo $ #expr $a ...”为内容创建页面)  | 
				无编辑摘要  | 
				||
| 第14行: | 第14行: | ||
#${varible:n1:n2}:截取变量varible从n1到n2之间的字符串。  | 
  #${varible:n1:n2}:截取变量varible从n1到n2之间的字符串。  | 
||
:“*”只是一个通配符可以不要  | 
  :“*”只是一个通配符可以不要  | 
||
*字符替换例:将文件名中的空格替换成一个下划线  | 
|||
 #!/bin/bash  | 
|||
 # 名称:delspace.sh  | 
|||
 # 功能:将文件名中的空格替换成一个下划线。  | 
|||
 # 用法1、批量替换:find * -type d -exec /path/delspace.sh {} \;  | 
|||
 # 用法2、替换当前目录:/path/delspace.sh ./  | 
|||
 if test -z "$1"; then  | 
|||
 echo "String is null or empty"  | 
|||
  else  | 
|||
 echo "$1"  | 
|||
 cd "$1"  | 
|||
 ls | while read i; do  | 
|||
 j=`echo $i|tr -s ' ' '_'`  | 
|||
 if test "$i" != "$j"; then   | 
|||
 mv "$i" "$j"  | 
|||
 fi  | 
|||
 done  | 
|||
 fi  | 
|||
2013年12月19日 (四) 02:30的版本
- Linux shell 截取字符变量的前8位,有方法如下:
 
- expr substr “$a” 1 8
 - echo $a|awk ‘{print substr(,1,8)}’
 - echo $a|cut -c1-8
 - echo $
 - expr $a : ‘\(.\\).*’
 - echo $a|dd bs=1 count=8 2>/dev/null
 
- 按指定的字符串截取
 
- ${varible##*string} 从左向右截取最后一个string后的字符串
 - ${varible#*string}从左向右截取第一个string后的字符串
 - ${varible%%string*}从右向左截取最后一个string后的字符串
 - ${varible%string*}从右向左截取第一个string后的字符串
 - ${varible:n1:n2}:截取变量varible从n1到n2之间的字符串。
 
- “*”只是一个通配符可以不要
 
- 字符替换例:将文件名中的空格替换成一个下划线
 
 
#!/bin/bash
# 名称:delspace.sh
# 功能:将文件名中的空格替换成一个下划线。
# 用法1、批量替换:find * -type d -exec /path/delspace.sh {} \;
# 用法2、替换当前目录:/path/delspace.sh ./
if test -z "$1"; then
echo "String is null or empty"
 else
echo "$1"
cd "$1"
ls | while read i; do
j=`echo $i|tr -s ' ' '_'`
if test "$i" != "$j"; then 
mv "$i" "$j"
fi
done
fi