六周第一次课(1月15日) 9.1 正则介绍_grep上 9.2 grep中 9.3 grep下

六周第一次课(1月15日)
9.1 正则介绍_grep上
9.2 grep中
9.3 grep下

在计算机科学中,对“正则表达式" 的定义是:它使用单个字符串来描述或匹配一系列符合某个句法规则的字符串。在很多文本编辑器或其他工具里,正则表达式通常用来检索和替换那些符合某个模式的文本内容。许多程序设计语言也都支持利用正则表达式进行字符串操作。对于系统管理员来讲,正则表达式贯穿在我们的日常运维工作中,无论是查找某个文档,还是查询某个日志文件并分析其容,都会用到正则表达式。
其实正则表达式只是一种思想、一种表示方法。只要我们使用的工具支持这种表示方法,那么这个工具就可以处理正则表达式的字符串。常用的工具有grep、sed、awk等,其中grep、sed和都是针对文本的行进行操作的。

正则介绍_grep上
该命令的格式为:grep [-cinvABC]‘word’filename

常用的选项如下:
-c:表示打印符合要求的行数。
-i:表示不区分大小写。
-n:表示输出符合要求的行及其行号。
-v:表示打印不符合要求的行。
-r:遍历所有子目录。
-A:后面跟一个数字(有无空格都可以)。过滤出符合要求的行以及下面n行
-B:后面跟一个数字。过滤出符合要求的行以及上面n行
-C:后面跟一个数字。过滤出符合要求的行以及上面n行。

grep中
方括号[ ]:匹配方括号里任意一个字符

^x:匹配以x开头的字符

[^xyz]:非,匹配除方括号里xyz之外的任意字符串

^[^xyz]:匹配以除方括号里xyz之外的任意字符开头的字符
点 .:匹配任意一字符
*:匹配星号左边的字符重复0到n次的字符串。
.*:表示零个或多个任意字符,空行也包含再内。
会把passwd文件里面的所有行都匹配到。

grep下

花括号 x\{ n\}:字符x重复n次。
x\{ n,m\}:字符x重复n-m次
egrep:是grep的扩展版本,可以完成grep不能完成的工作,可以不使用字符 \
grep –E 跟egrep使用效果一样
+:匹配加号左边的字符重复1到n次的字符串。
?:问号前面的字符重复的次数是0或者1。
|:或者

grep ‘[0-9]’ passwd #把passwd文件中含有数字的行过滤打印出来

grep ‘^#’ shadow #把shadow文件中以“#”号开头的行过滤打印出来

grep –v ‘^#’ shadow #把shadow文件中不以“#”开头的行过滤打印出来

grep ‘^[^0-9]’ shadow #把shadow文件中以非数字开头的行过滤打印出来

grep‘r.o‘passwd#把passwd文件中r在前o在尾中间任意字符的字符串过滤打印出来

grep ‘o*o’passwd #把passwd文件中o重复n次且以o结尾的字符串过滤打印出来

grep ‘.*’ passwd #把passwd文件中任意字符创过滤打印出来

grep ‘o\{2\}’ passwd #把passwd文件中含有两个o的字符串过滤打印出来

egrep ‘o{2}’ passwd====grep ‘o\{2\}’passwd====grep –E ‘o{2}’ passwd

grep ‘o+o’ passwd #把passwd文件中o开头o结尾的字符串过滤打印出来

grep‘o?t’passwd #把passwd文件中有或者没有o开头以t结尾的字符过滤打印出来

egrep‘root|nologin’passwd #把passwd文件中匹配root或者nologin的字符
============================================================================================================================================================================================================
grep命令:
文件过滤分割与合并

grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。

