Linux shell string interception, replacement, deletion, and trim code sample _linux shell_ Script Home

Linux shell string interception, replacement, deletion, and trim code examples

Updated: Mar 01, 2024 14:54:24 by aabond
In Shell programming, intercept, replace, delete string is a very common operation, this article mainly introduces you about Linux shell string intercept, replace, delete and trim related information, the article through the code is very detailed, the need of friends can refer to the next

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!

Related article

  • 25个好用的Shell脚本常用命令分享

    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 next
    2014-03-03
  • Shell脚本实现关闭多余的tty终端

    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 it
    2014-12-12
  • 解决bash:/root/.bashrc:Permission denied的问题

    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 it
    2021-10-10
  • ./ 和 sh 的使用区别详解

    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 below
    2019-09-09
  • Linux命令行里的“瑞士军刀”

    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 do
    2013-07-07
  • Linux Bash 提示符的一些骚操作(自定义 Bash 提示符)

    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 to
    2017-07-07
  • Linux常用命令与命令缩写整理

    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 below
    2022-06-06
  • 通过Shell脚本批量创建服务器上的MySQL数据库账号

    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 it
    2019-07-07
  • 详解Sed命令的用法与正则表达式元字符

    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 next
    2017-12-12
  • 一句话Shell命令关闭不需要的随机启动服务

    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 it
    2014-12-12

Latest comments