1. What is a regular expression
A regular expression is a pattern (pattern) that is written by a class of characters: Select, Grep,egrep,fgrep
Use regular expressions to describe selection criteria
Regular Expressions: Basic regular expressions, extending regular expressions
Metacharacters: *,? This type of character does not represent the meaning of the character itself, but is used for the description of the additional functionality
Given selection criteria, only rows that match the criteria are displayed
grep: The default support for basic regular expressions;
EGERP: Supports extended regular expressions;
Fgrep: Regular expression metacharacters are not supported, fast searching, all characters as characters themselves
grep: Simple search, global search for basic regular expressions
grep [options] ' pattern ' FILE
# grep ' root '/etc/passwd #显示含有root字符串的行root: X:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologi N
The metacharacters of the basic regular expression:
^: Anchoring the content of the line at the beginning of the condition, using the format "^pattern"
# grep ' ^root '/etc/passwdroot:x:0:0:root:/root:/bin/bash
$: Anchor line end of eligible content, usage format "pattern$"
# grep--color=auto ' sh$ '/etc/passwdroot:x:0:0:root:/root:/bin/bashoracle:x:500:500::/home/oracle:/bin/bashhadoop : X:501:502::/home/hadoop:/bin/bash
^pattern$: Begins with a word and ends with it
^$: Blank Line
.: Matches any single character
*: matches any number of characters immediately preceding it
A*b:ab,aab,acb,b
. *: Matches any character of any length
[]: matches any but one character in the specified range
[^]: matches any single character outside the specified range
\? : matches the character immediately before it 0 or 1 times
A\?b:ab,aab,acb,b
\{m,n\}: Matches the preceding character at least m times, up to N times
A\{1,3\}b:b,ab,aab,aaaab,abc
\{0,n\}: Up to n times, 0-n times
\{m,\}: At least m times
\{m\}: Exact Match m times
\<: Anchor word header,, usage format: \<pattern
\b:\bpattern
\>: Anchor ending, usage format:pattern\>
\b:pattern\b
\<pattern\>: Anchoring words
\ (\): Group, usage format: \ (pattern\)
\ (ab\) \{1,3\}:ab,aab,abb,abab,ababab
\ (ab\). *\1: Front and back
# grep "\ (gentoo\). *\1"/etc/passwdgentoo:x:1001:1001:cao Yuan Lang:/home/gentoo:/bin/bash
Options for grep:
--color=auto
Export grep_color= ' 01;36 ' 01 indicates font color (foreground color), 36 for background
-V: Reverse selection, showing only rows that do not conform to the pattern
-O: Displays only the string that is matched to the pattern, not the positive row
-I: match, character case insensitive
-A N: Displays the rows to which the match is matched, by passing n rows after it
-B N: Displays the row to which it was matched, by passing N rows preceding it
-C N: Displays the rows that match to, passing in the preceding and subsequent N rows
-E: Using extended regular expressions
Cases:
Displays lines in the/proc/meminfo file that begin with a non-differentiated s;
# grep "^[ss]"/proc/meminfo
Displays the line ending with Nologin in/etc/passwd;
# grep "nologin$"/etc/passwd # grep "nologin$"/etc/passwd | Wc-l #显示有多少行
Remove the default shell for/sbin/nologin user list
# grep "nologin$"/etc/passwd | Cut-d:-f1
Remove the default shell's bash with the user name with the smallest user ID number
# grep "/bin/bash$"/etc/passwd | Sort-n-T:-k3 | head-1 | Cut-d:-f1 #-t Specifies the delimiter,-K specifies the first few fields to sort
Lines that display/etc/rc.d/rc.sysinit with #, followed by one or more white space characters, followed by any non-whitespace characters
# grep "^#[[:spaec:]]\{1,\}[^[:space:]]"/etc/rc.d/rc.sysinit
Displays the line in/etc/rc.d/rc.sysinit that conforms to any single character n any character n of any single char: that is, any character followed by a n,n with any length followed by any single character followed by an n
# grep ". N.*.N"/etc/rc.d/rc.sysinit
Displays lines in/etc/rc.d/rc.sysinit that conform to any single character n arbitrary character n of any single character: Any character followed by an arbitrary length of n,n followed by any single character followed by an N. But the character preceded by N is the same as the previous N character.
# grep "\ (. n\). *\1"/etc/rc.d/rc.sysinit
Displays lines that start with one or more whitespace characters in the/boot/grub/grub.conf file
# grep "^#[[:spaec:]]\{1,\}"/boot/grub/grub.conf
Find the 1-digit, or 2-digit number in a file
# grep "\<[0-9]\{1,2\}\>"/etc/rc.d/rc.sysinit # grep-w "[0-9]\{1,2\}"/etc/rc.d/rc.sysinit
Find an integer between 1-255 in the ifconfig command result
Ifconfig | Grep
# Ifconfig | Egerp--color=auto "\< ([1-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \> "
100-199:1[0-9][0-9]
200-249:2[0-4][0-9]
250-255:25[0-5]
Find information about the account of the user whose name is student (must appear at the beginning of the line) on the current system, the file is/etc/passwd
# grep "^student:"/etc/passwd
To extend the regular expression meta-character:
.: Any single character
[]: matches any but one character in the specified range
[^]: matches any single character outside the specified range
*: matches any number of characters immediately preceding it
+: Match the characters in front of it at least 1 times
? : matches the character immediately before it 0 or 1 times
{M,n}: At least m times, up to N times
(): Group, support reference \1,\2
A|b: Two Choose one
\<: Anchor word header,, usage format: \<pattern
\b:\bpattern
\>: Anchor ending, usage format:pattern\>
\b:pattern\b
\<pattern\>: Anchoring words
^: Anchoring the content of the line at the beginning of the condition, using the format "^pattern"
$: Anchor line end of eligible content, usage format "pattern$"
# egrep "c| Cat "/root/test.txtc,cat# egrep" (c| C) at "/root/test.txtcat,cat
Fgrep:fist
Sort: Sorting
-F: Ignore case
-N: Sort by numeric size: default to numeric size in ASCII table
-R: Reverse order
-U: Removes duplicate rows, rows with the same content retain only one
only two lines are exactly the same, and next to each other, they are duplicates.
-r: Random sort
-T: Specify delimiter
-K: Specify to sort by the first number of fields
Uniq
-C: Statistics for each row repeat
-U: Show only rows with no duplicates
-D: Show only rows that have been repeated
# sort Sort.txt | Uniq-c 1 First line. 1 four line. 1 How is it? 2 Second line. 2 Third line.# Sort Sort.txt | Uniq-d Second Line. Third line.# Sort Sort.txt | Uniq-u first line. Four line. How is it?
Text Processing Three musketeers: Grep,sed,awk
2. redirect
Linux I/O redirection, piping
I/O: this is a collectively
Linux provides an I/O redirection capability for applications
The program itself should have the function of input and output
For security reasons, the input and output are standardized, the standard input is the keyboard, the standard output is the monitor (monitor), the error output is also the monitor
Each kernel opens with a file descriptor: file disciptor,fd
Standard input fd:0 standard output fd:1 error output Fd:2
Change its default data stream to another device, called IO redirection
Output redirection:
1>: Overwrite redirect, that is, overwrite the contents of the file 1 can be omitted
SET-C: Avoid overwriting the contents of a file that already exists
Set +c: Turn off the above features
1>>: Append redirect 1 can be omitted
/dev/null: Data black hole, bit bucket
Error redirection:
2>: Overwrite redirect
2>>: Append redirect
">" to ">>" means to append the output to the end of the filename file and create it if the file does not exist.
Simultaneous redirection of standard output and error output:
COMMAND >/path/to/stdout 2>/path/to/error
Standard output and error output directed to a file:
&>
&>>
Command &>/path/to/somewhere or command >/path/to/somewhere 2>&1
Enter redirection:
<:
COMMAND </from/somewhere
<<:here document, creating documents here
Cat >/path/to/somefile << EOF is commonly used to generate documents in scripts
Cat << EOF is typically used to output large pieces of information to the user in a script
3. Pipeline
Pipeline: |
COMMAND1 | COMMAND2 | COMMAND3 |
The last command of the pipeline is executed in the child shell of the current shell.
Multi-Channel output:
Tee
COMMAND1 | Tee/path/to/somefile
# Ifconfig | grep "inet Addr:" | Grep-v ' 127.0.0.1 ' | Cut-d:-f2 | Cut-d '-f1192.168.1.1
Cases:
Count the number of files in the/usr/bin/directory
# Ls/usr/bin | Wc-l
Remove the shell for all users on the current system, requiring each shell to be displayed only once, and displayed sequentially
# cut-d:-f7/etc/passwd | Sort-u | Grep-v "^$"
How do i show the content type of each file in the/var/log directory?
# file/var/log/* or # cd/var/log; File ' Ls/var/log '
Remove line 5th of the/etc/inittab file
# head-5/etc/inittab | Tail-1
Remove the user name and shell of the 9th user in the etc/passwd file, display it to the screen and save it to the/tmp/users.txt file
# tail-9/ETC/PASSWD | head-1 | Cut-d:-f1,7 | Tee/tmp/users.txt
Show all files starting with PA in/etc directory and count them
# ls/etc/pa* | Wc-l
Add alias Cle=clear line to the current user without using the file editor. BASHRC tail
# echo ' Alias Cls=clear ' >> ~/.BASHRC
This article is from the "three elder brother" blog, please be sure to keep this source http://523958392.blog.51cto.com/9871195/1617900
Regular expressions, pipelines, and redirects