Shell Learning Path: Shell Basics Daquan 2

Source: Internet
Author: User
Tags clear screen

Original: Http://note.youdao.com/share/?id=cd2ad6e6d5db2b347f92958efc2bdbc1&type=note

Regular expressions and wildcard characters:

First, Introduction:1. A regular expression is used to match a string in a file that matches a condition .contains matching. grep, awk, sed, and other commands can support regular expressions2. Wildcards are used to match the qualified file name, and the wildcard character isExact Match, LS, find, CP These commands do not support regular expressions, so they can only be matched using the shell's own wildcard character. second, the basic regular expression:grep "Following" Demo.txt". *": matches any character any time"^$": Match blank line"A *": matches a 0 or more times, so you can match all the content"^a": matches lines starting with a"aa*" matches two consecutive lines of a
Metacharacters Role
* The previous character matches 0 or more times
. Match any character except line break
^ Matches the beginning of the line, for example: ^hello matches the row at the beginning of hello.
$ Matches the end of the line, for example: hello$ matches the line ending with Hello.
[] Matches any one of the characters specified in the brackets, matching only one character.
For example: [AOEIU] matches any one of the vowel letters; [0-9] Match any number [a-z][0-9] to match lowercase letters and a two-character string consisting of one digit
[^] Match any character other than the character in brackets, for example: [^0-9] matches any non-numeric character; [^a-z] indicates a character that matches any one non-lowercase letter
\ The transfer character. Used to cancel the meaning of special symbols
\{n\} Indicates that the character before it is knocked out n times, for example: [0-9]\{4\} matches 4 digits; [1][3-8][0-9]\{9\} matches the phone number (the first digit must start with 1 the second digit must be the third digit between 3 and 8 after the number 1 to 9 matches 9 times)
\{n,m\} Indicates that the characters preceding it appear at least n times and appear at most m times. For example: [a-z]\{6,8\} matches lowercase letters 6 to 8 times (bits)
character interception:cut Field Extraction command        Description: The disadvantage is that you cannot use spaces as separators[ [email protected]] #cut [options] File nameOptions:- F Column number: Extract the first few columns- D delimiter: Separates Fu Funglie by specified    I want to extract the users who can log in to the system except root:[Email protected] demo]# cat/etc/passwd | grep/bin/bash | grep-v root | cut-d ":"-F 1
Fengtaotao printf Command (Format output command)    Introduction:[[email protected]] #printf ' output type output format ' outputOutput Type:
%ns: Output string, n is a numeric reference to output several characters
%ni: Output integer. N is a digital reference to output several numbers%m.nf: The output floating-point number, M and n are numbers, which refer to the integer and decimal digits of the output. such as%8.2f represents an output 8-digit number, which has two decimal places, 6-bit integers.     output Format:
\a Output Warning Sound
\b Output backspace key, i.e. backspace key
\f Clear Screen
\ n Line break
\ r Carriage return, which is the ENTER key
\ t The horizontal output backspace key, which is the TAB key
\v The vertical output backspace key, which is the TAB key

For example:

[[Email protected]]# printf "%s\t%s\t%s\t%s\t%s\t%s\n" $ (cat student.txt) to correct output format


awk command: Introduction:[[email protected]] #awk ' condition 1{action 1} Condition 2{action 2} ... ' File namecondition (Pattern)general use of relational expressions as criteriax>10 Determine if the variable x is greater than tenx>=10 greater than or equal tox<=10 less than or equal toactions (Action)formatted outputProcess Control Statements    Example:[email protected]]# awk ' {printf $ "\ t" $6 "\ n"} ' Student.txtThe above command is: the direct action will student.txt in the second column of the format \ t is the tab and the 6th column +\n the output form of the newline    Begin Condition  FS built-in variable modifier delimiterEnd: An action in an end after all the content has finished executingafter all data processing is completed, a AAAAAA data is printed sed command:    Description: SED is a lightweight flow editor that is almost included on all UNIX platforms (including Linux). SED is primarily used to select, replace, delete, and add commands to the data. [ [email protected]] #sed [options] ' [action] ' file nameOptions:- N: The general sed command will output all data to the screen, and if you join this option, only the lines processed by the SED command will be output to the screen. (Show only action content)-e: Allow multiple sed command edits to input data-I : Directly modifies the file that reads the data with the result of the SED modification, rather than the screen output (write)Action:
A\ Append to add one or more rows after the current line. When multiple rows are added, the end of the end of the data is required with "\" for each line, except for the last line.
C\ Line substitution, replacing the original data row with a string after C, replacing multiple rows, with the "\" at the end of each line, except for the last line, to indicate that the data is not finished.
I\ Insert, inserting one or more rows in the current row. When inserting multiple rows, out of the last row, the end of each line needs to be "\" to represent the data not ending
D Delete, delete the specified line
P Prints the line specified by the print output
S Character substitution, replacing another string with one string. The format is "row range s/old string/new string/g" (similar to the replacement format in vim).
Example: 1 Example: 2 Example 3 character processing commands:1. Sorting Commands Sort[ [email protected]] #sort [options] File nameOptions:- F: Ignore case- N: Sort by numeric type, by default using string type sorting- R: Reverse Sort-T: Specifies the delimiter, which is a tab character by default-K n[,m]: Sorts by the specified field range. Starting with the nth field, the M field ends (default to end of line) [[Email protected]]# sort/etc/passwd Sort user Information file[[email protected]] #sort-R/etc/passwd Reverse alphabetical order
Statistics Command WC:[ [email protected]~] #wc [options] File nameOptions:-1: Counting Rows only- W: count only words- M: only statistics characters[[email protected]] #wc/etc/passwod[[email protected]] #wc-l/ETC/PASSWOD[[email protected]] #wc-lm/etc/passwod Condition Judgment:two kinds of judging formats:[email protected]~] #test-e/root/install.log[email protected]~]#[-e/root/install.log]For example: Echo $? To determine whether the previous command was executed correctly, return 0 correctly, or return the corresponding numberFor example 2:[[email protected]]# [-d/root]&& echo ' Yes ' | | echo "No" yes[[email protected]]# [-f/root/install.log] && echo ' Yes ' | | Echo ' No ' yes if the judgment in [] is true then output yes no output no with logic and logic or1. Judge by file type:2. According to the permissions of the file 3. Compare two files for example: 4. Two integers compare 5 strings of judgment 6; Judging from the condition of a lot

Shell Learning Path: Shell Basics Daquan 2

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.