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