Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use string interception in shell programming

Shulou Source: shulou.com Published: 2022-06-03 09:21:56 09月28日 Update

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!

Tags: Characters strings code variables programming content forms methods more knowledge length practicality simplicity learning and then in between examples colons just suffixes. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei macOS Xiaomi Shulou Tech Info Shulou Technology