Shell string Matching

Source: Internet
Author: User
Question: How does the shell judge a filename to start with a character ~
Solve:
#var = "cn get the length of me"
#$1:parameter 1 to judge the name of the file
Var=$1
Iscn=false

# ${var:0:2} takes the Var substring from the No. 0 character and takes 22 characters

if [[${var:0:2}-eq ' CN ']]
Then
echo Sub is Chinese ${var:0:2}
Else
echo Sub is 中文版 ${var:0:2}
Fi
#function: Judgment
ISCN ()
{
if [[${var:0:2}-eq ' CN ']]
Then
echo "in ISCN"
Iscn=true
Fi
}

#执行function
ISCN

echo the last IsCN $isCN
=================================================================
First, the shell to pick the substring commonly used:
1)
Echo ${string:1:2}
2 echo $string |cut-c 2-3 (2nd to 3rd characters from string)

Second, the processing of strings

Include: Intercept, connect, match, replace, flip ...
Handling of strings:
1, Intercept
Method One:
echo $a |awk ' {print substr (, 1,8)} '
SUBSTR is a child function in awk that intercepts the first argument, starts with the first character, intercepts 8 characters, and, if not enough, complements the second character.
Method Two
Echo $a |cut-b2-8
Cut: Handle the string of standard input
Cut-bn-m: In byte, starting with Nth Byte, taking m
Cut-bn,m: Intercept the n,m byte in byte
Cut-b-n,m: In Byte, intercept 1-n, and the first m
-C: In Charactor
-D: Specifies the separator, the default is tab
-S: No delimeter in the standard input
CUT-F1: Intercepting domain 1th
Method Three
a=123456
Echo ${string:1:2}
Method Four
Intercept the last two bits of a string using SED
echo $test |sed ' s//(. */)/(.. /)$//'
Intercept the first 2 bits of a string
echo $test |sed ' s/^/(. /)/(.*/)//'
Welcome to visit 007 Computer Information
2, compare
There seems to be nothing to compare.
3, connecting
$a $b
Or
$string
4, flip
Method One:
Use Rev command
Method Two:
Writing a 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, matching
Grep
Egrep
Fgrep
6, sort
7, replace
In bash:
%x=abcdabcd
%echo $ # replaces only one
Bbcdabcd
%echo $ # Replaces all
Bbcdbbcd
In SH:
。。
How to replace/
Using SED
Replace all matches
echo $test |sed ' S/xx/yy '
Replace a single match
。。
8, get the length of the string:
Bash among
$
Or
Expr "$VAR": '. * '
9, determine if the string is a number
10, get the number of repetitions of a character in the string
echo $a |tr "x" "N" |wc-l
The resulting results need to be subtracted by 1.
or echo $a |awk-f "x" ' {Print NF-1} '
11, get the number of repetitions of a string in 007 computer information
12, replace all strings in a batch of files
For I in File_list
Todo
VI $i <<-!
: g/xxxx/s//xxxx/g
: Wq
!
Done
13, how to insert a character in the middle of every two characters in a string
Using SED
echo $test |sed ' s/. /&[insert char]/g '
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.