Day 7 文件管理补充和练习 File management & exercises

Day 7 文件管理补充和练习 File management & exercises

Regular expression 正则

^

Beginning of line 行首

$

End of line 行末

.

Single Character 单个字符

*

Zero or more Occurrences 左边的字符出现0次或更多次

+

One or more Occurrence 左边的字符出现一次或更多次

Zero or one Occurrence 昨天的字符出现0次或1次

[]

[0-9]

The character class is nothing but a list of characters mentioned within a square bracket; this is used to match only one out of several characters.任意一位数字

[a-z] [a-Z]

Within the square bracket, you can use a hyphen you can specify a range of characters. For example, [0123456789] can be represented by [0-9], and alphabetic ranges can be specified such as [a-z],[A-Z] etc.

任意一个小写字母/大写或小写字母

[^]

^在中括号里表示“非”的意思

|

OR Operation 表示“或”

{}

{m}

Exactly M Occurrences 表示出现了m次

{m,n}

M to N Occurrences 表示出现了m到n次

 

AWK

awk -Fs '/pattern/ {action}' input-file

AWK exercises

打印uid在30~40范围内的用户名。
awk -F: '$3>=30 && $3<=40 {print $0}' /etc/passwd
打印第5-10行的行号和用户名
awk -F: 'NR>=5 && NR<=10 {print NR,$1}' /etc/passwd
打印奇数行
awk -F: 'NR%2==1' /etc/passwd
打印偶数行
awk -F: 'NR2%==0' /etc/passwd
打印字段数大于5的行
awk -F: 'NF>5 {print $0}' /etc/passwd
打印UID不等于GID的用户名
awk -F: '$3!=$4 {print $0}' /etc/passwd
打印没有指定shell的用户
awk -F: '$NF~ /nologin$/{print $1}' /etc/passwd


打印1..1000以内的7的倍数和包含7的数

 

原文地址:https://www.cnblogs.com/fengshili666/p/14118882.html