在做關(guān)鍵詞清洗過程中,需要將一類不符合某個字結(jié)尾的詞過濾出來,思路是把這一批詞按最后一個字排序,于是想到了先把這些詞反轉(zhuǎn)一下,如把12345轉(zhuǎn)為54321,好像以前在夜息的文章里看過用shell可以實現(xiàn),就百度了一下,找到幾個可行的解決方法,現(xiàn)記錄一下。
shell實現(xiàn)字符串反轉(zhuǎn),一句命令搞定!
代碼如下:
cat keywords.txt|while read line;do echo $line|rev;done
命令的:
代碼如下:
echo 12345|rev
54321
python 的:
代碼如下:
echo 12345|python -c ‘print raw_input()[::-1]'
sed 的:
代碼如下:
echo 12345|sed ‘//n/!G;s//(./)/(.*/n/)/&/2/1/;//D;s/.//'
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 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
新聞熱點
疑難解答