选项
-a 不要忽略二进制数据。
-A<显示列数> 除了显示符合范本样式的那一行之外,并显示该行之后的内容。
-b 在显示符合范本样式的那一行之外,并显示该行之前的内容。
-c 计算符合范本样式的列数。
-C<显示列数>或-<显示列数> 除了显示符合范本样式的那一列之外,并显示该列之前后的内容。
-d<进行动作> 当指定要查找的是目录而非文件时,必须使用这项参数,否则grep命令将回报信息并停止动作。
-e<范本样式> 指定字符串作为查找文件内容的范本样式。
-E 将范本样式为延伸的普通表示法来使用,意味着使用能使用扩展正则表达式。
-f<范本文件> 指定范本文件,其内容有一个或多个范本样式,让grep查找符合范本条件的文件内容,格式为每一列的范本样式。
-F 将范本样式视为固定字符串的列表。
-G 将范本样式视为普通的表示法来使用。
-h 在显示符合范本样式的那一列之前,不标示该列所属的文件名称。
-H 在显示符合范本样式的那一列之前,标示该列的文件名称。
-i 忽略字符大小写的差别。
-l 列出文件内容符合指定的范本样式的文件名称。
-L 列出文件内容不符合指定的范本样式的文件名称。
-n 在显示符合范本样式的那一列之前,标示出该列的编号。
-q 不显示任何信息。
-R/-r 此参数的效果和指定“-d recurse”参数相同。
-s 不显示错误信息。
-v 反转查找。
-w 只显示全字符合的列。
-x 只显示全列符合的列。
-y 此参数效果跟“-i”相同。
-o 只输出文件中匹配到的部分。
grep命令常见用法
在文件中搜索一个单词,命令会返回一个包含“match_pattern”的文本行:

grep match_pattern file_name
grep "match_pattern" file_name
在多个文件中查找:

grep "match_pattern" file_1 file_2 file_3 ...
输出除之外的所有行 -v 选项:

grep -v "match_pattern" file_name
标记匹配颜色 --color=auto 选项:

grep "match_pattern" file_name --color=auto
使用正则表达式 -E 选项:

grep -E "[1-9]+"

egrep "[1-9]+"
只输出文件中匹配到的部分 -o 选项:

echo this is a test line. | grep -o -E "[a-z]+\."
line.

echo this is a test line. | egrep -o "[a-z]+\."
line.
统计文件或者文本中包含匹配字符串的行数 -c 选项:

grep -c "text" file_name
输出包含匹配字符串的行数 -n 选项:

grep "text" -n file_name

cat file_name | grep "text" -n

#多个文件
grep "text" -n file_1 file_2
打印样式匹配所位于的字符或字节偏移:

echo gun is not unix | grep -b -o "not"
7:not

#一行中字符串的字符便宜是从该行的第一个字符开始计算,起始值为0。选项 -b -o 一般总是配合使用。
搜索多个文件并查找匹配文本在哪些文件中:

grep -l "text" file1 file2 file3...
grep递归搜索文件
在多级目录中对文本进行递归搜索:

grep "text" . -r -n
# .表示当前目录。
忽略匹配样式中的字符大小写:

echo "hello world" | grep -i "HELLO"
hello
选项 -e 制动多个匹配样式:

echo this is a text line | grep -e "is" -e "line" -o
is
line

#也可以使用-f选项来匹配多个样式,在样式文件中逐行写出需要匹配的字符。
cat patfile
aaa
bbb

echo aaa bbb ccc ddd eee | grep -f patfile -o
在grep搜索结果中包括或者排除指定文件:

#只在目录中所有的.php和.html文件中递归搜索字符"main()"
grep "main()" . -r --include *.{php,html}

#在搜索结果中排除所有README文件
grep "main()" . -r --exclude "README"

#在搜索结果中排除filelist文件列表里的文件
grep "main()" . -r --exclude-from filelist
使用0值字节后缀的grep与xargs:

#测试文件:
echo "aaa" > file1
echo "bbb" > file2
echo "aaa" > file3

grep "aaa" file* -lZ | xargs -0 rm

#执行后会删除file1和file3,grep输出用-Z选项来指定以0值字节作为终结符文件名(\0),xargs -0 读取输入并用0值字节终结符分隔文件名,然后删除匹配文件,-Z通常和-l结合使用。
grep静默输出:

grep -q "test" filename

#不会输出任何信息,如果命令运行成功返回0,失败则返回非0值。一般用于条件测试。
打印出匹配文本之前或者之后的行:

#显示匹配某个结果之后的3行,使用 -A 选项:
seq 10 | grep "5" -A 3
5
6
7
8

#显示匹配某个结果之前的3行,使用 -B 选项:
seq 10 | grep "5" -B 3
2
3
4
5

#显示匹配某个结果的前三行和后三行,使用 -C 选项:
seq 10 | grep "5" -C 3
2
3
4
5
6
7
8

#如果匹配结果有多个,会用“--”作为各匹配结果之间的分隔符:
echo -e "a\nb\nc\na\nb\nc" | grep a -A 1
a
b
--
a
b

============================================================================================================================================================================================================
egrep命令:

文件过滤分割与合并

egrep命令用于在文件内查找指定的字符串。egrep执行效果与grep -E相似,使用的语法及参数可参照grep指令,与grep的不同点在于解读字符串的方法。egrep是用extended regular expression语法来解读的,而grep则用basic regular expression 语法解读,extended regular expression比basic regular expression的表达更规范。

