Flip _linux Shell Using Perl, Python, PHP, Shell, SED, awk, c to implement strings

Source: Internet
Author: User
Tags vps

Original title:

Q: There are a.txt files, which are as follows 1234569 ABCABCABC requires awk to print the following results 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\)/&\2\1/;//d;s/.//' a.txt 9654321 CBACBACBA

Python: (One of the methods)

>>> arr= ' Hello '
>>> Arr[::-1]
' Olleh '

(Method II)

>>> 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 above is the use of these implementations we are in the terminal command line to achieve a string flip example, haha, in fact, or Perl to achieve a better ah.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.