Examples of string operations in work
Filename= '/home/admin/jobs/cnclickstat/dfsloader/loader.cfg '
#下面是使用shell字符串操作
buname1=${filename#*/jobs/} #去除 '/home/admin/jobs/cnclickstat/dfsloader/loader.cfg ' prefix get ' CnClickstat/DFSLoader/ Loader.cfg '
buname1=${buname1%%/*} #去除 ' cnclickstat/dfsloader/loader.cfg ' suffix gets ' cnclickstat '
echo $buName 1
#下面用awk获取需要的字符串内容
Buname2= ' echo $filename | Awk-f/' {printf ('%s ', $)} ';
Echo $buName 2
#下面使用cut获取需要的字符串内容
Buname3= ' echo $filename | cut-d/F 5 ';
Echo $buName 3
All above can obtain the result: Cnclickstat
String Operation example Ends
String-related operations are often involved in the shell batch process. There are a lot of command statements, such as: awk,sed can do various operations of string. In fact, the Shell has a series of operating symbols, you can achieve similar effects, you know, using the internal operator will omit the start of the external program and so on time, so the speed will be very fast.
First, judge read string value
An expression
Meaning
${var}
Variable var with the same value as $var
${var-default}
If Var is not declared, then use $default as its value *
${var:-default}
If Var is not declared, or if its value is null, then the value is $default
${var=default}
If Var is not declared, then use $default as its value *
${var:=default}
If Var is not declared, or if its value is null, then the value is $default
${var+other}
If Var is declared, then its value is $other, otherwise it is a null string
${var:+other}
If Var is set, the value is $other, otherwise it is a null string
${var? ERR_MSG}
If Var is not declared, then print $err_msg *
${var:? ERR_MSG}
If Var is not set, then print $err_msg *
${!varprefix*}
Match all previously declared variables preceded by Varprefix
${!varprefix@}
Match all previously declared variables preceded by Varprefix
Adding "*" does not mean: Of course, if Var has been set, the 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 also assigns 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@}, you can search for a variable with a variable name prefix character, regardless of whether it is a null value or not.
Second, string operation (length, read, replace)
Get the length
Code:
%x= "ABCD"
#方法一
%expr length $x
4
# method Two
? o $
4
# method Three
%expr "$x": ". *"
4
# Help for expr
# String:regexp anchored pattern Match of REGEXP in STRING
Find substring
Code:
%expr index $x "B"
2
%expr index $x "a"
1
%expr index $x "B"
2
%expr index $x "C"
3
%EXPR index $x "D"
4
Get substring
Code:
# method One
# expr <string> startpos length
%expr substr "$x" 1 3
Abc
%expr substr "$x" 1 5
Abcd
%expr substr "$x" 2 5
Bcd
# method Two
# $
? o $
Bcd
? o $
Cd
? o $
Abcd
? o $
Ab
%pos=1
%len=2
? o $
Bc
Matching regular expressions
Code:
# Print Match length
%expr match $x "."
1
%expr match $x "ABC"
3
%expr match $x "BC"
0
Qiatouquwei of strings
Code:
%x=aabbaarealwwvvww
? o "$"
Aabbaarealwwvv
? o "$"
Aabbaareal
? o "$"
Lwwvvww
? o "$"
Bbaarealwwvvww
Where, # means pinch head, because on the keyboard # on the left of $.
Where% represents% because the keyboard is on the right side of the $.
A single representation minimum match, with two representing the maximum match.
In other words, when matching a variety of scenarios, choose the maximum length of the match or the minimum length.
Substitution of strings
Code:
%x=abcdabcd
? o $ # replaces only one
Bbcdabcd
? o $ # Replaces all
Bbcdbbcd
Can not use RegExp, can only use *? The way files are expanded
Third, performance comparison
In the shell, through awk,sed,expr and so on can be implemented, string above operations. Here's a performance comparison.
[Chengmo@localhost ~] $test = ' C:/windows/boot.ini '
[Chengmo@localhost ~]$ time to I in $ (seq 10000);d oa=${#test};d one;
Real 0m0.173s
User 0m0.139s
SYS 0m0.004s
[Chengmo@localhost ~]$ time to I in $ (seq 10000);d o a=$ (expr length$test);d one;
Real 0m9.734s
User 0m1.628s
Truncating strings like professionals
Although basename and dirname are good tools, there may be times when you need to perform more advanced string "truncation" rather than just standard pathname operations. When you need more convincing, you can take advantage of the variable extension capabilities built into bash. A variable extension with a standard type similar to ${myvar} has been used. But bash itself can perform some handy string truncation. Take a look at these examples:
The first method:
${varible##*string} to intercept the string after the last string from left to right
${varible#*string} to intercept the string after the first string from left to right
${varible%%string*} to intercept the string after the last string from right to left
${varible%string*} to intercept the string after the first string from right to left
"*" is just a wildcard character that you can not
$ myvar=foodforthought.jpg
$ echo ${myvar##*fo}
Rthought.jpg
$ echo ${myvar#*fo}
Odforthought.jpg
In the first example, the ${MYVAR##*FO} is entered. What is the exact meaning of it? Basically, enter the environment variable name in ${}, two # #, then the wildcard character ("*fo"). Then bash gets MYVAR, finds the eldest string that starts at the beginning of the string "Foodforthought.jpg" and matches the wildcard "*fo", and then truncates it from the beginning of the string. It's going to be a bit hard to start understanding and to feel how this particular "# #" option works, let's take a step-by-step look at how bash completes this extension. First, it searches for substrings that match the "*FO" wildcard character at the beginning of "Foodforthought.jpg". The following is a substring of the check:
F
FO matches *FO
Foo
Food
Foodf
FOODFO matches *FO
Foodfor
Foodfort
Foodforth
Foodfortho
Foodforthou
Foodforthoug
Foodforthought
Foodforthought.j
foodforthought.jp
Foodforthought.jpg
After searching for a matching string, you can see that bash finds two matches. It selects the longest match, drops from the beginning of the initial string, and returns the result.
The second variable expansion form shown above looks the same as the first, but it only uses a "#"--and bash executes almost the same process. It looks at the same substring series as the first example, but bash removes the shortest match from the initial string and then returns the result. So, as soon as you find the "fo" substring, it removes "fo" from the string and returns "Odforthought.jpg".
This can be very confusing, and here's a simple way to remember this feature. When searching for the longest match, use # # (because # # is longer than #). When searching for the shortest match, use #. Look, it's not hard to remember! Wait a minute, how do you remember that you should use ' # ' characters to remove from the beginning of the string? Very simple! Notice: On the American keyboard, shift-4 is "$", which is a bash variable extension character. On the keyboard, close to "$" on the left is "#". In this way, you can see that "#" is at the beginning of "$", so (according to our memory method), "#" removes characters from the beginning of the string. You may want to ask: How to remove characters from the end of a string. If we guessed that we used the character ("%") immediately to the right of "$" on the American keyboard, we guessed it. Here are some simple examples that explain how to truncate the end of a string:
$ myfoo= "chickensoup.tar.gz"
$ echo ${myfoo%%.*}
Chickensoup
$ echo ${myfoo%.*}
Chickensoup.tar
As you can see, the% and% percent variable extension options work the same way as # and # #, except that matching wildcard characters are removed from the end of the string. Note: If you want to drop a specific substring from the end, you do not have to use the "*" Character:
Myfood= "Chickensoup"
$ echo ${myfood%%soup}
Chicken
In this case, it is not important to use percent% or% because there can only be one match. Also remember: If you forget to use "#" or "%", look at the 3, 4, and 5 keys on your keyboard, and guess what.
The second method: ${varible:n1:n2}: Intercepts the string between the variable varible from N1 to N2.
You can select a specific substring by using a variable extension of another form, depending on the specific character offset and length. Try entering the following line in bash:
$ exclaim=cowabunga
$ echo ${exclaim:0:3}
Cow
$ echo ${exclaim:3:7}
Abunga
This form of string truncation is simple, with a colon separated to specify the starting character and substring length.
Apply string truncation
Now that we've learned all about truncating strings, let's write a simple, short shell script. Our script will accept a file as an argument and then print: whether the file is a tar file. To determine if it is a tar file, find the mode ". Tar" at the end of the file. As shown below:
Mytar.sh--A simple script
#!/bin/bash
if ["${1##*.}" = "tar"]
Then
Echo This is appears to be a tarball.
Else
Echo at the glance, this does is not appear to be a tarball.
Fi
To run this script, enter it into the file mytar.sh and enter "chmod 755 mytar.sh" to generate the executable file. Then, do the tar file test as follows:
$./mytar.sh Thisfile.tar
This is appears to be a tarball.
$./mytar.sh thatfile.gz
At the glance, this does is not appear to be a tarball.
Good, run successfully, but not very practical. Before making it more useful, look at the "if" statement used above. A Boolean expression is used in the statement. In bash, the "=" comparison operator checks whether strings are equal. In bash, all Boolean expressions are enclosed in square brackets. But what does a Boolean expression actually test? Let's take a look at the left. Based on the previously learned string truncation knowledge, "${1##*.}" Removes the longest "*." Match from the beginning of the string contained in the environment variable "1" and returns the result. This will return all the parts after the last "." In the file. Obviously, if the file ends with ". Tar", the result will be "tar" and the condition is true.
You might think: What is the "1" environment variable at the beginning? It's simple--the first command-line argument that is passed to the script, the second one, and so on.