语法
egrep(选项)(查找模式)(文件名1,文件名2,……)
实例
显示文件中符合条件的字符。例如,查找当前目录下所有文件中包含字符串"Linux"的文件,可以使用如下命令:

egrep Linux *
结果如下所示:

#以下五行为 testfile 中包含Linux字符的行
testfile:hello Linux!
testfile:Linux is a free Unix-type operating system.
testfile:This is a Linux testfile!
testfile:Linux
testfile:Linux

#以下两行为testfile1中含Linux字符的行
testfile1:helLinux!
testfile1:This a Linux testfile!

#以下两行为 testfile_2 中包含Linux字符的行
testfile_2:Linux is a free unix-type opterating system
testfile_2:Linux test

============================================================================================================================================================================================================

测试::::

[[email protected] ~]# ls
1.txt 234 aa.txt anaconda-ks.cfg.1 新建文本文档_(2).txt
[[email protected] ~]# mkdir grep
[[email protected] ~]# cd grep/
[[email protected] grep]# cp /etc/passwd .
[[email protected] grep]# ls
passwd
[[email protected] grep]# pwd
/root/grep
[[email protected] grep]# ls
passwd
[[email protected] grep]# grep ‘nologin‘ passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
adkxx1:x:1006:1001::/home/adkxx:/sbin/nologin
[[email protected] grep]# which grep
alias grep=‘grep --color=auto‘
/usr/bin/grep
[[email protected] grep]# /usr/bin/grep ‘nologin‘ passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
adkxx1:x:1006:1001::/home/adkxx:/sbin/nologin
[[email protected] grep]# grep -c ‘nologin‘ passwd
18
[[email protected] grep]# grep -n ‘nologin‘ passwd
2:bin:x:1:1:bin:/bin:/sbin/nologin
3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
4:adm:x:3:4:adm:/var/adm:/sbin/nologin
5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/root:/sbin/nologin
11:games:x:12:100:games:/usr/games:/sbin/nologin
12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13:nobody:x:99:99:Nobody:/:/sbin/nologin
14:systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
15:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
16:dbus:x:81:81:System message bus:/:/sbin/nologin
17:polkitd:x:998:996:User for polkitd:/:/sbin/nologin
18:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
19:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
20:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
21:chrony:x:997:995::/var/lib/chrony:/sbin/nologin
24:adkxx1:x:1006:1001::/home/adkxx:/sbin/nologin
[[email protected] grep]# vim passwd
[[email protected] grep]# grep -ni ‘nologin‘ passwd
3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
4:adm:x:3:4:adm:/var/adm:/sbin/nologin
5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/root:/sbin/nologin
11:games:x:12:100:games:/usr/games:/sbin/nologin
12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13:nobody:x:99:99:Nobody:/:/sbin/nologin
14:systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
15:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
16:dbus:x:81:81:System message bus:/:/sbin/nologin
17:polkitd:x:998:996:User for polkitd:/:/sbin/nologin
18:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
19:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
20:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
21:chrony:x:997:995::/var/lib/chrony:/sbin/nologin
24:adkxx1:x:1006:1001::/home/adkxx:/sbin/nologin
[[email protected] grep]# vim passwd
[[email protected] grep]# grep -ni ‘nologin‘ passwd
3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
4:adm:x:3:4:adm:/var/adm:/sbin/nologin
5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/root:/sbin/nologin
11:games:x:12:100:games:/usr/games:/sbin/nologin
12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13:nobody:x:99:99:Nobody:/:/sbin/nologin
14:systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
15:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
16:dbus:x:81:81:System message bus:/:/sbin/nologin
17:polkitd:x:998:996:User for polkitd:/:/sbin/nologin
18:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
19:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
20:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
21:chrony:x:997:995::/var/lib/chrony:/sbin/nologin
24:adkxx1:x:1006:1001::/home/adkxx:/sbin/nologin
[[email protected] grep]# vim passwd
[[email protected] grep]# grep -ni ‘nologin‘ passwd
2:bin:x:1:1:bin:/bin:/sbin/NOLOgin
3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
4:adm:x:3:4:adm:/var/adm:/sbin/nologin
5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/root:/sbin/nologin
11:games:x:12:100:games:/usr/games:/sbin/nologin
12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13:nobody:x:99:99:Nobody:/:/sbin/nologin
14:systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
15:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
16:dbus:x:81:81:System message bus:/:/sbin/nologin
17:polkitd:x:998:996:User for polkitd:/:/sbin/nologin
18:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
19:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
20:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
21:chrony:x:997:995::/var/lib/chrony:/sbin/nologin
24:adkxx1:x:1006:1001::/home/adkxx:/sbin/nologin
[[email protected] grep]# grep -vni ‘nologin‘ passwd
1:root:x:0:0:root:/root:/bin/bash
6:sync:x:5:0:sync:/sbin:/bin/sync
7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8:halt:x:7:0:halt:/sbin:/sbin/halt
22:adkee:x:1000:1000::/home/adkee:/bin/bash
23:adkee1:x:1001:1002::/home/adkee1:/bin/bash
25:adkxx5:x:1007:1007::/home/adkxx5:/bin/bash
[[email protected] grep]# grep -r ‘root‘ /etc/
/etc/pki/ca-trust/ca-legacy.conf:# The upstream Mozilla.org project tests all changes to the root CA
/etc/pki/ca-trust/ca-legacy.conf:# to temporarily keep certain (legacy) root CA certificates trusted,
/etc/pki/ca-trust/ca-legacy.conf:# It may keep root CA certificate as trusted, which the upstream
/etc/pki/ca-trust/extracted/README:root CA certificates.
/etc/pki/ca-trust/extracted/java/README:root CA certificates.
匹配到二进制文件 /etc/pki/ca-trust/extracted/java/cacerts
/etc/pki/ca-trust/extracted/openssl/README:root CA certificates.
/etc/pki/ca-trust/extracted/pem/README:root CA certificates.
/etc/pki/tls/certs/make-dummy-cert: echo [email protected]
/etc/pki/tls/openssl.cnf:dir = ./demoCA # TSA root directory
/etc/rpm/macros.perl:%define perl_br_testdir %{buildroot}%{perl_testdir}/%{cpan_dist_name} \
/etc/rpm/macros.perl:%defattr(-,root,root,-)\
/etc/yum/pluginconf.d/fastestmirror.conf:# as root).
/etc/aliases:postmaster: root
/etc/aliases:bin: root
/etc/aliases:daemon: root
/etc/aliases:adm: root
/etc/aliases:lp: root
/etc/aliases:sync: root
/etc/aliases:shutdown: root
/etc/aliases:halt: root
/etc/aliases:mail: root
/etc/aliases:news: root
/etc/aliases:uucp: root
/etc/aliases:operator: root
/etc/aliases:games: root
/etc/aliases:gopher: root
/etc/aliases:ftp: root
/etc/aliases:nobody: root
/etc/aliases:radiusd: root
/etc/aliases:nut: root
/etc/aliases:dbus: root
/etc/aliases:vcsa: root
/etc/aliases:canna: root
/etc/aliases:wnn: root
/etc/aliases:rpm: root
/etc/aliases:nscd: root
/etc/aliases:pcap: root
/etc/aliases:apache: root
/etc/aliases:webalizer: root
/etc/aliases:dovecot: root
/etc/aliases:fax: root
/etc/aliases:quagga: root
/etc/aliases:radvd: root
/etc/aliases:pvm: root
/etc/aliases:amandabackup: root
/etc/aliases:privoxy: root
/etc/aliases:ident: root
/etc/aliases:named: root
/etc/aliases:xfs: root
/etc/aliases:gdm: root

