Linux shell string interception, replacement, deletion, and trim code examples
String interception
Cut the string after the first // from left to right
word=abcd-//master-01://httpab echo ${word#*//} # Output :master-01://httpab
Cut the string after the last // from left to right
Word = abcd - / / master - 01: / / httpab echo ${word# # * / /} # output: httpab
Cut the string after the first // from right to left
Word = abcd - / / master - 01: / / httpab echo ${word % / / *} # output: abcd - / / master - 01:
Cut the string after the last // from right to left
Word = abcd - / / master - 01: / / httpab echo ${word % % / / *} # output: abcd -
Intercept 3 characters after position 0
word=abcd-//master-01://httpab echo ${word:0:3} # Output :abc
Cut from position 1 to the end
word=abcd-//master-01://httpab echo ${word:1} # Output :bcd-//master-01://httpab
Cut from position -1 to the end, that's the last one
Word = abcd - / / master - 01: / / httpab echo ${word: (1)} # output: b
String substitution
From left to right, match the first one and replace // with cd
word=abcd-//master-01://httpab echo ${word/\/\//cd} # Output: abcd-CDmaster-01 ://httpab
Replace all matching // with cd
word=abcd-//master-01://httpab echo ${word//\/\//cd} # Output: abcd-CDmaster-01 :cdhttpab
Prefix matching: matches only characters starting from position 0
word=abcd-//master-01://httpab echo ${word/#ab/cd} # - Not a prefix, echo ${word/#-/cd} # Output :abcd-cdmaster-01://httpab # Output :abcd-//master-01://httpab
Suffix matching. Only the end character is matched
Word = abcd - / / master - 01: / / httpab echo ${word/HTTP * / x, y} % # output: abcd - / / master - 01: / / xy echo ${word/ab/x, y} % # Output :abcd-//master-01://httpxy echo ${word/%ab*/xy} # * appears and matches # from the start output :xy
String deletion
The effect of deleting characters can be achieved by using character interception, substitution and concatenation
Delete the first 3 characters
Word = abcd - / / master - 01: / / httpab echo ${word# * ${word: 3-0}} # output: d - / / master - 01: / / httpab
Delete after 3 characters
Word = abcd - / / master - 01: / / httpab echo ${word % * ${word: (3)}} # output: abcd - / / master - 01: / / HTTP
Delete the first ab
word=abcd-//master-01://httpab echo ${word/ab/} # Output :cd-//master-01://httpab
Delete all ab
Word = abcd - / / master - 01: / / httpab echo ${word / / ab /} # output: CD / / master - 01: / / HTTP
Delete the last ab
Word = abcd - / / master - 01: / / httpabcd echo ${word % ab *} ${word# # * ab} # output: abcd - / / master - 01: / / HTTPCD
String trim
trim can be implemented with xargs
word=" hello wolrd "word= 'echo $word | xargs' echo $word # Output :hello wolrd
Reference blog: / / www.jb51.net/jiaoben/316765sh8.htm
Sum up
To this article about Linux shell string interception, replacement, deletion and trim article is introduced to this, more related Linux shell string interception replacement deletion content please search script home previous articles or continue to browse the following related articles hope that you will support script home in the future!
- Shell script 8 kinds of string interception methods summary
- Detailed method of Shell string interception
- Shell string interception implementation method (very detailed)
- An example of how the Linux Shell intercepts a string
- shell string operations (length, find, replace) detailed
- linux shell string operations (length, find, replace) detailed
Related article
-
25 useful Shell scripts commonly used commands to share
This article mainly introduces 25 useful Shell script commonly used commands, collected by the individual, the need of friends can refer to the next2014-03-03 -
The Shell script is implemented to close redundant tty terminals
This article mainly introduces the Shell script to close the redundant tty terminal, this script applies to centos6 system, please choose to use according to your own server system, need friends can refer to it2014-12-12 -
Bash: / root /. Bashrc: Permission denied
This paper mainly introduced the solution to bash: / root/bashrc: Permission denied problem, in this paper, through the example code very detailed introduced, which has some reference value, interested friends can refer to it2021-10-10 -
Description of the difference between./ and sh
This article mainly introduces the difference between the use of./ and sh in detail, the article introduces very detailed through the example code, which has certain reference value for everyone's study or work, and the friends who need to study together with the small series below2019-09-09 -
The Swiss Army Knife of the Linux Command Line
The Swiss Army knife in this article refers to a simple command that can do what other high-level languages would take a large chunk of code to do2013-07-07 -
Some actions of Linux Bash prompt (Custom Bash prompt)
This article mainly introduces Linux Bash prompt some SAO operation, some can let you customize Bash prompt black technology, need friends can refer to2017-07-07 -
Organize common Linux commands and command abbreviations
This article introduces the common commands and command abbreviations of Linux, which has certain reference learning value for everyone's study or work, and the friends who need to learn together with Xiaobian below2022-06-06 -
Create MySQL database accounts on servers in batches using Shell scripts
The company has hundreds of MySQL instances, and manually logging in to create an account is cumbersome and impractical. So, we wrote a simple shell script to create a batch server mysql account. This article mainly introduces the related knowledge of batch creation of MySQL database account on the server through Shell script, and the friends who need it can refer to it2019-07-07 -
Explain the use of Sed command and regular expression metacharacters
sed is a stream editor. It is a very useful tool for text processing. It works perfectly with regular expressions. This article mainly introduces the use of Sed command with regular expression metacharacters, need friends can refer to the next2017-12-12 -
A one-sentence Shell command shuts down unwanted random startup services
This article mainly introduces a one-sentence Shell command to shut down unnecessary random startup services. Using a one-sentence command in this article, the purpose of optimizing the system can be achieved. The services to be retained can be defined by themselves, and the friends who need them can refer to it2014-12-12
Latest comments