Shell string operation

Source: Internet
Author: User

Shell string operation

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: ftp@mirror.lzu.edu.cn/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: ftp@mirror.lzu.edu.cn/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

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.