Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write Shell script to quickly remove spaces in a string

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to write a Shell script to quickly remove spaces in a string". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to write a Shell script to quickly remove spaces in a string".

The effect is shown in the following figure, the top half of which is the sample text, and the bottom half is the effect of removing spaces with shell. The third method is used below.

A common problem encountered in string processing using the sed command on UNIX is how to delete the space at the beginning and end of a line.

Here's how sed is implemented, and of course awk can do the same.

1. Delete the first space of the line

The code is as follows:

Sed's / ^ [\ t] * / / g'

Description:

To the left of the first / is s for substitution, that is, replacing spaces with empty spaces.

To the right of the first / is to indicate that the following begins with xx.

Square brackets denote "or", a space or any of the tab. This is the specification of regular expressions.

To the right of the brackets is *, indicating one or more.

There is nothing in between the second and the third, which means empty.

G means to replace the original buffer (buffer). Sed does not directly deal with the source file when dealing with strings. Create a buffer first, but adding g means replacing the original buffer.

Replace one or more ontology strings that begin with spaces or tab with empty characters

2. Delete the space at the end of the line

The code is as follows:

Sed's / [\ t] * $/ / g'

It is slightly different from the above in that the ^ character is removed and the dollar sign is added after it, which means that the string ending with xx is the object.

Note, however, that in KSH, Tab is not\ t but directly into a Tab.

3. Delete all spaces

The code is as follows:

Sed s/ [[: space:]] / / g

Thank you for reading, the above is the content of "how to write Shell scripts to quickly remove spaces in strings". After the study of this article, I believe you have a deeper understanding of how to write Shell scripts to quickly remove spaces in strings, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report