Delete, replace, and replace variable content-Delete and replace

Source: Internet
Author: User

In addition to directly configuring variables to modify the original content, is there a way to fine-tune the content of variables through simple actions? For example, you can delete, replace, and replace variable content! Yes! We can fine-tune the variable content through several simple small steps! Next, let's try it!

This section describes how to delete a variable.

The variable content can be easily deleted through a few clicks! We can use the content of the PATH variable for testing.

Example 1: Let the lower-case path custom variable be configured with the same content as the path

[Root @ sor_sys ~] # Path =$ {path}
[Root @ sor_sys ~] # Echo $ path
/Usr/local/The ruby-1.8.7-p330/bin :/Usr/Kerberos/sbin:/usr/Kerberos/bin:/Usr/local/sbin:/usr/local/bin

:/Sbin:/bin:/usr/sbin:/usr/bin:/usr/local/jdk1.6.0 _ 05/bin:/usr/local/Maven/bin: /root/bin

:/Usr/local/jdk1.6.0 _ 05/bin:/usr/local/Maven/bin #These three rows are one line!

Example 2: If I do not like Kerberos, how can I display it if I want to delete these two directories?
[root@SOR_SYS ~]#echo ${path#/*kerberos/bin:}
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/jdk1.6.0_05/bin
:/Usr/local/Maven/bin:/root/bin:/usr/local/jdk1.6.0 _ 05/bin:/usr/local/Maven/binThese two rows are one line!

The above example is very interesting! His focus can be explained in the following table:

