Split string in Shell

Source: Internet
Author: User

Recently the system has learned shell programming.

See an example in the book, very dick. I thought about it before, but I didn't get the result.

I believe that the students also guessed this example, is a line of string splitting. Students can try it out before they see the answer.

Here I have summed up three ways. For reference only.

1. First of all, of course, the contents of the book:

echo JALSDFJLASJDL | Sed "s/[^\n]/&\n/g"

The output of this example is to separate each character nonalphanumeric into a single line.

SED statement believe that the students are not unfamiliar. The only thing that can be confusing is the use of & symbols. In fact, the combination of this sentence, can also understand. Replaces the smallest unit of the previous match. [^\n] matches any character that is not a newline. The smallest unit is naturally a single character.


2. Use the string extension. The code is as follows:

#!/bin/bashstr= "Love your Jingjing" for i in ' seq ${#str} ' Doecho ${str: $i -1;1}done
In this code, the expansion of curly braces is used. The first ${#str}. Represents the length of Str. The second ${str: $i -1:1} means to take the 1 characters of the i-1 position of the STR string.

So the printing here naturally also prints each character by line. I don't know how to explain why I write $i-1 directly. I thought it would be an error.

3. Use awk. This is also not entirely their own thinking. It is also summarized by reference to the data.

echo "Love Jingjing" | Awk-f "" ' {for (i=1;i<=nf;i++) {print $i}} '

In fact, it's not hard to really understand the code of AWK. The difficulty is that I don't know "" "two double quotes next to each other is no delimiter, so-called No, that is, empty is the delimiter. That's a mouthful.

Understanding this point, the rest of the content can be understood.

is not beginning to associate. Split into two or more how to get.

How to sed: Echo alksfdjals | Sed "s/[^\n]\{2\}/&\n/g"

How to extend the string:

#!/bin/bash-xalai= "Love-Jingjing" for ((i=0; $i <=${#alai};i= $i +2));d o     echo ${alai: $i: 2};d One
This code uses the special use of parentheses, that is, two parentheses attached, you can freely do the operation. I am personally beautiful in the name of the C language form.


Summed up such a few ideas for a few days, and are not independent thinking, are more or less borrowed from others or books above the idea. Although digested. But it's a bit lacking in the spirit of independence.

After reading this article, students have their own different ideas

Split string in Shell

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.