欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Shell中實(shí)現(xiàn)字符串反轉(zhuǎn)方法分享

 更新時(shí)間:2014年12月01日 09:40:16   投稿:junjie  
這篇文章主要介紹了Shell中實(shí)現(xiàn)字符串反轉(zhuǎn)方法分享,本文同時(shí)提供了多種語言的實(shí)現(xiàn)方法,如awk、python、bash、C語言等,需要的朋友可以參考下

在做關(guān)鍵詞清洗過程中,需要將一類不符合某個(gè)字結(jié)尾的詞過濾出來,思路是把這一批詞按最后一個(gè)字排序,于是想到了先把這些詞反轉(zhuǎn)一下,如把12345轉(zhuǎn)為54321,好像以前在夜息的文章里看過用shell可以實(shí)現(xiàn),就百度了一下,找到幾個(gè)可行的解決方法,現(xiàn)記錄一下。

shell實(shí)現(xiàn)字符串反轉(zhuǎn),一句命令搞定!

復(fù)制代碼 代碼如下:

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

命令的:

復(fù)制代碼 代碼如下:

echo 12345|rev

54321

python 的:

復(fù)制代碼 代碼如下:

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

sed 的:

復(fù)制代碼 代碼如下:

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

awk 的:

復(fù)制代碼 代碼如下:

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

純 bash 的:

復(fù)制代碼 代碼如下:

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

c 的:

復(fù)制代碼 代碼如下:

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 string\n",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]='\0'; } })&& ./a "12345" ;rm -f a

相關(guān)文章

最新評(píng)論