手机
当前位置:查字典教程网 >脚本专栏 >linuxshell >利用perl、python、php、shell、sed、awk、c 实现字符串的翻转
利用perl、python、php、shell、sed、awk、c 实现字符串的翻转
摘要:原题:Q:有a.txt文件,里面内容如下1234569abcABCabc要求使用awk打印出以下结果987654321cbaCBAcbaA:...

原题:

Q:有a.txt文件,里面内容如下 1234569 abcABCabc 要求使用awk打印出以下结果 987654321 cbaCBAcba

A:

shell :[root@vps tmp]# rev a.txt 9654321 cbaCBAcba

perl : [root@vps tmp]# perl -nle ‘print scalar reverse $_;' a.txt 9654321 cbaCBAcba

awk: [root@vps tmp]# awk ‘{num=split($0,arr,”");for(i=num;i>0;i–){printf arr[i];if(i==1){printf “n”}}}' a.txt 9654321 cbaCBAcba

php: [root@vps tmp]# php -r ‘$fp=fopen(“a.txt”,”r”);while(!feof($fp)){ echo strrev(fgets($fp,999));} echo “n”;;' 9654321 cbaCBAcba

sed: [root@vps tmp]# sed ‘/n/!G;s/(.)(.*n)/&21/;//D;s/.//' a.txt 9654321 cbaCBAcba

python: (方法之一)

>>> arr='hello'

>>> arr[::-1]

‘olleh'

(方法二)

>>> str='hello'

>>> tmp=”

>>> for s in reversed(str):

… tmp+=s

>>> print tmp

olleh

reverse.h

#ifndef _reverse_h

int getLine(char s[],int limit);

void reverse(char s[]);

#endif

int getLine(char s[],int limit)

{

int c,i ;

for(i =0; ireverse.c

#include "stdio.h"

#include "/Users/chenqing/tmp/reverse.h"

#define MAXLINE 1000

/*

reverse a string use c langusge

*/

int main(void)

{

int len;

char line[MAXLINE];

if((len = getLine(line,MAXLINE)) > 0){

reverse(line);

printf("%s",line);

}

return 0;

}

gcc reverse.c -o reverse

Qing:tmp chenqing$ echo "ChinaCache" |./reverse

ehcaCanihC上面就是利用这些实现我们在终端命令行下实现字符串翻转的例子,哈哈,其实还是perl实现起来比较爽啊。

【利用perl、python、php、shell、sed、awk、c 实现字符串的翻转】相关文章:

shell字符串操作详解

使用iconv批量改变文件编码的shell脚本

linux使用select实现精确定时器详解

linux shell实现转换输入日期的格式

在Shell中分割字符串的例子

学习linux常用命令(推荐)

Shell字符串截取的详细方法

Shell脚本实现查找字符串中某字符最后出现的位置

linux shell实现判断输入的数字是否为合理的浮点数

linux下实现ftp自动备份shell脚本

精品推荐
分类导航