Shell learning notes and shell scripts

Source: Internet
Author: User

Shell learning notes and shell scripts
I. What is a regular expression?

Regular Expressions are a syntax rule used to describe character arrangement and matching modes. It is mainly used for string mode segmentation, matching, search and replacement operations.

Ii. Regular Expressions and wildcards1. Regular Expression

It is used to match matching strings in the file. The regular expression is "include matching ". Commands such as grep, awk, and sed support regular expressions.

2. Regular Expression metacharacters

Regular Expressions are String Matching through metacharacters. For details, see: http://www.cnblogs.com/refine1017/p/5011522.html

3. wildcard characters

Used to match the name of a qualified file. The wildcard character is "exact match ". Commands such as ls, find, and cp do not support regular expressions, so they can only be matched using shell wildcards.

4. wildcard characters include

* Match any character

? Match any character

[] Match any character in brackets

3. cut command

The cut command cut bytes, characters, and fields from each line of the file and writes these bytes, characters, and fields to the standard output.

1. Common Parameters

-B:Split in bytes. These byte locations ignore the multi-byte character boundary unless the-n flag is also specified.
-C:Separated by characters.
-D:Custom delimiter. The default Delimiter is tab.
-F:Used with-d to specify the region to display.
-N:Undelimiter multi-byte characters. It is used only with the-B flag.

2. Example 1: print a line of the file separated by a tab
[root@localhost shell]# cat student.txt ID      Name    Gender  Mark1       ming    F       852       zhang   F       703       wang    M       754       li      M       90[root@localhost shell]# cut -f 4 student.txt Mark85707590
3. Example 2: print a line of the csv file
[root@localhost shell]# cat student.csv ID,Name,Gender,Mark1,ming,F,852,zhang,F,703,wang,M,754,li,M,90[root@localhost shell]# cut -d "," -f 4 student.csv Mark85707590
4. Example 3: print the nth character of a string
[root@localhost shell]# echo "abcdef" | cut -c 3c
5. Example 4: extract a text from a Chinese character
[Root @ localhost shell] # echo "Shell programming" | cut-nb 1 S [root @ localhost shell] # echo "Shell programming" | cut-nb 2 h [root @ localhost shell] # echo "Shell programming" | cut-nb 3e [root @ localhost shell] # echo "Shell programming" | cut-nb 4l [root @ localhost shell] # echo "Shell programming "| cut-nb 5l [root @ localhost shell] # echo" Shell programming "| cut-nb 8 [root @ localhost shell] # echo" Shell programming "| cut- nb 11 Process
Iv. printf command 1. Command Format

Printf'output type output format' output content

2. Output type

% Ns:Output string. N indicates the output of several characters. n indicates all characters if n is omitted.

% Ni:The output integer. N refers to the number of output numbers. n is omitted to indicate all numbers.

% M. nf:Output floating point number. M and n are numbers, indicating the number of digits and the number of decimal places in the output. For example, % 8.2f indicates that a total of 8 digits are output, of which 2 digits are small trees and 6 digits are integers.

3. Output Format

\:Output warning sound

\ B:Backspace)

\ F:Clear Screen

\ N:Line feed

\ R:Enter)

\ T:Horizontal output return key

\ V:Vertical output return key

4. Example
[root@localhost ~]# printf '%i %s %i %s %i\n' 1 "+" 2 "=" 31 + 2 = 3[root@localhost ~]# printf '%i-%i-%i %i:%i:%i\n' 2015 12 3 21 56 302015-12-3 21:56:30
5. awk command 1. Command Format

Awk 'condition 1 {Action 1} condition 2 {Action 2}... 'file name

Condition:Relational expressions are generally used as conditions, such as x> 10.

Action:Formatting output and flow control statements

2. Example 1: extract a line of a tab-separated File
[root@localhost shell]# cat student.txt ID      Name    Gender  Mark1       ming    F       852       zhang   F       703       wang    M       754       li      M       90[root@localhost shell]# awk '{print $1 "\t" $4}' student.txt ID      Mark1       852       703       754       90
3. Example 2: Get disk Utilization
[root@localhost shell]# df -hFilesystem            Size  Used Avail Use% Mounted on/dev/sda2              18G  2.4G   14G  15% //dev/sda1             289M   16M  258M   6% /boottmpfs                 411M     0  411M   0% /dev/shm[root@localhost shell]# df -h | grep "sda1" | awk '{print $5}'6%
Vi. sed command

Sed is a lightweight stream editor that covers almost all UNIX platforms (including Linux. Sed is mainly used to select, replace, delete, and add commands for data.

1. Command Format

Sed [Option] '[action]' file name

2. Options

-N:Generally, the sed command outputs all data to the screen. If this option is added, only the rows processed by the sed command are output to the screen.

-E:Multiple sed commands can be applied to input data for editing.

-I:Use the sed Modification result to directly modify the file for reading data, rather than the screen output.

3. Action

A:Append, add one or more rows after the current row

C:Line replacement: replace the original data row with the string following c.

I:Insert: insert one or more rows before the current row.

D:Delete: deletes a specified row.

P:Print and output the specified row

S:String replacement: replace one string with another. The format is "Row range/s/old string/New String/g" (similar to the replacement format in vim)

4. Example
[Root @ localhost shell] # cat student.txt ID Name Gender Mark1 ming F 852 zhang F 703 wang M 754 li M 90

# Test-n parameter [root @ localhost shell] # sed-n '2p' student.txt 1 ming F 85

# Delete the [root @ localhost shell] # sed '2d 'student.txt ID Name Gender Mark2 zhang F 703 wang M 754 li M 90

# Test multiline deletion [root @ localhost shell] # sed '2, 4d 'student.txt ID Name Gender Mark4 li M 90

# Test append [root @ localhost shell] # sed '2a test append' student.txt ID Name Gender Mark1 ming F 85 test append2 zhang F 703 wang M 754 li M 90

# Test insertion of [root @ localhost shell] # sed '2i test insert' student.txt ID Name Gender Marktest insert1 ming F 852 zhang F 703 wang M 754 li M 90

# Replace [root @ localhost shell] # sed '2c test replace 'student.txt ID Name Gender Marktest replace2 zhang F 703 wang M 754 li M 90

# Replace the test content with [root @ localhost shell] # sed '2s/ming/replace/G' student.txt ID Name Gender Mark1 replace F 852 zhang F 703 wang M 754 li M 90

 

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.