etc/postfix/master.cf:# service type private unpriv chroot wakeup maxproc command + args
/etc/audit/auditd.conf:log_group = root
/etc/audit/auditd.conf:action_mail_acct = root
/etc/sudoers:## the root user, without needing the root password.
/etc/sudoers:## Allow root to run any commands anywhere
/etc/sudoers:root ALL=(ALL) ALL
/etc/sudoers:## cdrom as root
匹配到二进制文件 /etc/aliases.db
匹配到二进制文件 /etc/.profile.swp
/etc/updatedb.conf:PRUNEFS = "9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fuse.sshfs fusectl gfs gfs2 gpfs hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfs"
匹配到二进制文件 /etc/.fstab.swp
[[email protected] grep]# grep ‘root‘ /etc/
grep: /etc/: 是一个目录
[[email protected] grep]# grep ‘root‘ /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] grep]# grep ‘root‘ /etc/ > /
.autorelabel etc/ media/ root/ sys/
bin/ home/ mnt/ run/ tmp/
boot/ lib/ opt/ sbin/ usr/
dev/ lib64/ proc/ srv/ var/
[[email protected] grep]# grep ‘root‘ /etc/ > /
.autorelabel etc/ media/ root/ sys/
bin/ home/ mnt/ run/ tmp/
boot/ lib/ opt/ sbin/ usr/
dev/ lib64/ proc/ srv/ var/
[[email protected] grep]# grep ‘root‘ /etc/ > /
.autorelabel etc/ media/ root/ sys/
bin/ home/ mnt/ run/ tmp/
boot/ lib/ opt/ sbin/ usr/
dev/ lib64/ proc/ srv/ var/
[[email protected] grep]# grep ‘root‘ /etc/ > /tmp/passwd.log
grep: /etc/: 是一个目录
[[email protected] grep]# grep -r ‘root‘ /etc/ > /tmp/passwd.log
[[email protected] grep]# grep passwd /tmp/passwd.log
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
/etc/passwd-:root:x:0:0:root:/root:/bin/bash
/etc/passwd-:operator:x:11:0:operator:/root:/sbin/nologin
/etc/postfix/main.cf:# the system passwd file in the chroot jail is just not practical.
[[email protected] grep]# grep -A2 ‘root‘ 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
--
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[[email protected] grep]# grep -nA2 ‘root‘ passwd
1:root:x:0:0:root:/root:/bin/bash
2-bin:x:1:1:bin:/bin:/sbin/NOLOgin
3-daemon:x:2:2:daemon:/sbin:/sbin/nologin
--
10:operator:x:11:0:operator:/root:/sbin/nologin
11-games:x:12:100:games:/usr/games:/sbin/nologin
12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[[email protected] grep]# grep -nB2 ‘root‘ passwd
1:root:x:0:0:root:/root:/bin/bash
--
8-halt:x:7:0:halt:/sbin:/sbin/halt
9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] grep]# grep -nC2 ‘root‘ passwd
1:root:x:0:0:root:/root:/bin/bash
2-bin:x:1:1:bin:/bin:/sbin/NOLOgin
3-daemon:x:2:2:daemon:/sbin:/sbin/nologin
--
8-halt:x:7:0:halt:/sbin:/sbin/halt
9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/root:/sbin/nologin
11-games:x:12:100:games:/usr/games:/sbin/nologin
12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[[email protected] grep]# grep ‘[0-9]‘ 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
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
adkee:x:1000:1000::/home/adkee:/bin/bash
adkee1:x:1001:1002::/home/adkee1:/bin/bash
adkxx1:x:1006:1001::/home/adkxx:/sbin/nologin
adkxx5:x:1007:1007::/home/adkxx5:/bin/bash
[[email protected] grep]# grep -v ‘[0-9]‘ passwd
[[email protected] grep]# grep -v ‘[0-9]‘ /etc/inittab
# inittab is no longer used when using systemd.
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#
# systemd uses ‘targets‘ instead of runlevels. By default, there are two main targets:
#
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target
#
[[email protected] grep]# grep -vn ‘[0-9]‘ /etc/inittab
1:# inittab is no longer used when using systemd.
2:#
3:# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
4:#
5:# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
6:#
7:# systemd uses ‘targets‘ instead of runlevels. By default, there are two main targets:
8:#
11:#
12:# To view current default target, run:
13:# systemctl get-default
14:#
15:# To set a default target, run:
16:# systemctl set-default TARGET.target
17:#
[[email protected] grep]# vim /etc/inittab
[[email protected] grep]# grep -n ‘^#‘ /etc/inittab
1:# inittab is no longer used when using systemd.
2:#
3:# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
4:#
5:# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
6:#
7:# systemd uses ‘targets‘ instead of runlevels. By default, there are two main targets:
8:#
9:# multi-user.target: analogous to runlevel 3
10:# graphical.target: analogous to runlevel 5
11:#
12:# To view current default target, run:
13:# systemctl get-default
14:#
15:# To set a default target, run:
16:# systemctl set-default TARGET.target
17:#
[[email protected] grep]#

 

