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 use string interception in shell programming

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

Share

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

This article introduces the knowledge of "how to intercept strings in shell programming". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Gnu Linux shell intercepts the first 8 bits of a character variable. The methods are as follows:

1.expr substr "$a" 1 8

2.echo $a | awk'{print substr (, 1meme 8)}'

3.echo $a | cut-C1-8

4.echo $

5.expr $a:'(.\). *'

6.echo $a | dd bs=1 count=8 2 > / dev/null

Intercept according to the specified string

1. The first method:

${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.

Example:

The code is as follows:

$MYVAR=foodforthought.jpg

$echo ${MYVAR##*fo}

Rthought.jpg

$echo ${MYVAR#*fo}

Odforthought.jpg

2. The second method: ${varible:n1:n2}: intercepts the string of variables varible from N1 to N2.

You can use another form of variable extension to select a specific substring based on a specific character offset and length. Try entering the following line in bash:

The code is as follows:

$EXCLAIM=cowabunga

$echo ${EXCLAIM:0:3}

Cow

$echo ${EXCLAIM:3:7}

Abunga

This form of string truncation is very simple, simply using colons to specify the starting character and substring length.

(3) to divide according to the specified requirements:

Such as getting a suffix.

The code is as follows:

Ls-al | cut-d "."-f2

That's all for "how to intercept strings in shell programming". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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