13th day-linux Regular expressions and key commands

Source: Internet
Author: User

Regular Expressions:
To put it simply, regular expressions are a set of rules and methods for handling strings, dealing with strings in behavioral units, and with special symbolic assistance, we can quickly filter and replace certain strings.
Operation and maintenance work, there will be a large number of access logs, error logs. How to quickly filter out what we need depends on regular expressions.
Awk,sed,grep (egrep) Linux Three Musketeers want to work more efficient, then must be inseparable from the regular expression of the cooperation.
We want to play a good Three musketeers, mainly awk,sed,grep. The regular expression of the Three Musketeers.


The regular expression is actually some special character, giving him the meaning of the principal.
1) ^word search for word starting with
2) word$ Search for the end of Word
3). Represents and can only represent any one character
4) \ Escape character, escape symbol, let the character with special identity meaning, take off the vest, restore the prototype.
5) * Repeat 0 or more of the preceding characters
6). * Matches all characters. ^.* start with any number of characters
7) [] Character set, repeating special character symbols
8) [^word] matches content that does not contain any characters after ^
9) A\{n,m\} repeats N to M times, the previous character
\{n,\} repeats at least n times, the previous character
\{n\} repeats n times, previous character

Extended Regular expression: ERE
1) + Repeat one or more of the previous characters
2)? Repeat 0 or more than 0 preceding characters
3) | Find multiple strings in a way or
Practical examples
1. How to Obtain the/etiantian file permissions corresponding to the digital content, such as-rw-r--r--644, requires the use of commands to obtain 644 or 0644 such numbers.

method 1:[email protected]:~/test$ ll Etiantian-rw-r--r--1Xiaorui Xiaorui0November -  +: -Etiantian[email protected]:~/test$ ll Etiantian |Cut-C2-TenRW-r--r--[email protected]:~/test$ ll Etiantian |Cut-C2-Ten|TRrwx-4210420400400[email protected]:~/test$ ll Etiantian |Cut-C2-Ten|TRrwx-4210|awk-F"" '{print $}'4[email protected]:~/test$ ll Etiantian |Cut-C2-Ten|TRrwx-4210|awk-F"" '{print $1+$2+$3 $4+$5+$6 $7+$8+$9}'644method 2:[email protected]:~/test$StatEtiantian File:"Etiantian"Size:0Block:0IO BLOCK:4096normal empty file device: 802h/2050d Inode:23200567Hard Links:1Permissions: (0644/-rw-r--r--) Uid: ( +/Xiaorui) Gid: ( +/Xiaorui) recently visited: -- One- -  +: -:48.071446235+0800Recent Changes: -- One- -  +: -:48.071446235+0800Recent Changes: -- One- -  +: in:07.707803664+0800creation Time:-[email protected]:~/test$StatEtiantian |sed-N'4p'|awk-F'[(/]' '{print $}'0644method 3:[email protected]:~/test$StatEtiantian |awk-F'[(/]' 'nr==4 {print $}'0644method 4:[email protected]:~/test$StatEtiantian |Head-4|Tail-1|awk-F'[(/]' '{print $}'0644

2.linux create a new directory with the mkdir command what is the number of hard links/home/xiaorui/ett,ett, and why?
What is the number of hard links in Ett when you create a test directory under Ett? Why?

[Email protected]:~$mkdirEtt[email protected]:~$ls-LDett/DRWXRWXR-X2Xiaorui Xiaorui4096November -  A: theett/[email protected]:~$ CD ett/[email protected]:~/ett$mkdirTest[email protected]:~/ett$ls-LD.. /ett/DRWXRWXR-X3Xiaorui Xiaorui4096November -  A: -.. /ett/[email protected]:~/ett$ LL-I24512863Drwxrwxr-x3Xiaorui Xiaorui4096November -  A: -./23068674Drwxr-xr-x theXiaorui Xiaorui4096November -  A: the.. /24512865Drwxrwxr-x2Xiaorui Xiaorui4096November -  A: -test/[email protected]:~/ett$ LL-ID/home/xiaorui/ett/24512863Drwxrwxr-x3Xiaorui Xiaorui4096November -  A: -/home/xiaorui/ett//[Email protected]:~/ett/test$ls-Lai24512865Drwxrwxr-x2Xiaorui Xiaorui4096November -  A: - .24512863Drwxrwxr-x3Xiaorui Xiaorui4096November -  A: -..

When a new directory is created, the number of hard links for this directory is 2 because:
1. The created directory itself is a hard link.
2, directory Ett under the hidden directory. (dot) There is a hard link to create a table of contents, so its number of hard links is 2.
When you create a catalog test under the newly created Ett, the number of hard links in the Ett directory is 3, because there is a hidden one in the newly created test directory: (Two dots) is another hard link for ett.

3. Please execute the command to take out the IP address of wlan0 in Linux (please use the cut, the ability can also be used separately awk,sed command answer).

method 1:[email protected]:~$ifconfigwlan0|grep "inet"inet Address:192.168.1.111Broadcasting:192.168.1.255Mask:255.255.255.0Inet6 Address: fe80::162d:27ff:fefd:c215/ -Scope:link[email protected]:~$ifconfigwlan0|grep "inet"inet Address:192.168.1.111Broadcasting:192.168.1.255Mask:255.255.255.0[email protected]:~$ifconfigwlan0|grep "inet"|awk '{print $}'Address:192.168.1.111[email protected]:~$ifconfigwlan0|grep "inet"|awk '{print $}'|awk-F':' '{print $}'192.168.1.111method 2:[email protected]:~$ifconfigwlan0|grep "inet"|Cut-D':'-F2192.168.1.111radio [email protected]:~$ifconfigwlan0|grep "inet"|Cut-D':'-f2|Cut-D' '-F1192.168.1.111method 3:[email protected]:~$ifconfigwlan0|grep "inet"|awk-F"[ :]" '{print $13}'192.168.1.111method 4:[email protected]:~$ifconfigwlan0|grep "inet"|awk-F"[ :]+" '{print $4}'192.168.1.111method 5:[email protected]:~$ifconfigwlan0|awk-F"[ :]+" 'Nr==2{print $4}'192.168.1.111method 6:[email protected]:~$ifconfigwlan0|sed-N'2p'|sed  's#^.* Address: # #g'|sed 's# broadcast:. *$# #g'192.168.1.111 

4. Give the path to the eth0 NIC configuration file by default and the path to the client DNS.
Eth0 NIC configuration path:/etc/sysconfig/network-scripts/ifcfg-eth0
Path to client DNS:/etc/resolv.conf

5. Recommend the old boy teacher two blog posts:
Basic network configuration of Linux system Veteran's essence
Brief summary of the route command in layman
  

13th day-linux Regular expressions and key commands

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.