Cut of shell string intercept

Source: Internet
Author: User

The Cut command is the next powerful tool in the shell, it is a processing object with each behavior, can be a row of strings for a variety of pattern matching cutting operations, you can also operate on a well-organized text, let's say cut this command:

The Cut command consists of 3 positioning methods:

(1) Location of Byte, with-B option

(2) The positioning of the character, with the-C option

(3) domain, with type-f


Let's take a look at the specific implementation:

650) this.width=650; "Title=" 6yn@h_f8fwvru (aqk}3 (c87.png "src=" http://s4.51cto.com/wyfs02/M02/82/DB/ Wkiol1dithqsbmjkaabl0zklud0430.png "alt=" Wkiol1dithqsbmjkaabl0zklud0430.png "/>

Let's talk about the above phenomenon:

Let's first look at our current path with the PWD command. Then we want to extract the 4th byte of this string in the current path, using the command shown here:

PWD | Cut-b 4

We can see that after executing this command, the output of the fourth byte m;

So we see here, if we want to extract which byte, directly after the-B option to add the corresponding number.


What if we want to extract the first 3,4,5,8 byte directly? We can use the second command:

PWD | Cut-b 3-5,8

After executing this command, we can see that the output of Omex corresponds to the 3,4,5,8 byte respectively.

Note:If the-B option is used for the cut command, then when we execute the command, the cut will sort all the positions of-B from small to large and then extract them. For example, the final two orders, we put the command inside the "3-5,8" replaced with "8,3-5", the output is the same result.


Let's take a look again:

650) this.width=650; "title=" Ltl3d~ww$yasi2mdbyyjnnb.png "src=" http://s3.51cto.com/wyfs02/M00/82/DB/ Wkiol1divd7qvcqraaajwfvxnzo267.png "alt=" Wkiol1divd7qvcqraaajwfvxnzo267.png "/>

From what we can see,--3 and 3--there is a difference. 3 represents the contents of all this line from the first byte to the third byte, and 3-for all after the third byte.


Let's take a look at the differences between the following two command options:

650) this.width=650; "title=" D2 ' wj}qe%3prxtl6f3 ' 8F) j.png "src=" http://s5.51cto.com/wyfs02/M00/82/DB/ Wkiol1diwa6yjjx5aaakpac543o125.png "alt=" Wkiol1diwa6yjjx5aaakpac543o125.png "/>

We now see that the "-B" and "-C" options in these two commands are not the same, but they have the same result, so let's look at the following and see where they differ:

650) this.width=650, "title=") (WM0) l@4z$ok{_} ' ot3.png ' src= ' http://s2.51cto.com/wyfs02/M00/82/DC/ Wkiom1diwubr2po7aaaovcggnhq254.png "alt=" Wkiom1diwubr2po7aaaovcggnhq254.png "/>

As we can see in the previous diagram, when we are going to intercept single-byte characters, there is no difference between-B and-C, but when we extract Chinese? For example, the above picture, we wrote a file.c file, there are 3 lines of Chinese characters, we then use the above two commands, we can see the difference: when we use the "-C" option, the output is normal, but "-B" is in bytes, so the output is garbled. So there's a difference between the two commands.


Let's talk about the concept of "domain":

Why is there a "domain" extraction? Because the-B and-C mentioned above can only extract information in a fixed-format document, and the information in the non-fixed format is useless, this is where the domain comes in handy.

We know that in some files there will be a lot of colons, and colons play a very important role in each line of the file, and colons are used to separate each item. And we cut the command to provide such a method of extraction, that is, set the "spacer", and then set to extract the first domain on the line, we specifically look at the following experiment:

We can get the default path for the current bash by executing the following command at the terminal:

650) this.width=650, "title=" doqwyc9n349epu~e$ @PKE8M. png "src=" http://s3.51cto.com/wyfs02/M00/82/DC/ Wkiom1dixthsrt8qaaauhrcc_qm798.png "alt=" Wkiom1dixthsrt8qaaauhrcc_qm798.png "/>

As we can see, each of these paths is separated by a ":", and if we now want to get the path between the first colon and the second colon, we can easily use the Cut command to help us implement, we enter the following command at the terminal:

650) this.width=650; "Title=" [R]cx65u@_ (Uq_6g0gt@}wl.png "src=" http://s4.51cto.com/wyfs02/M01/82/DC/ Wkiom1dixussro49aaas34ay8sw082.png "alt=" Wkiom1dixussro49aaas34ay8sw082.png "/>

From the results we can see that we extracted the path to the second domain we wanted. Let's explain the above command:-D and ': ' Together, a line string entered by the pipeline is delimited by a field, and each delimiter (:) and the preceding string are called a domain. The-f option is followed by the field that we want to extract. So the command cut-d ': '-F 2 indicates the field before the second delimiter is output.

If we execute the following command:

650) this.width=650; "title=" K%jdqzgjzk5$u2wq755h257.png "src=" http://s1.51cto.com/wyfs02/M00/82/DB/ Wkiol1diynnadmjraaazmpa6uag871.png "alt=" Wkiol1diynnadmjraaazmpa6uag871.png "/>

Enter the above two very similar commands, I think you can clearly see their differences, the first output is the second field and the fourth field, and the second command output is the second to fourth field (as we see in the figure in different colors).

Let's take a look at the operation of Cut formatted text:

650) this.width=650; "title=" Zs{_ ' E (mw9kiq1l%fx%e] ' n.png "src=" http://s2.51cto.com/wyfs02/M00/82/DB/wKioL1diy_KS_ Mqbaaboyoftf58828.png "alt=" Wkiol1diy_ks_mqbaaboyoftf58828.png "/>

From the above we can see that at this time formatted a relatively neat part of the text, then we want to remove the previous declare-x, what should we do? In fact, we have said above:

650) this.width=650; "title="}3s@u849fx~vw5uu24@c4) u.png "src=" http://s2.51cto.com/wyfs02/M02/82/DB/ Wkiol1dizo6igcliaabxay-snqg052.png "alt=" Wkiol1dizo6igcliaabxay-snqg052.png "/>

where "12-" represents all characters after the output from the 12th character. In fact, there is no difference between the operation of the string and what we said above.


Above is the basic usage of the cut command that you now know.























Cut of shell string intercept

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.