${variable#/*kerberos/bin:}
The special font above is a keyword! The
${variable#/*kerberos/bin:}
This is the original variable name. For example 2 above, enter the "variable name" of path.
${variable#/*kerberos/bin:}
This is the focus! Indicates "delete from the beginning of the variable content to the right", and only the shortest
${variable#/*kerberos/bin:}
Indicates the part to be deleted. Because # indicates that the part is deleted from the beginning, it is written from the beginning.
It should be noted that we can also use the wildcard * to replace 0 to infinite Multiple Arbitrary characters.
The result of Example 2 above shows that the variable path is deleted as follows:
/usr/local/ruby-1.8.7-p330/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin
:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/jdk1.6.0_05/bin
:/Usr/local/Maven/bin:/root/bin:/usr/local/jdk1.6.0 _ 05/bin: /usr/local/Maven/bin <= these three rows are actually the same line!

Example:

[Root @ sor_sys ~] # Echo $ {path #/* jdk1.6.0 _ 05/bin :}
/Usr/local/Maven/bin:/root/bin:/usr/local/jdk1.6.0 _ 05/bin:/usr/local/Maven/bin this is the focus! Indicates "delete from the beginning of the variable content to the right", and only the shortest

I didn't understand what it meant at the beginning, and I just thought that there were not two JDK directories. Then I tested them and you should understand the results ~~

 

Interesting! Have you understood the # feature? Next let's take a look at Example 3 below!

Example 3: I want to delete all the preceding directories. Only the last directory is retained.
[Root @ sor_sys ~] # Echo $ {path #/*:}
/Usr/Kerberos/sbin:/usr/Kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin

:/Usr/bin:/usr/local/jdk1.6.0 _ 05/bin:/usr/local/Maven/bin:/root/bin

Because one # only deletes the shortest one, you can use the following strikethrough to delete it:

/Usr/local/ruby-1.8.7-p330/bin:/Usr/Kerberos/sbin:/usr/Kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin

:/Usr/bin:/usr/local/jdk1.6.0 _ 05/bin:/usr/local/Maven/bin:/root/bin

[Root @ sor_sys ~] # Echo $ {path ##/*:}
/Root/bin

Hey! After adding one more # To #, it becomes "Delete the longest data 』! That is:

 

/Usr/local/ruby-1.8.7-p330/bin:/usr/Kerberos/sbin:/usr/Kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin: /bin:/usr/sbin

:/Usr/bin:/usr/local/jdk1.6.0 _ 05/bin:/usr/local/Maven/bin:/Root/bin

Very interesting! Isn't it? In the content of the path variable, each directory is separated by a colon ":", so you need to delete the directory from the beginning as data between the slash (/) and colon! But there is more than one colon (:) In path! So # And # represent:

#: Match the "shortest" that replaces the text;

#: Match the "longest" to replace the text

The above mentioned is "delete variable content from the beginning". What if you want to "delete variable content from the beginning? At this time, you need to use the percentage (%) symbol! Let's take a look at Example 4!

Example 4: I want to delete the last directory, that is, the string from: To Bin.

[Root @ sor_sys ~] #Echo $ {PATH %: * bin}
/Usr/local/ruby-1.8.7-p330/bin:/usr/Kerberos/sbin:/usr/Kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin: /bin:/usr/sbin

:/Usr/bin:/usr/local/jdk1.6.0 _ 05/bin:/usr/local/Maven/bin

This % symbol indicates that it is deleted from the last side.! Therefore, the above result is actually from the following:

 

/Usr/local/ruby-1.8.7-p330/bin:/usr/Kerberos/sbin:/usr/Kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin: /bin:/usr/sbin

:/Usr/bin:/usr/local/jdk1.6.0 _ 05/bin:/usr/local/Maven/bin:/Root/bin

Example 5: What if I only want to keep the first directory?

[Root @ sor_sys ~] # Echo $ {PATH %: * bin}
/Usr/local/ruby-1.8.7-p330/bin

Similarly, % indicates the longest matching string, so the result is actually from the following:

/Usr/local/ruby-1.8.7-p330/bin:/Usr/Kerberos/sbin:/usr/Kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin

:/Usr/bin:/usr/local/jdk1.6.0 _ 05/bin:/usr/local/Maven/bin:/root/bin

Because I want to delete the variable content from the front, and the end of the variable content is "/root/bin 』, so you can see that the data I deleted above must eventually be "bin", that is, ": * bin", which * represents the wildcard! As for the meaning of % and %, it is actually similar to # And! Do you understand this?

Example:

Suppose you are root, then your mail variable should be/var/spool/mail/root. Suppose you only want to keep the last file name (Root), and the previous directory name is not needed. How can we use the $ mail variable to achieve this?

A:

The question is actually like this. "/Var/spool/mail/Root, that is, delete all data between two diagonal lines (maximum ). You can do this at this time:

[Root @ sor_sys ~] # Echo $ {mail ##/*/}
Root

On the contrary, if you only want to remove the file name, keep the directory name, that is, "/var/spool/mail/Root"(Minimum ). However, if you do not know what the ending letter is, you can use wildcards to process it, as shown below:

[Root @ sor_sys ~] # Echo $ {mail % /*}
/Var/spool/mail

After learning about the delete function, let's talk about how to replace it! Continue to play Example 6!

Example 6: replace sbin in the variable content of path with sbin: [root @ sor_sys ~] # Echo $ {path/sbin}

/Usr/local/ruby-1.8.7-p330/bin:/usr/Kerberos/Sbin:/Usr/Kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

:/Usr/local/jdk1.6.0 _ 05/bin:/usr/local/Maven/bin:/root/bin

This part is much easier to understand! Key words:The two diagonal linesIn the middle of the two slashes is the old string
Next isNew StringSo the result will show the special font section above!

[Root @ sor_sys ~] # Echo $ {path // sbin/sbin}
/Usr/local/ruby-1.8.7-p330/bin:/usr/Kerberos/Sbin:/Usr/Kerberos/bin:/usr/local/Sbin:/Usr/local/bin :/Sbin:/Bin:/usr/Sbin:/Usr/bin

:/Usr/local/jdk1.6.0 _ 05/bin:/usr/local/Maven/bin:/root/bin

If yesTwo diagonal linesSo that all the content that meets the requirements will be replaced.

We will give a summary of this part:

Variable configuration method

We will give a summary of this part:

Variable Configuration

$ {Variable#Keywords}

$ {Variable##Keywords}

If the variable contentStart from scratchIf the data meets the keywordShortest dataDelete

If the variable contentStart from scratchIf the data meets the keywordLongest dataDelete

$ {Variable%Keywords}

$ {Variable%Keywords}

If the variable contentForward from endIf the data meets the keywordShortest dataDelete

If the variable contentForward from endIf the data meets the keywordLongest dataDelete

$ {Variable/Old string/New String}

$ {Variable//Old string/New String}

If the variable content matches the "Old string 『The first old stringWill be replaced by a new string 』

If the variable content matches the "Old string 『All old stringsWill be replaced by a new string 』

   

 

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.