Bash shell string Truncation

Source: Internet
Author: User
Tags first string

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, retrieve the prefix with the suffix removed.
Ls-al | cut-d "."-f1
For example, get the suffix
Ls-al | cut-d "."-f2


Here stringIt can be viewedHere documentIn additionCOMMAND <$ WORDAnd nothing else, De> $ WORD De> will be extended and sent De> COMMAND De>StdinMedium.

 

1 String = "This is a string of words ."
2
3 read-r-a Words <"$ String"
4 #-a option of the "read" command
5 # + the result values are allocated to each item in the array in order.
6
7 echo "First word in String is: $ {Words [0]}" # This
8 echo "Second word in String is: $ {Words [1]}" # is
9 echo "Third word in String is :$ {Words [2]}" #
10 echo "Fourth word in String is: $ {Words [3]}" # string
11 echo "th word in String is: $ {Words [4]}" #
12 echo "Sixth word in String is: $ {Words [5]}" # words.
13 echo "Seventh word in String is: $ {Words [6]}" # (null)
14 # $ String end.
15
16 # Thanks for Francisco Lobo's suggestion.

Example 17-13. Add text at the beginning of a file


1 #! /Bin/bash
2 # prepend. sh: Add text at the beginning of the file.
3 #
4 # example of the script donated by Kenny Stauffer,
5 # + The author of this article made a few modifications to this script.
6
7
8 E_NOSUCHFILE = 65
9
10 read-p "File:" file # 'read' command. The-p parameter is used to display the prompt.
11 if [! -E "$ file"]
12 then # If the file does not exist, come in.
13 echo "File $ file not found ."
14 exit $ E_NOSUCHFILE
15 fi
16
17 read-p "Title:" title
18 cat-$ file <$ title> $ file. new
19
20 echo "Modified file is $ file. new"
21
22 exit 0
23
24 # below is a section in 'Man bash:
25 # Here String
26 # here document:
27 #
28 # <word
29 #
30 # word is extended and provided to standard input of command.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.