And cutting a string interception

Creative Commons License Creative Commons

Problem
when using Linux Shell perform various operation and maintenance tasks, when it comes to determining, test conditions and other related operations, often require the relevant command output filter, the extracted character string to meet the requirements.
This case requires familiarity with a string of common processing operations, do the following exercises:
Reference PPT demonstration operation, complete interception substring, replace, etc.
renamed batch script according to the class, write an improved version renfilex.sh : can batch edit the current directory all files with extensions of the front extension modified / rear positional parameters provided by $ 1, $ 2
embodiment
three uses substring taken:
v a r : Start beginning Place Put : long degree e x p r s in b s t r " {Var: Start position: length} expr substr & quot; var" starting position length
echo v a r c in t b Start beginning d i r n a m e " " b a s e n a m e " " var | cut -b start position - end position of the path split: take directory location: dirname & quot; string & quot; substantially take the name of the document: the basename & quot; string & quot; substring alternative two ways: to replace only the first matching results: {var/ Old / new new}
replace all of the matching results: v a r / / o l d / n e w {Var // old / new} string break off both ends: from left to right, delete the shortest match: {# variable namekeyword}
from left to right, the longest match deleted:Katex parse error: Expected '}',variable name} # # * keywords from right to left, the shortest match ...{variable name% keyword
}
from right to left, delete the longest match : variable name %% {$ keywords
}
step
to achieve this case the following procedure is required.
*

Step a: intercepting string

1) a method using {} Expression format: {var:Start position: length}
define a variable SCHOOL, and confirmed its string length:

[root@svr5 ~]# SCHOOL="Tarena IT Group."
[root@svr5 ~]# echo ${#SCHOOL}
16  										//包括16个字符(含空格)

When taken with $ {}, the start position may be omitted, a character from the first cut starts when omitted. For example, the following can be taken before the start six characters from the left:

[root@svr5 ~]# echo ${SCHOOL::6}
Tarena
或者
[root@svr5 ~]# echo ${SCHOOL:0:6}
Tarena

When using the embodiment taken $ {} string, the start position is zero (the number and array subscripts similar).
Therefore, if a start capturing six characters from the starting position, it would become like this:

[root@svr5 ~]# echo ${SCHOOL:1:6}
arena 							//最后的空格未显示出来,实际为“arena ”

Application example: extracting file / etc / fstab MD5SUM the checksum string exclude unrelated text. Related operations and results are as follows:

[root@svr5 ~]# md5sum /etc/fstab  				//直接查看MD5SUM校验值
eef0254e6049a411dc30db864c0ee6ea  /etc/fstab
[root@svr5 ~]# MD5STR=$(md5sum /etc/fstab) 		//保存到变量
[root@svr5 ~]# echo ${MD5STR::32}  		//截取前32个字符(MD5值的固定长度)
eef0254e6049a411dc30db864c0ee6ea

2) Method II using expr substr
format: expr substr "$ var" length of start position
is also an example in the foregoing variables SCHOOL, confirmed the original value:

[root@svr5 ~]# echo $SCHOOL
Tarena IT Group.
[root@svr5 ~]# echo ${#SCHOOL}
16

When using a string expr substr interception, the starting number starting from 1, to be noted that the $ {} distinguished.
Taken from the left of the first six characters SCHOOL variables:

[root@svr5 ~]# expr substr "$SCHOOL" 1 6
Tarena

Note here that, since the value SCHOOL variable contains spaces, so it should be called double quotes, otherwise will complain:

[root@svr5 ~]# expr substr $SCHOOL 1 6
expr: 语法错误

SCHOOL variable taken from the left side of the character 11-16:

[root@svr5 ~]# expr substr "$SCHOOL" 11 16
Group.

Application examples: or extract file / etc / fstab of MD5SUM checksum string exclude unrelated text. When using expr substr, then the correlation operation and results are as follows:

[root@svr5 ~]# md5sum /etc/fstab  				//确认MD5SUM校验值
eef0254e6049a411dc30db864c0ee6ea  /etc/fstab
[root@svr5 ~]# MD5STR=$(md5sum /etc/fstab) 		//保存到变量
[root@svr5 ~]# expr substr "$MD5STR" 1 32  		//截取前32个字符
eef0254e6049a411dc30db864c0ee6ea

3) three ways, using tools cut segmentation
format: echo $ var | cut -b start position - end position
Options -b represented by character bytes taken, wherein the start position, end position can be omitted. When the initial position is omitted, considered from the start of the first character (also numbered starting with 1, and similar expr), when the end position is omitted, taken to the last considered.
Also to the front SCHOOL variables, for example, to confirm the original value:

[root@svr5 ~]# echo $SCHOOL
Tarena IT Group.
[root@svr5 ~]# echo ${#SCHOOL}
16

Taken from the left of the first six characters, do the following:

[root@svr5 ~]# echo $SCHOOL | cut -b 1-6
Tarena

or

[root@svr5 ~]# echo $SCHOOL | cut -b -6
Tarena

Interception 11-16 characters:

[root@svr5 ~]# echo $SCHOOL | cut -b 11-16
Group.

Taken from the end of the eighth character:

[root@svr5 ~]# echo $SCHOOL | cut -b 8-
IT Group.

Taken only a single character, such as the first nine characters:

[root@svr5 ~]# echo $SCHOOL | cut -b 9
T

If the start position, end position while omitting, and then direct the echo nothing different variable values:

[root@svr5 ~]# echo $SCHOOL | cut -b -
Tarena IT Group.

Application examples: or extract file / etc / fstab of MD5SUM checksum string exclude unrelated text. The use of a cut tool by pipelining directly related to the operation and results are as follows:

[root@svr5 ~]# md5sum /etc/fstab  				//确认MD5SUM校验值
eef0254e6049a411dc30db864c0ee6ea  /etc/fstab
[root@svr5 ~]# md5sum /etc/fstab | cut -b -32	//截取前32个字符
eef0254e6049a411dc30db864c0ee6ea

Guess you like

Origin blog.csdn.net/weixin_44774638/article/details/91851043