How to remove spaces or specified characters in a string in Shell

How to remove spaces or specified characters in a string in Shell

There are many methods on the Internet that, although correct, do not produce correct results when used. Here is the correct method.

Remove leading spaces

$text=" 123 456 "
# This way of writing ensures the correct result.
text=`echo $text | sed -e 's/^[ \t]*//g'`
# These methods are not tested, please refer to the above for writing.
# Remove trailing spaces sed 's/[ \t]*$//g'
# Delete leading and trailing spaces, but not spaces in between sed -e 's/^[ \t]*//g' -e 's/[ \t]*$//g'
# Remove all spaces in a string sed 's/[[:space:]]//g'

Of course there is an easy way:

# Replace only one text=${text/ /-}
# Replace all text=${text// /-}

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • Shell script reads content from a file line by line in several ways
  • Several examples of using pipelines in Shell scripts
  • Shell script uses for loop to traverse parameters
  • Usage of awk command in Shell script
  • Several methods of string sorting in Shell
  • Several ways to calculate integers in Shell
  • One command lets you understand the common parameters of the read command in the shell
  • Several methods to count the number of words in a string in Shell
  • How to remove leading and trailing spaces in a string in Shell
  • Using getopts in Shell Scripts to Process Multiple Command Line Options

<<:  MySQL slow query log configuration and usage tutorial

>>:  In-depth understanding of the implementation principle of require loader

Recommend

Example of using Vue built-in component keep-alive

Table of contents 1. Usage of keep-alive Example ...

A brief analysis of React's understanding of state

How to define complex components (class component...

MySQL 5.7.23 version installation tutorial and configuration method

It took me three hours to install MySQL myself. E...

The impact of limit on query performance in MySQL

I. Introduction First, let me explain the version...

MySQL REVOKE to delete user permissions

In MySQL, you can use the REVOKE statement to rem...

Tips for making web table frames

<br />Tips for making web table frames. ----...

Basic commands for MySQL database operations

1. Create a database: create data data _name; Two...

How to create a stored procedure in MySQL and add records in a loop

This article uses an example to describe how to c...

Practical method of deleting a row in a MySql table

First, you need to determine which fields or fiel...

How to write beautiful HTML code

What Beautiful HTML Code Looks Like How to write ...

Detailed process analysis of docker deployment of snail cinema system

Environmental Statement Host OS: Cetnos7.9 Minimu...

Implementation of Docker deployment of Tomcat and Web applications

1. Download docker online yum install -y epel-rel...