Shell string operation

Source: Internet
Author: User
1. The length of the string, for example:
* $ Var = "Get the length of me"
$ Echo $ {# var}
20
* $ Expr length "$ Var" # note that double quotation marks are used.
20
* $ Echo $ var | awk '{printf ("% d", length ($0 ))}'
20
* $ Echo-N $ var | WC-C # echo: The line break is automatically output each time the output is made.-N indicates that line break is deployed.
20
 
2. Obtain the number of certain characters in a string, such
* $ Echo $ var | tr-cd g | WC-C
2
$ Echo-N $ var | SED's/[^ g] // G' | WC-C
2
$ Echo-N $ var | SED's/[^ gt] // G' | WC-C
5
 
3. count the number of words
* $ Echo $ var | WC-W
 
4. the array data structure provided by bash, which is based on numbers, is the same as that of the C language starting from 0:
* $ Var = "Get length of me"
$ Var_arr = ($ var) # store the VaR string as an array in var_arr. It is separated by spaces by default.
$ Echo $ {var_arr [0] }$ {var_arr [1] }$ {var_arr [2] }$ {var_arr [3] }$ {var_arr [4]}
Get the length of me
$ Echo $ {var_arr [@]} # @ indicates the entire string
Get the length of me
$ Echo $ {var_arr [*]} # * indicates the entire string
Get the length of me
$ Echo $ {# var_arr [@]} # obtain the number of elements in the entire character array.
5
$ Echo $ {# var_arr [0]} # returns the number of characters in a string.
3
$ Var_arr [5] = "new_element" # assign a value directly to the array element
$ Echo $ {var_arr [@]}
Get the length of me new_element
$ Echo $ {# var_arr [@]}
6
* $ For I in $ var; do echo $ I ""; done;
Get the length of me
 
5. arrays in awk:
* Echo $ var | awk '{printf ("% d % s \ n", split ($0, var_arr, ""), var_arr [1])}'
Split splits the string by space, stores it in var_arr, and returns the length of the array. Note that the subscript of the first element is 1 instead of 0.
* Awk divides a row into several fields by space and uses $1, $2, $3... for reference. $0 indicates the whole row, and NF indicates the total number of fields in the row.
* Echo $ var | awk '{split ($0, var_arr, ""); For (I in var_arr) printf ("% s", var_arr [I]);}'
In the for loop, the variable of the loop is subscript.
* Echo $ var | awk '{split ($0, var_arr, ""); Delete var_arr [1]}'
Delete the first array element
 
6. Obtain the substring
* $ Var = "Get length of me"
$ Echo $ {var: 0: 3}
Get
$ Echo $ {VAR (-2)} # reverse direction
Me
$ Echo 'expr substr "$ Var" 5 3'
The
$ Echo $ var | awk '{printf ("% s \ n", substr ($, 6)}' # select 6 characters from the ninth position
Length
* Use cut to retrieve substrings
$ Echo $ var | cut-d ""-F 5
Me
* $ Echo $ var | SED's/[A-Z] * // G'
Get
Delete space + String
$ Echo $ var | SED's/[A-Z] * // G'
Me
Delete string + Space
 
7. Matching for substrings
* $ Echo $ {var % *} # Start from the rightmost side and delete all characters on the rightmost side of the space
Get
* $ Echo $ {var % *} # Delete All characters on the right of the first space from the rightmost side
Get the length
* $ Echo $ {var ###*} # Delete All characters on the left of the rightmost space from the leftmost corner.
Me
* $ Echo $ {var #*} # Delete All characters on the left of the first space from the leftmost corner.
The length of me
 
8. Sed supports row-based printing. Remember to use tr to replace spaces with row numbers.
* $ Echo $ var | TR "\ n" | sed-N 5 p
Me
* $ Echo $ var | TR "\ n" | sed-N 1 p
Get
 
9. tr to get the substring
* $ Echo $ var | tr-d ""
Getthelengthofme
* $ Echo $ var | tr-cd "[A-Z]" # Keep Only letters
Getthelengthofme
 
10. head and tail
* $ Echo "abcdefghijk" | head-C 4
ABCD
* $ Echo "abcdefghijk" | tail-C 4
Hijk
 
11. query strings
* $ Var = "Get the length of me"
$ Expr Index "$ Var" t
3
* $ Echo $ var | awk '{printf ("% d \ n", match ($0, ""));}'
5
 
12. substring replacement
* $ Var = "Get the length of me"
$ Echo $ {var // _} # Replace the first space with an underscore
Get_the length of me
$ Echo $ {var // _} # Replace all spaces with underscores
Get_the_length_of_me
$ Echo $ var | awk '{sub ("", "_", $0); printf ("% s \ n", $0 );}'
Get_the length of me
$ Echo $ var | awk '{gsub ("", "_", $0); printf ("% s \ n", $0 );}'
Get_the_length_of_me
$ Echo $ var | SED's //_/'
Get_the length of me
$ Echo $ var | TR """_"
Get_the_length_of_me
$ Echo $ var | TR "[A-Z]" [A-Z]"
Get the length of me
 
13. TAC will display the text content upside down, just as the name is different from cat,
The function is the opposite of cat.
 
14. Insert a substring
* $ Var = "Get the length of me"
$ Echo $ {var // _} # Add _ before the first space _
Get _ the length of me
$ Echo $ {var // _} # Add _ before all spaces _
Get _ The _ length _ of _ me
$ Echo $ {var //_}
Get _ the length of me
$ Echo $ {var // _} # Add _ after all spaces _
Get _ The _ length _ of _ me
 
* $ Echo $ var | SED's/\ (\)/\ 1 _/'# Add _ after the first space _
Get _ the length of me
$ Echo $ var | SED's/\ (\)/\ 1 _/G'
Get _ The _ length _ of _ me
$ Echo $ var | SED's/\ ([A-Z] * \) \ ([A-Z] * \) /\ 2 \ 1/'# change the get and the location
The get length of me
 
15. delete a substring
* $ Var = "Get the length of me"
$ Echo $ {var ///}
Getthelengthofme
 
* $ Echo $ var | awk '{gsub ("", "", $0); printf ("% s", $0 );}'
Getthelengthofme
 
* $ Echo $ var | SED's // G'
Getthelengthofme
 
* $ Echo $ var | tr-d ""
Getthelengthofme
 
16. Use the test command to compare substrings
 
17. Sort substrings
* $ Echo $ var | TR "\ n" | sort # Positive Sorting
Get
Length
Me
Of
The
$ Echo $ var | TR "\ n" | sort-R
The
Of
Me
Length
Get
* $ Cat data.txt | TR "\ n" | sort-n
 
18. hexadecimal conversion
* $ Echo "IBASE = 10; obase = 16; 10" | BC
A
 
19. Regular Expression Processing URL
* $ Url = "ftp: // anonymous: [email protected]/software/scim-1.4.7.tar.gz"
$ Echo $ URL | grep "ftp: // [A-Z] *: [A-Z] * @ [a-z0-9 \./-] *" # determine the validity of the URL
FTP: // anonymous: [email protected]/software/scim-1.4.7.tar.gz
* $ Echo "$ URL" "$ (expr Index" $ URL ":)" | awk '{printf ("% s \ n", substr ($, $2-1);} '# intercept protocol
FTP
$ Echo $ {URL % :*}
FTP
* $ Echo $ {URL ### * @} | cut-d "/"-F 1 # capture domain names
2.16.lzu.edu.cn
$ TMP =$ {URL ##* @}; echo $ {TMP % /*}
2.16.lzu.edu.cn
* $ TMP =$ {URL ##* @}; echo $ {TMP %/*} # truncate path
2.16.lzu.edu.cn/software
* $ Echo $ {URL ##*/} # capture the file name
Scim-1.4.7.tar.gz
* $ Echo $ URL | SED's/. * [0-9]. // G' # specifies the object type.
Tar.gz
 
20. Sed and awk match the rows in the file
* $ Sed-N 7,9 P readme # print the README7-9 line
 
21. Process formatted text
* $ CAT/etc/passwd | cut-d ":"-F # truncate the user name and group in the/etc/passwd file
* $ CAT/etc/group | cut-d ":"-F # truncate the group name and group ID in the/etc/group file
 
22. File Association operations
* $ Join-O 1.1, 2.1-T ":"-1 4-2 3/etc/passwd/etc/group
# The join command is used to connect two files, similar to the connection between two tables in the database. -T specifies the delimiter,
-1 4-2 3: Specify the group ID according to the fourth column of the First file and the third column of the Second file.

Connection.-O 1.1 2.1 indicates that only the first column of the First file and the first column of the Second file are output.


References

================

Shell programming example -- by Falcon

Shell string operation

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.