shell 字符串反转方法的例子

发布时间:2020-11-28编辑:脚本学堂
有关shell 字符串反转方法,并介绍了其它多种语言的实现方法,包括awk、python、bash、c语言等实现字符串反转的方法,需要的朋友参考下。

如何实现把一批关键词按最后一个字排序,可以先把这些词反转,即字符串反转,例如把12345转为54321。

shell实现字符串反转
 

cat keywords.txt|while read line;do echo $line|rev;done

shell命令:
 

echo 12345|rev
54321

python进行字符串反转:
 

echo 12345|python -c ‘print raw_input()[::-1]'

sed命令 实现字符串反转:
 

echo 12345|sed ‘/n/!G;s/(.)(.*n)/&21/;//D;s/.//'

linuxjishu/13830.html target=_blank class=infotextkey>awk命令 实现字符串反转:
 

echo 12345|awk ‘BEGIN{FS=””}{for(a=NF;a>0;a–)printf(“%s”,a==1?$a”n”:$a)}'

纯bash命令行脚本
 

echo 12345|{ read;for((i=${#REPLY};i>0;i–))do echo -n “${REPLY:$[i-1]:1}”;done;echo; };

用c来进行字符串反转:
 

gcc -o a -O2 -x c <(cat <<! #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc,char *argv[]) { if(argc != 2) { printf("%s reverse lines of a stringn",argv[0]); exit(1); } int i=0; char *p; p=argv[1]; i=strlen(argv[1])-1; for(i;i>=0;i--) { printf("%s%s",&p[i],(i==0)?"n":""); p[i]=''; } })&& ./a "12345" ;rm -f a