原文地址:https://www.cnblogs.com/yikebaicai/p/8290316.html

时间: 2024-08-24 16:13:51

六周第一次课(1月15日) 9.1 正则介绍_grep上 9.2 grep中 9.3 grep下的相关文章

Linux 三周第一次课(4月2日)Linux,windows 互传文件 用户配置文件密码 组管理

三周第一次课(4月2日) 2.27linux和windows互传文件3.1 用户配置文件和密码配置文件3.2 用户组管理3.3 用户管理 Linux和windows文件互传 我通常使用的是ftp或者是其他文件管理方式 这里介绍一个办法,但是只能在xshell下使用, 安装 lrzsz工具包 安装好了之后就可以使用sz命令 +要传输的文件 如果是想从windows往linux传就可以使用 rz命令 overall 总结 可以使用xshell securecrt 不能使用putty 安装lrzsz工

Linux学习笔记第三周第一次课(2月5日)

2.27linux和windows互传文件 用xshell软件 #yum install -y lrzsz linux传到windows,命令为#sz a.txt 按提示保存到windows windows传到linux,命令为#rz 选择windows文件,之后保存到当前目录 3.1 用户配置文件和密码配置文件5 /etc/passwd用户账号密码文件,配置文件: 配置文件内容,以冒号分隔,共7列: 第一列,用户名: 第二列,密码全用X表示: 第三列,UID: 第四列,GID: 第五列,注释:

