linux-Regular Expression-awk-sed use method

Source: Internet
Author: User

• Regular Expressions
SED
awk


Regular expressions
Use a single string to describe and match a series of strings that conform to a grammar rule.
1. Features:
(1) Flexibility, logic and functionality are very strong;
(2) The complex control of strings can be achieved quickly and in a very simple way.
(3) It is difficult to understand the people who have just contacted
2. Applied object: Text
3. Matching mode
Number of matches:
. Match any single character
*: Match its preceding character any time (>=0)
\?: matches the preceding character 1 or 0
\{m,n\}: Matches the preceding character at least m times, up to N times (n>m)
can have no upper limit, but must have a lower limit
Location Matching:
^: Beginning of line, any content after this character must appear at the beginning of the line
$: End of line, any content preceding this character must appear at the end of the row
^$: Blank Line
\&LT: Any character following it must appear as the word header
\&gt: Any character in front of it must be out of the tail of the word
\<\>: Exact match
\<root\>: Find root word
Grouping: Back references
\ (love\). *\1: Refers to the first opening parenthesis and the contents of the corresponding closing parenthesis

Regular expressions are divided into two categories:
1. Basic Regular Expressions
2. Extending the regular expression

Using grep
-V does not match
-N Display Line number
-A N: Displays the found result and displays the following N rows
-B N: Displays the results found and displays the first N rows
-C N: Displays the found results and displays the front and back N rows
-e Use extended regular expression grep-e = Egrep

To extend the regular expression:
Character matching: The same as the basic regular expression

Do not display lines beginning with # or beginning with;
[Email protected] ~]# egrep-v "^#|^;"/etc/samba/smb.conf

Number of matches:
*: Match its preceding character any time (>=0)
? : Do not need to match the preceding characters 1 or 0 times
+: Match its first character at least once, similar to \{1,\}? and the combination of + equals *
[Email protected] test]# grep-e ' ^[[:space:]]+ '/etc/grub.conf
{M,n}: no need to add \

Location matching: Same as the basic regular expression



===========================================================
Sed
Strem Editor Editor
Sed is an online editor that processes a single line of content at a time. When processing,
Stores the currently processed rows in a temporary buffer, called the pattern space,
The contents of the buffer are then processed with the SED command, and the contents of the buffer are sent to the screen after processing is completed.
Then the next line is processed, so it repeats until the end of the file. The contents of the file did not change,
Unless you use redirection to store output. SED is primarily used to automatically edit one or more files, simplifying the repetitive operation of files.

Grammar:
sed [options] ' [command] ' [filename]
Options
-N suppresses automatic (default) output * * Read Next input line
-e executes multiple sed instructions
-F Run Script
-I edit File contents * * *
-i.bak edit while creating a backup of. bak
-R using Extended regular expressions * * *
Command
A after match is added * * *
I insert after match * * *
P Print * * *
D Delete * * *
R/R Read File/Line
W Save
s find
C Replace
Y Replace
h/h Copy/Append mode space (buffer) to storage space
g/g paste Fetch/Append to mode space from storage space
X two exchange of space content
n/n Copy/Append the next line of content to the current
D Delete the contents before \ n
P Print the contents of the previous \ n
B Unconditional Jump
T satisfies a match after the jump
T meet jump when match is not met

Instance:
1: Show/etc/passwd third row
[Email protected] ~]# sed-n ' 3p '/etc/passwd
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
2: Display the first three lines of the file
[Email protected] ~]# sed-n ' 1,3p '/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
3: Show all content except the first three lines of the file
[Email protected] ~]# sed-n ' 1,3!p '/etc/passwd
4: Add # # # to the file header
[[Email protected] ~]$ sed ' 1i### '/etc/passwd >a.txt
5: Add @@@ at end of file
[Email protected] ~]$ sed ' [email protected]@@ '/etc/passwd >b.txt
6: Replace the third line of the file with "$$$
[[Email protected] ~]$ sed ' 3c$$$ '/etc/passwd >c.txt
[Email protected] ~]$ head-4 c.txt
7: Copy the second line of the file to line fourth to the end of the file
[[Email protected] ~]$ sed ' 2,4h; $G '/etc/passwd > D.txt

The regular in SED:
1: Delete file empty line
[[Email protected] ~]$ sed '/^$/d '/etc/passwd >e.txt
2: Write a record (line) containing EXT4 in Fstab to a new file
[[Email protected] ~]$ sed '/ext4/w newfstab '/etc/fstab

======================================================================
Awk
AWK is a programming language tool for working with text
Any awk statement is made up of patterns and actions, and an awk script can have multiple statements. The mode determines the trigger condition and the trigger time of the action statement.

Special fields:
The BEGIN statement sets the count and print header information before any action.
The end statement outputs statistical results, which are executed after the action is completed.

The delimiter default is a space, you can use-f, to change to a comma delimiter-F, or to a colon-f:


Instance:
1. Customize the display mode for month and day:
[[Email protected] ~]$ Date | awk ' {print ' year: "$6" \ T month: "$" \ T days: "$"
2: Show All content:
[[email protected] ~]$ awk ' {print $} '/etc/passwd
3: Display the contents of the first and third columns
[Email protected] ~]$ awk-f: ' {print $1,$3} '/etc/passwd
4: Print a file header, print a file footer
Special fields:
The BEGIN statement sets the count and print header information before any action.
The end statement outputs statistical results, which are executed after the action is completed.
[Email protected] ~]$ awk-f: ' BEGIN {print ' name uid bash\n "}{print $1,$3,$7} end{print" END of Class passwd "} '/etc/pass Wd

5: Those records with the 3rd column equal to 0
[[email protected] ~]$ awk-f: ' {if ($3== "0") print $} '/etc/passwd
Root:x:0:0:root:/root:/bin/bash

6: Those records with 3rd column greater than or equal to 100
[Email protected] ~]$ awk-f: ' $3>=100 {print $} '/etc/passwd
Rtkit:x:499:497:realtimekit:/proc:/sbin/nologin
Abrt:x:173:173::/etc/abrt:/sbin/nologin
Avahi-autoipd:x:170:170:avahi Ipv4ll Stack:/var/lib/avahi-autoipd:/sbin/nologin
SASLAUTH:X:498:76:SASLAUTHD User:/var/empty/saslauth:/sbin/nologin
Nfsnobody:x:65534:65534:anonymous NFS User:/var/lib/nfs:/sbin/nologin
Pulse:x:497:495:pulseaudio System Daemon:/var/run/pulse:/sbin/nologin
Pagoda:x:500:500::/home/pagoda:/bin/bash





linux-Regular Expression-awk-sed use method

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.