Processing strings in Shell

Source: Internet
Author: User

Original article link

Abstract: string processing in Shell
Processing strings
Including: intercept, connection, match, replace, flip ......
One day accumulated!
String processing:
1. truncate
Method 1:
Echo $ A | awk '{print substr (, 1, 8 )}'
Substr is a subfunction in the awk. It intercepts the first parameter. It starts from the first character and truncates 8 characters in total. If not, it is added to the second character.
Method 2
Echo $ A | cut-b2-8
Cut: process the standard input string
Cut-BN-M: in bytes, starting from n Bytes, take M
Cut-BN, M: the unit is byte. The N and M bytes are intercepted.
Cut-B-n, m: in bytes, intercept 1-N, and the M
-C: the unit is charactor.
-D: Specifies the delimiter. The default Delimiter is tab.
-S: Enable standard input without delimeter
Cut-F1: intercepts 1st Fields
Method 3
A = 123456
Echo $
Method 4
Use sed to extract the last two digits of a string
Echo $ test | SED's // (. */)/(../) $ //'
Truncates the first two digits of a string.
Echo $ test | SED's/^ /(../)/(.*/)//'
2. Comparison
There seems to be nothing to compare.
3. Connection
$ A $ B
Or
$ String
4. Flip
Method 1:
Run the rev command
Method 2:
Script implementation
#! /Usr/bin/awk-F
######################################## ########################
# Description: duplicate Rev in awk
######################################## ########################
{
Revline = ""
For (I = 1; I <= length; I ++)
{
Revline = substr (, I, 1) revline
}
}
End {print revline}
5. Match
Grep
Egrep
Fgrep
6. Sort
7. Replace
Bash:
% X = abcdabcd
% Echo $ # Replace only one
Bbcdabcd
% Echo $ # Replace all
Bbcdbbcd
SH:
??
How to replace/
Use SED
Replace all matches
Echo $ test | SED's/XX/yy'
Replace a single match
??
8. Obtain the length of the string:
Bash
$
Or
Expr "$ Var ":'.*'
9. determines whether the string is a number.
10 to obtain the number of repetitions of a character in the string.
Echo $ A | TR "X" "/N" | WC-l
The result is subtracted from 1.
Or
Echo $ A | awk-F "X" '{print NF-1 }'
11. Obtain the number of repetitions of a string in the string.
12. replace all strings in a batch of Files
For I in file_list
Do
VI $ I <-!
: G/xxxx/S // xxxx/g
: WQ
!
Done
13. How to insert one character between every two characters in a string
Use SED
Echo $ test | SED's/../& [insert char]/G'

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.