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 implement string manipulation in shell

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

Share

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

Editor to share with you shell how to achieve string operation, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

The code is as follows:

Examples of string operation in work

Filename='/home/admin/jobs/CnClickstat/DFSLoader/loader.cfg'

# the following is to use shell string operation

BuName1=$ {filename#*/jobs/} # remove the'/ home/admin/jobs/CnClickstat/DFSLoader/loader.cfg' prefix to get 'CnClickstat/DFSLoader/loader.cfg'

BuName1=$ {buName1%%/*} # remove the suffix 'CnClickstat/DFSLoader/loader.cfg'' to get 'CnClickstat'

Echo $buName1

# now use awk to get the string content you need.

BuName2= `echo $filename | awk-F /'{printf ("% s", $5)}'`

Echo $buName2

# now use cut to get the required string content

BuName3= `echo $filename | cut-d /-f 5`

Echo $buName3

The above results can be obtained: CnClickstat

End of string operation example

String-related operations are often involved when doing shell batch programs. There are many command statements, such as: awk,sed can do a variety of string operations. In fact, shell built-in a series of operation symbols, can achieve a similar effect, as we all know, the use of internal operators will omit the start of external programs and other time, so the speed will be very fast.

First, judge the read string value

Expression.

Meaning

${var}

The value of the variable var, same as $var

${var-DEFAULT}

If var is not declared, then take $DEFAULT as its value *

${var:-DEFAULT}

If var is not declared, or if its value is empty, then take $DEFAULT as its value *

${var=DEFAULT}

If var is not declared, then take $DEFAULT as its value *

${var:=DEFAULT}

If var is not declared, or if its value is empty, then take $DEFAULT as its value *

${var+OTHER}

If var is declared, its value is $OTHER, otherwise it is a null string

${var:+OTHER}

If var is set, its value is $OTHER, otherwise it is a null string

${var?ERR_MSG}

If var is not declared, print $ERR_MSG *

${var:?ERR_MSG}

If var is not set, print $ERR_MSG *

${! varprefix*}

Matches all previously declared variables that begin with varprefix

${! varprefix@}

Matches all previously declared variables that begin with varprefix

Adding "*" does not mean: of course, if the variable var has been set, its value is $var.

[chengmo@localhost ~] $echo ${abc-'ok'}

Ok

[chengmo@localhost ~] $echo $abc

[chengmo@localhost ~] $echo ${abc='ok'}

Ok

[chengmo@localhost ~] $echo $abc

Ok

If abc does not declare "=", it will also assign a value to abc.

[chengmo@localhost ~] $var1=11;var2=12;var3=

[chengmo@localhost ~] $echo$ {! v @}

Var1 var2 var3

[chengmo@localhost ~] $echo ${! v *}

Var1 var2 var3

${! varprefix*} is similar to ${! varprefix@} in that you can search for defined variables, whether or not they are null, by using the variable name prefix character.

Second, string operation (length, read, replace)

Expression.

Meaning

${# string}

Length of $string

${string:position}

In $string, extract the substring from the location $position

${string:position:length}

In $string, extract a substring of length $length from the location $position

${string#substring}

Delete the shortest substring that matches $string from the beginning of the variable $substring

${string##substring}

From the beginning of the variable $string, delete the substring that matches the longest $substring

${string%substring}

From the end of the variable $string, delete the substring that matches the shortest $substring

${string%%substring}

From the end of the variable $string, delete the substring that matches the longest $substring

${string/substring/replacement}

Use $replacement instead of the first matching $substring

${string//substring/replacement}

Use $replacement instead of all matching $substring

${string/#substring/replacement}

If the prefix of $string matches $substring, then replace the matching $substring with $replacement

${string/%substring/replacement}

If the suffix of $string matches $substring, then replace the matching $substring with $replacement

Note: "* $substring" can be a regular expression.

1. Length

[web97@salewell97 ~] $test='I love china'

[web97@salewell97 ~] $echo ${# test}

twelve

${# variable name} gets the string length

two。 Intercept string

[chengmo@localhost ~] $test='I love china'

[chengmo@localhost ~] $echo ${test:5}

E china

[chengmo@localhost ~] $echo ${test:5:10}

E china

${variable name: start: length} get the substring

3. String deletion

[chengmo@localhost ~] $test='c:/windows/boot.ini'

[chengmo@localhost ~] $echo ${test#/}

C:/windows/boot.ini

[chengmo@localhost ~] $echo ${test#*/}

Windows/boot.ini

[chengmo@localhost ~] $echo ${test##*/}

Boot.ini

[chengmo@localhost ~] $echo ${test%/*}

C:/windows

[chengmo@localhost ~] $echo ${test%%/*}

The ${variable name # substring regular expression} is equipped with substring from the beginning of the string to remove the expression on the match.

The ${variable name% substring regular expression} is equipped with substring from the end of the string, removing the expression on the match.

Note: ${test##*/} and ${test%/*} are the easiest ways to get a file name or directory address, respectively.

4. String substitution

[chengmo@localhost ~] $test='c:/windows/boot.ini'

[chengmo@localhost ~] $echo ${test/\ / /\}

C:\ windows/boot.ini

[chengmo@localhost ~] $echo ${test//\ / /\}

C:\ windows\ boot.ini

${variable / find / replace value} a "/" means to replace the first one, "/" means to replace all, and when it appears in the search: "/" Please add the escape character "\ /".

III. Performance comparison

In shell, it can be done through awk,sed,expr, etc., and the above operations can be done in strings. Let's compare the performance.

[chengmo@localhost ~] $test='c:/windows/boot.ini'

[chengmo@localhost ~] $time for i in $(seq 10000); doa=$ {# test}; done

Real 0m0.173s

User 0m0.139s

Sys 0m0.004s

[chengmo@localhost] $time for i in $(seq 10000); do asides $(expr length$test); done

Real 0m9.734s

User 0m1.628s

The speed difference is hundreds of times, and the performance of calling external commands is very different from that of the built-in operators. In shell programming, try to do it with built-in operators or functions. The result is similar to using awk,sed.

These are all the contents of the article "how to manipulate strings in shell". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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