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

Unix string interception

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

1. Use the method that comes with unix:

${varible##*string} intercepts the string after the last string from left to right

${varible#*string} intercepts the string after the first string from left to right

${varible%%string*} intercepts the string after the last string from right to left

${varible%string*} intercepts the string after the first string from right to left

"*" is just a wildcard. You don't have to.

$test1=123456.txt

$echo ${test1%.txt}

123456

$echo ${test1#*23}

456.txt

$test2=$ {test1%.txt} '.rst'

$echo $test2

123456.rst

2. Use the cut command

$test1=123456.txt

$echo $test1 | cut-f 1-d.

123456

I have a slight question here, why can't you assign this value to another variable? For example:

$test2=echo $test1 | cut-f 1-d.

Ksh: 123456.txt: not found.

$echo test2=$test1 | cut-f 1-d.

Test2=123456

$echo $test2

Test2 has no value here, why?

I suddenly thought of the backquotation marks in shell. The one above the TAB key, the part enclosed by the backquotation marks, will be executed first, and the following test has been done:

$test2= `echo $test1 | cut-f 1-d. `

$echo $test2

123456

$test2= `echo $test1 | cut-f 1-d. ``.rst'

$echo $test2

123456.rst

Success!

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

Servers

Wechat

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

12
Report