三周第一次课(12月25日)

2.27linux和windows互传文件 yum install -y lrzsz sz a.txt linux传到windows rz  windows传到linux当前目录 3.1 用户配置文件和密码配置文件 ls /etc/passwd cat /etc/passwd aming:x:1000:1000::/home/aming:/bin/bash (6个冒号分成7段) loginID:x:UID:GID:comment:home_directory:login_shell  用户名:代

三周第一次课(2月5日)

2.27linux和windows互传文件 3.1 用户配置文件和密码配置文件 3.2 用户组管理 3.3 用户管理 2.27 Linux和windows互传文件 Xshell可以实现windows Linux传输,putty不支持Windows Linux互存.(xhshell, securecrt,可以,putty不行) #yum install -y lrzsz 安装windows linux互传工具. #lrzsz windows文件可以传到Linux上去 把Linux文件存到windo

一周第一次课(3月19日)

1.5安装centos1.虚拟机的创建:打开安装好的VMware选择"文件--->新建虚拟机",如图:选择"自定义",下一步:选择默认,下一步:选择"第三项"稍后安装操作系统",以后安装,方便进行配置.下一步:上图指的是,打算在虚拟机中模拟一个怎样的操作系统,这里选择Linux,Version里选择CentOS 64位,下一步:输入虚拟机名称(可以随便起),选择镜像文件存放的路径.下一步:选择给虚拟机分配的cpu核的数量,如果为虚

六周第一次课(1月15日)

六周第一次课(1月15日)9.1 正则介绍_grep上 1.作用Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户. 2.格式grep [options] 3.主要参数[options]主要参数:-c:只输出匹配行的计数.-I:不区分大 小写(只适用于单字符).-h:查询多文件时不显示文件名.-l:查询多文件时只输出包

Linux20180425六周第一次课(4月25日)

六周第一次课(4月25日) 9.1 正则介绍_grep上9.2 grep中9.3 grep下 正则表达式 使用grep可以查找文件中的关键字. 语法: grep  "关键字" 文件名 有一些选项是可以选的. -n 是显示行号 -c显示行数,一共在这个文件中有多少行带有这个关键字 -v取反,就是多少行没有这个关键字,也就是除了关键字的那两行外 -A 数字,表示显示出关键字以及往下数字行的内容 -B 数字,是和A想反是往上数字行 -C 数字 就是既有上又有下,往上数字行 往下数字行 gre

四周第一次课(1月2日)

四周第一次课(1月2日)4.10/4.11/4.12 lvm讲解 LVM是 Logical Volume Manager(逻辑卷管理)的简写,它由Heinz Mauelshagen在Linux 2.4内核上实现.LVM将一个或多个硬盘的分区在逻辑上集合,相当于一个大硬盘来使用,当硬盘的空间不够使用的时候,可以继续将其它的硬盘的分区加入其中,这样可以实现磁盘空间的动态管理,相对于普通的磁盘分区有很大的灵活性. 与传统的磁盘与分区相比,LVM为计算机提供了更高层次的磁盘存储.它使系统管理员可以更方便

四周第一次课(1月2日) 4.10/lvm讲解 4.11/lvm讲解 4.12/lvm讲解 4.13 磁盘故障小案例

四周第一次课(1月2日)4.10/lvm讲解4.11/lvm讲解4.12/lvm讲解4.13 磁盘故障小案例 onnecting to 192.168.183.128:22...Connection established.To escape to local shell, press 'Ctrl+Alt+]'. Last login: Tue Jan 2 19:34:17 2018[[email protected] ~]# ip add1: lo: <LOOPBACK,UP,LOWER_UP