Shell string interception; shell string interception problem: 1. Linuxshell intercepts the first eight digits of the character variable, the method is as follows: 1. exprsubstr $ a182.echo $ a | awk & amp; lsquo; {printsubstr (, 1, 8)} & amp; rsquo; 3. echo $ a | cut-c1-84...
Shell string processing
Interception;
Shell string truncation problem:
1. Linux shell intercepts the first eight characters of the character variable by using the following methods:
1. expr substr "$ a" 1 8
2. echo $ a | awk '{print substr (, 1, 8 )}'
3. echo $ a | cut-c1-8
4. echo $
5. expr $ :'\(.\\).*'
6. echo $ a | dd bs = 1 count = 8 2>/dev/null
2. truncate by specified string
1. Method 1:
$ {Varible # * string} captures the string after the last string from left to right.
$ {Varible # * string} captures the string after the first string from left to right.
$ {Varible % string *} captures the string after the last string from the right to the left
$ {Varible % string *} captures the string after the first string from the right to the left.
"*" Is only a wildcard.
Example:
$ Myvar1_foodforthought.jpg
$ Echo $ {MYVAR # * fo}
Rthought.jpg
$ Echo $ {MYVAR # * fo}
Odforthought.jpg
2. Method 2: $ {varible: n1: n2}: Intercept the string from n1 to n2.
You can use another form of variable extension to select a special character string based on the specific character offset and length. Enter the following lines in bash:
$ EXCLAIM = cowabunga
$ Echo $ {EXCLAIM: 0: 3}
Cow
$ Echo $ {EXCLAIM: 3: 7}
Abunga
String truncation in this form is very simple. you only need to use a colon to separate the Start character and the length of the substring.
3. split according to specified requirements:
For example, get the suffix
Ls-al | cut-d "."-f2
This article is from LinuxLeon"