shell 截取字符串(转)
linux中对字符串的处理:
1.字符串分割例如 AAAAA-BBBBBB 按-分割去前后两部分
cut :
[rich@localhost ~]$ str=AAAAA-BBBBBB
[rich@localhost ~]$ echo $str | cut -d "-" -f 1
AAAAA
[rich@localhost ~]$ echo $str | cut -d "-" -f 2
BBBBBB
解释:A | B 将A命令的输出 作为B命令的输入
cut
-d :分隔符
-f : field 分割后的数组索引 只不过是从1开始
如果不存在分割符则返回全部:
[rich@localhost ~]$ echo $str | cut -d "c" -f 1
AAAAA-BBBBBB
expr && cut:
取“-”的索引
[rich@localhost ~]$ index=`expr index "${str}" "-"`
[rich@localhost ~]$ echo $str | cut -c`expr "${index}" + 1`-
BBBBBB
[rich@localhost ~]$ echo $str | cut -c1-`expr ${index} - 1`
AAAAA
cut
-c 截取字符串 索引从1开始
-cA- A到末尾
-cA A位置
-c A-B 索引A-B
-c-A 截取前A个字符
substr:
[rich@localhost ~]$ index=`expr ${index} - 1`
[rich@localhost ~]$ echo `expr substr "${str}" 1 ${index}`
AAAAA
substr str A B
从索引A开始截取B长度的字符串
转自 http://blog.csdn.net/a276202460/article/details/5080551
shell 截取字符串(转)的更多相关文章
- Linux Shell 截取字符串
Linux Shell 截取字符串 shell中截取字符串的方法很多 ${var#*/} ${var##*/} ${var%/*} ${var%%/*} ${var:start:len} ${var: ...
- shell截取字符串的方法
参考文献: linux中shell截取字符串方法总结 [Linux]如何在Shell脚本中计算字符串长度? 截取字符串的方法一共有八种,主要为以下方法 shell中截取字符串的方法有很多中, ${ex ...
- shell截取字符串的8种方法
参考文献: linux中shell截取字符串方法总结 [Linux]如何在Shell脚本中计算字符串长度? 截取字符串的方法一共有八种,主要为以下方法 shell中截取字符串的方法有很多中, ${ex ...
- inux中shell截取字符串方法总结
shell中截取字符串的方法有很多中, ${expression}一共有9种使用方法. ${parameter:-word} ${parameter:=word} ${parameter:?word} ...
- linux中shell截取字符串方法总结
截取字符串的方法一共有八种,主要为以下方法 shell中截取字符串的方法有很多中, ${expression}一共有9种使用方法. ${parameter:-word} ${parameter:=wo ...
- shell截取字符串方法
shell中截取字符串的方法有很多中, ${expression}一共有9种使用方法.${parameter:-word}${parameter:=word}${parameter:?word}${p ...
- shell 截取字符串实例教程
本节内容:shell字符串截取方法 1,去掉字符串最左边的字符 [root@jbxue ~]$ vi test.sh 1 STR="abcd" 2 STR=${STR#" ...
- shell截取字符串
image_tag="pangu-20151021102145\"" 1.用#号截取,符号-右面所有字符串 TMP=${image_tag#*-} echo $TMP 得 ...
- Shell 截取字符串方法
原文链接 方法1 "${varible##*string}" 从左向右截取最后一个string后的字符串 e.g. exampleString="abc//888//ab ...
随机推荐
- Duilib学习之基础(一个SDK程序)
版权声明:本文为灿哥哥http://blog.csdn.net/caoshangpa原创文章,转载请标明出处. https://blog.csdn.net/caoshangpa/article/det ...
- Srvctl命令具体解释(10g)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/lovedieya/article/details/28169481 Srvctl命令 Srvct ...
- Apache Thrift的简单介绍
1.什么是Thrift thrift是一种可伸缩的跨语言服务的发展软件框架.它结合了功能强大的软件堆栈的代码生成引擎,以建设服务.不同开发语言开发的服务可以通过该框架实现通信. thrift是face ...
- v-for指令用法二
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- Keras 可视化 model
参考:https://keras.io/visualization/ error解决参考:http://blog.csdn.net/wangjian1204/article/details/50346 ...
- Python: PS 滤镜-- 极坐标变换到平面坐标
本文用 Python 实现 PS 中的一种滤镜 极坐标变换到平面坐标,具体的算法原理和效果可以参考之前的博客: http://blog.csdn.net/matrix_space/article/de ...
- Linux vSphere SDK for Perl 执行脚本报错
本人在gentoo系统上安装完vSphere for Perl之后,执行/usr/lib/vmware-viperl/app/vm/vminfo.pl脚本. 提示错误如下: Server vers ...
- BZOJ_3489_ A simple rmq problem_KDTree
BZOJ_3489_ A simple rmq problem_KDTree Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这 ...
- [CTSC 2018] 混合果汁
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5343 [算法] 对于每组询问 , 首先二分答案 显然 , 最优策略为优先选择价格低的 ...
- app自动化测试工具robotium
robotium基于instramentation框架,可对app白盒黑盒测试,缺点是测试进程和被测进程需要在一个进程中,不能跨应用 白盒测试时,需要app源代码,在eclipse里新建android ...