Implement string reversal method in shell share _linux shell

Source: Internet
Author: User

In the process of doing keyword cleaning, you need to filter out a class of words that do not conform to the end of a word, the idea is to put this batch of words according to the last word sort, so think of the first of these words to reverse, such as the 12345 to 54321, as if in the night interest in the article looked at the shell can be realized, Baidu a bit, Find out a few possible solutions, now record.

Shell implementation string inversion, a command to fix!

Copy Code code as follows:

Cat Keywords.txt|while Read Line;do echo $line |rev;done

Ordered by:

Copy Code code as follows:

Echo 12345|rev

54321

Python's:

Copy Code code as follows:

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

SED's:

Copy Code code as follows:

Echo 12345|sed '/\n/! G;s/\ (. \) \ (. *\n\)/&\2\1/;//d;s/.//'

Awk's:

Copy Code code as follows:

echo 12345|awk ' begin{fs= '}{for (a=nf;a>0;a–) printf ("%s", a==1? $a "\ n": $a)} '

For pure bash:

Copy Code code as follows:

Echo 12345| {read;for (i=${#REPLY};i>0;i–)) do Echo-n "${reply:$[i-1]:1}";d One;echo;

C's:

Copy Code code as follows:

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]= ' "; &&/a "12345"; Rm-f a

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.