- 浏览: 4733560 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
bzhao:
你也应该可以这样:(not tested)./rbtunnel ...
在Bash脚本中怎么关闭文件描述符? -
bzhao:
如果有A进程原代码情况下,通过如下调用,把他的子进程继承关闭则 ...
在Bash脚本中怎么关闭文件描述符? -
Master-Gao:
楼主咋没分析下源码呢?
我使用过的Linux命令之dirname - 截取给定路径的目录部分 -
jiedushi:
tail -F 就可以吧
Linux下实时跟踪log4j日志文件的bash脚本 - 增强了tail -f的功能 -
java_is_new:
新手学习了,就是不明白为一个网卡配多个ip有什么用
我使用过的Linux命令之ifconfig - 网络配置命令
Bash字符串处理(与Java对照) - 19.查找字符的位置
In Java
String.indexOf & String.lastIndexOf
int indexOf(int ch)
返回指定字符在此字符串中第一次出现处的索引。
int indexOf(int ch, int fromIndex)
从指定的索引开始搜索,返回在此字符串中第一次出现指定字符处的索引。
int lastIndexOf(int ch)
返回最后一次出现的指定字符在此字符串中的索引。
int lastIndexOf(int ch, int fromIndex)
从指定的索引处开始进行后向搜索,返回最后一次出现的指定字符在此字符串中的索引。
StringUtils.indexOf & StringUtils.indexOfAny & StringUtils.indexOfIgnoreCase & StringUtils.lastIndexOf
在 org.apache.commons.lang.StringUtils 中提供了很多查找字符索引的方法,包括正向和反向。
static int indexOf(String str, char searchChar)
Finds the first index within a String, handling null.
static int indexOf(String str, char searchChar, int startPos)
Finds the first index within a String from a start position, handling null.
static int lastIndexOf(String str, char searchChar)
Finds the last index within a String, handling null.
static int lastIndexOf(String str, char searchChar, int startPos)
Finds the last index within a String from a start position, handling null.
在 org.apache.commons.lang.StringUtils 中还提供了查找任意字符出现的位置的方法。
static int indexOfAny(String str, char[] searchChars)
Search a String to find the first index of any character in the given set of characters.
static int indexOfAny(String str, String searchChars)
Search a String to find the first index of any character in the given set of characters.
static int indexOfAnyBut(String str, char[] searchChars)
Search a String to find the first index of any character not in the given set of characters.
static int indexOfAnyBut(String str, String searchChars)
Search a String to find the first index of any character not in the given set of characters.
In Bash
使用遍历字符的方式来查找字符的位置
函数:strchr <str> <ch>
如果找到,打印字符的位置,从0开始计数,退出码为0;否则打印-1,退出码为1
strchr(){ local i for ((i=0; i<${#1}; ++i)) do if [ "${1:i:1}" == "$2" ]; then echo $i return 0 fi done echo -1 return 1 }
[root@web ~]# STR=123456789
[root@web ~]# CH=6
[root@web ~]# strchr "$STR" "$CH"
5
[root@web ~]# echo $?
0
[root@web ~]# CH=a
[root@web ~]# strchr "$STR" "$CH"
-1
[root@web ~]# echo $?
1
[root@web ~]#
用expr index来查找字符的位置
格式:expr index "$STR" "$CHARS"
在STR中查找CHARS中的任何字符(而不是子串),打印第一个位置。
注意:返回的下标是从1开始的,0表示没有找到。
不完全对应于Java的indexOf方法。倒是与C++ STL string的find_first_of相似。
index in STRING where any CHARS is found, or 0
[root@jfht ~]# STR="Hello World"
[root@jfht ~]# SUB="l"
[root@jfht ~]# expr index "$STR" "$SUB"
3
[root@jfht ~]# SUB="not found"
# 注意,expr index并不能用能查找子串的位置,而是该字符串中任何字符首次出现的位置
[root@jfht ~]# expr index "$STR" "$SUB"
5
用awk index来查找字符出现的位置
格式1:awk -v "STR=$STR" -v "CH=$CH" '{print index(STR,CH)}' <<<""
格式2:echo | awk -v "STR=$STR" -v "CH=$CH" '{print index(STR,CH)}'
因为awk默认会从标准输入读取数据,所以必须进行输入的重定向。
此方法不仅可以查询字符的出现位置,也可以查询子串的出现位置。但不能查找任意字符的出现位置。
注意:索引位置从1开始计数,0表示没有找到。
implies that character indices start at one.)
[root@web ~]# STR=123456789
[root@web ~]# CH=6
[root@web ~]# awk -v "STR=$STR" -v "CH=$CH" '{print index(STR,CH)}' <<<""
6
[root@web ~]# echo | awk -v "STR=$STR" -v "CH=$CH" '{print index(STR,CH)}'
6
[root@web ~]#
本文链接:http://codingstandards.iteye.com/blog/1198917 (转载请注明出处)
返回目录:Java程序员的Bash实用指南系列之字符串处理(目录)
上节内容:Bash字符串处理(与Java对照) - 18.格式化字符串
下节内容:Bash字符串处理(与Java对照) - 20.查找子串的位置
发表评论
-
Bash字符串处理(与Java对照) - 22.判断字符串是否数字串
2011-10-25 09:25 5426Bash字符串处理(与Java对照) - 22.判断字符串是否 ... -
Bash字符串处理(与Java对照) - 21.字符串正则匹配
2011-10-24 09:07 11010Bash字符串处理(与Java对照) - 21.字符串正则匹配 ... -
Bash字符串处理(与Java对照) - 20.查找子串的位置
2011-10-19 09:14 6735Bash字符串处理(与Java对照) - 20.查找子串的位置 ... -
Bash字符串处理(与Java对照) - 18.格式化字符串
2011-10-17 09:18 4928Bash字符串处理(与Java对照) - 18.格式化字符串 ... -
Bash字符串处理(与Java对照) - 17.判断是否以另外的字符串结尾
2011-10-09 08:58 6930Bash字符串处理(与Java对照) - 17.判断是否以另外 ... -
Bash字符串处理(与Java对照) - 16.判断是否以另外的字符串开头
2011-10-08 09:17 8301Bash字符串处理(与Java对照) - 16.判断是否以另外 ... -
Bash字符串处理(与Java对照) - 15.计算子串出现的次数
2011-09-28 09:37 3369Bash字符串处理(与Java对照) - 15.计算子串出现的 ... -
Bash字符串处理(与Java对照) - 14.判断是否包含另外的字符串(多达6种方法)
2011-09-27 13:22 8269Bash字符串处理(与Java对照) - 14.判断是否包含另 ... -
Bash字符串处理(与Java对照) - 13.字符串数组连接(以指定分隔符合并)
2011-09-26 09:19 5119Bash字符串处理(与Java对照) - 13.字符串数组连接 ... -
Bash字符串处理(与Java对照) - 12.字符串连接
2011-09-23 09:08 6315Bash字符串处理(与Java对照) - 12.字符串连接 ... -
Bash字符串处理(与Java对照) - 11.比较两个字符串大小(字典顺序、数值比较)
2011-09-21 09:31 5647Bash字符串处理(与Java对照) - 11.比较两个字符串 ... -
Bash字符串处理(与Java对照) - 10.判断两个字符串是否相等(不等)
2011-09-20 09:16 6844Bash字符串处理(与Java对照) - 10.判断两个字符串 ... -
Bash字符串处理(与Java对照) - 9.获取字符串指定位置的字符、遍历字符串中的字符
2011-09-19 09:13 3692Bash字符串处理(与Java对照) - 9.获取字符串指定位 ... -
Bash字符串处理(与Java对照) - 8.计算字符串长度
2011-09-16 09:20 5595Bash字符串处理(与Java对照) - 8.计算字符串长度 ... -
Bash字符串处理(与Java对照) - 7.字符串与默认值
2011-09-15 09:20 3895Bash字符串处理(与Java对照) - 7.字符串与默认值 ... -
Bash字符串处理(与Java对照) - 6.判断字符串是否为空(不为空)
2011-09-14 09:20 7158Bash字符串处理(与Java对照) - 6.判断字符串是否为 ... -
Bash字符串处理(与Java对照) - 5.字符串输入(读取字符串)
2011-09-13 09:28 3886Bash字符串处理(与Java对照) - 5.字符串输入(读取 ... -
Bash字符串处理(与Java对照) - 4.字符串输出
2011-09-08 09:30 3699Bash字符串处理(与Java对照) - 4.字符串输出 I ... -
Bash字符串处理(与Java对照) - 3.给(字符串)变量赋值
2011-09-07 09:29 6838Bash字符串处理(与Java ... -
Bash字符串处理(与Java对照) - 2.字符串的表示方式(字符串常量)
2011-09-06 09:18 6082Bash字符串处理(与Java ...
相关推荐
bash-3.1-MSYS-1.0.11-snapshot.tar.bz2 ffmpeg 安装用包!
在centos6或rhel6 操作系统安装bash-completion-2.1-6.el7.noarch.rpm后,重启即生效
离线安装包,测试可用
libguestfs-bash-completion-1.40.2-28.module_el8.5.0+821+97472045.noarch(1).rpm官方离线安装包,亲测可用
openjdk-18.0.1.1(openjdk-18.0.1.1_linux-x64_bin.tar.gz)适用于Linux x64 系统:
-bash: /usr/local/jdk/jdk1.8.0_181/bin/java: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory 安装完后 java -version 查看版本出现: 原因是:没有那个文件或目录,找了很久发现需要...
git-bash.exe
修改redhat 7 bash远程执行任意代码漏洞
bash-4.1.2-9.el6_2.i686.rpm是centos工具类。
在Linux下使用sqlplus或者rman时,经常需要调用上次或之前运行过的命令 下载包地址: ... 地址2: ... 1. rpm -ivh rlwrap-0.42-1.el7.x86_64.rpm ...2. 检查包是否安装上 rpm -qa rlwrap ...4. 生效: source .bash_profile
redhat bash漏洞补丁包含文件:bash-4.1.2-15.el6_5.2.i686.rpmbash-4.1.2-15.el6_5.2.x86_64.rpmbash-3.0-27.el4.i386.rpmbash-3.0-27.el4.x86_64.rpmbash-3.2-33.el5_11.4.i386.r... redhat bash漏洞补丁 包含文件...
修复redhat6 bash远程执行任意代码漏洞CVE-2014-6271
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
命令补全 Centos6/7或Rhel6/7 安装 bash-completion-2.1-6.el7.noarch.rpm 后重启即生效
网上有很多bash补丁下载,但都是x86_64架构的,针对i686的下载很少,bash-4.1.2-15.el6_5.2.src.rpm 也很难下载,希望上传的这个bash-4.1.2-15.el6_5.2.i686.rpm对你有用。
#tar zxvf bash-4.4.tar.gz #cd bash-4.4 #./configure (如果centos7编译失败,请先安装#yum install gcc) #make #make install 添加环境变量 #echo‘ export PATH=/usr/local/bin:$PATH‘ >>/etc/profile #source...
离线安装包,亲测可用
执行如下命令查看设备是否存在bash安全漏洞。 #env -i X='() { (a)=>\\' bash -c '/dev/stdout echo vulnerable' 2>/dev/null 判定依据 如果以上命令的输出为以下内容则不合规: vulnerable
.git-completion.bash