Bash字符串处理(与Java对照) - 29.字符串翻转(字符串反转、字符串反序)
In Java
StringBuilder.reverse || StringBuffer.reverse
StringBuffer sb = new
StringBuffer(str);
String str2 = sb.reverse().toString();
In Bash
编写一个Bash函数来实现字符串翻转
格式:strrev "$STR"
strrev(){
local src=$1
local len=${#src}
local i
local dst
for ((i=len-1; i>=0; --i)) {
dst="$dst${src:i:1}"
}
echo "$dst"
}
[root@jfht ~]# STR=12345
[root@jfht ~]# strrev "$STR"
54321
[root@jfht ~]#
尝试一个多行文本。
[root@jfht ~]# STR="12345
> hello"
[root@jfht ~]# strrev "$STR"
olleh
54321
[root@jfht ~]#
使用外部命令rev来实现字符串翻转
格式:echo "$STR" | rev
格式:rev <<< "$STR"
注意:rev命令是把每行文本进行翻转。
man rev 写道
NAME
rev - reverse lines of a file
SYNOPSIS
rev [file]
DESCRIPTION
The rev utility copies the specified files to the standard output, reversing the order of characters in every
line. If no files are specified, the standard input is read.
[root@jfht ~]# echo "12345" | rev
54321
[root@jfht ~]# rev <<<"12345"
54321
[root@jfht ~]# STR=12345
[root@jfht ~]# echo "$STR" | rev
54321
[root@jfht ~]# rev <<< "$STR"
54321
[root@jfht ~]#
尝试一个多行文本。
[root@jfht ~]# STR="12345
> hello"
[root@jfht ~]# strrev "$STR"
olleh
54321
[root@jfht ~]# echo "$STR" | rev
54321
olleh
[root@jfht ~]# rev <<<"$STR"
54321
olleh
[root@jfht ~]#
使用rev和tac命令实现字符串翻转
格式:echo "$STR" | tac | rev
格式:echo "$STR" | rev | tac
man tac 写道
NAME
tac - concatenate and print files in reverse
SYNOPSIS
tac [OPTION]... [FILE]...
DESCRIPTION
Write each FILE to standard output, last line first. With no FILE, or when FILE is -, read standard input.
[root@jfht ~]# STR="12345
> hello"
[root@jfht ~]# echo "$STR" | tac | rev
olleh
54321
[root@jfht ~]# echo "$STR" | rev | tac
olleh
54321
[root@jfht ~]#
使用awk命令实现字符串翻转
格式:echo "$STR" | awk -F "" '{for(i=NF;i>0;i--)print $i}'
格式:awk -F "" '{for(i=NF;i>0;i--)print $i}' <<<"$STR"
使用sed命令实现字符串翻转
# 将行中的字符逆序排列,第一个字成为最后一字,……(模拟“rev”)
来自:http://bbs.chinaunix.net/viewthread.php?tid=859821&extra=&page=1
格式:echo "$STR" | sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'
格式:sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//' <<<"$STR"
[root@jfht ~]# echo "12345" | sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'
54321
[root@jfht ~]# STR=12345
[root@jfht ~]# echo "$STR" | sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'
54321
[root@jfht ~]# STR="12345
> hello"
[root@jfht ~]# echo "$STR" | sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'
54321
olleh
[root@jfht ~]# sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//' <<<"$STR"
54321
olleh
[root@jfht ~]#
本文链接:http://codingstandards.iteye.com/blog/1164916
(转载请注明出处)
返回目录:Java程序员的Bash实用指南系列之字符串处理(目录)
上节内容:Bash字符串处理(与Java对照) - 28.去除前后空白
下节内容:Bash字符串处理(与Java对照) - 30.综合实例:自动生成版本号源文件
分享到:
相关推荐
bash-3.1-MSYS-1.0.11-snapshot.tar.bz2 ffmpeg 安装用包!
在centos6或rhel6 操作系统安装bash-completion-2.1-6.el7.noarch.rpm后,重启即生效
-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 查看版本出现: 原因是:没有那个文件或目录,找了很久发现需要...
在Linux下使用sqlplus或者rman时,经常需要调用上次或之前运行过的命令 下载包地址: ... 地址2: ... 1. rpm -ivh rlwrap-0.42-1.el7.x86_64.rpm ...2. 检查包是否安装上 rpm -qa rlwrap ...4. 生效: source .bash_profile
修改redhat 7 bash远程执行任意代码漏洞
bash-3.0-1.aix5.1.ppc.rpm, git-1.7.0.6-1.aix5.1.ppc.rpm, git-svn-1.6.6.2-1.aix5.1.ppc.rpm, mysql-5.1.56-1.aix5.1.ppc.rpm, sudo-1.6.7p5-3.aix5.1.ppc.rpm, unzip-5.51-1.aix5.1.ppc.rpm, vim-common-...
找到centos7 安装的iso中的 Packages的 ncurses-devel-5.9-13.20130511.el7.x86_64.rpm ...vi .bash_profile 添加以下内容 stty erase ^h alias sqlplus="rlwrap sqlplus" alias rman="rlwrap rman
VMware-Workstation-Full-14.1.6-12368378.x86_64.bundle
git-bash.exe
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漏洞补丁 包含文件...
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
修复redhat6 bash远程执行任意代码漏洞CVE-2014-6271
解决:linux -bash: /usr/bin/python: /lib/ld-linux.so.2: bad ELF interpreter
外网下载有点慢,搬到国内来了,使用方式如下 tar -zxvf roundcubemail-1.3.3-complete.tar.gz -C /...echo '0 0 * * * root bash /var/www/roundcube/bin/cleandb.sh >> /dev/null' | sudo tee --append /etc/crontab
wget-1.14-15.el7.x86_64.rpm 解决 -bash:wget command not found
命令补全 Centos6/7或Rhel6/7 安装 bash-completion-2.1-6.el7.noarch.rpm 后重启即生效
执行如下命令查看设备是否存在bash安全漏洞。 #env -i X='() { (a)=>\\' bash -c '/dev/stdout echo vulnerable' 2>/dev/null 判定依据 如果以上命令的输出为以下内容则不合规: vulnerable
cirros-0.3.4-i386-disk.img 用来进行测试虚拟化的磁盘image文件
#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...
postgresql9.5的linux/centos的安装文件,postgresql-9.5.22-1-linux-x64.run