Linux 정규 표현 식 grep set awk
각종 프로 그래 밍 언어 에는 모두 정규 가 있 는데 원 리 는 같다.
상용 정규 작업: grep / egrep, sed, awk 1. grep 명령
- c 줄 수 - i 대소 문자 구분 없 음 - n 줄 번호 표시 - v 반전 - r 모든 하위 디 렉 터 리 편집 - A 뒤에 숫자 를 따라 요구 에 부합 되 는 줄 과 아래 n 줄 을 걸 러 냅 니 다 - B 뒤 와 숫자, 요구 에 맞 는 줄 과 위 n 줄 을 걸 러 냅 니 다 - C 뒤에 숫자 와 함께 요구 에 부 합 된 줄 과 상하 각 n 줄 을 걸 러 냅 니 다
[root@bogon ~]# mkdir grep grep
[root@bogon ~]# cd grep/
[root@bogon grep]# cp /etc/passwd .
[root@bogon grep]# ls
passwd
[root@bogon grep]# pwd
/root/grep
[root@bogon 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-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/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:998:996::/var/lib/chrony:/sbin/nologin
nginx:x:997:995:Nginx web server:/var/lib/nginx:/sbin/nologin
-c ;- n;-i;
[root@bogon grep]# grep -c 'nologin' passwd
16
[root@bogon 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-network:x:192:192:systemd Network Management:/:/sbin/nologin
15:dbus:x:81:81:System message bus:/:/sbin/nologin
16:polkitd:x:999:997:User for polkitd:/:/sbin/nologin
17:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
18:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
19:chrony:x:998:996::/var/lib/chrony:/sbin/nologin
21:nginx:x:997:995:Nginx web server:/var/lib/nginx:/sbin/nologin
[root@bogon grep]# grep -i 'nologin' passwd
- v;
[root@bogon grep]# grep -vn '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
20:user1:x:1005:1000::/home/user1:/bin/bash
-A,-B,-C
[root@bogon 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
[root@bogon 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
[root@bogon 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
[root@bogon 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
숫자 를 포함 하 는 모든 줄 필터 링: grep '[0 - 9]' filename
[root@bogon grep]# grep '[0-9]' /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
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-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/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:998:996::/var/lib/chrony:/sbin/nologin
user1:x:1005:1000::/home/user1:/bin/bash
nginx:x:997:995:Nginx web server:/var/lib/nginx:/sbin/nologin
숫자 가 포함 되 지 않 은 모든 줄 을 걸 러 냅 니 다: grep - v '[0 - 9]' filename
[root@bogon grep]# grep -v '[0-9]' /etc/passwd
[root@bogon 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:#
[root@bogon grep]# grep -nv '^#' /etc/inittab
[root@bogon grep]# grep '^[^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:
#
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target
#
임의의 문자 와 중복 문 자 를 걸 러 냅 니 다.
[root@bogon grep]# grep 'r.o' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@bogon grep]# grep 'o*o' /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
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
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-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/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:998:996::/var/lib/chrony:/sbin/nologin
user1:x:1005:1000::/home/user1:/bin/bash
nginx:x:997:995:Nginx web server:/var/lib/nginx:/sbin/nologin
걸 러 낸 문자 가 나타 나 는 횟수;예: grep 'o {2}' filename o 가 2 번 나타 납 니 다.
[root@bogon grep]# grep 'o\{2\}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
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
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
[root@bogon grep]# grep 'o\{0,3\}' /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
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-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/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:998:996::/var/lib/chrony:/sbin/nologin
user1:x:1005:1000::/home/user1:/bin/bash
nginx:x:997:995:Nginx web server:/var/lib/nginx:/sbin/nologin
grep 'o{2}'=egrep 'o{2}'=grep -E 'o{2}'
[root@bogon grep]# egrep 'o{2}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
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
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
[root@bogon grep]# grep -E 'o{2}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
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
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
+: 지정 한 문자 하나 이상 걸 러 내기
egrep 'o+o' filename=grep 'o+o' filename
[root@bogon grep]# egrep 'o+o' /etc/passwd
root:x:0:0:root:/root:/bin/bash
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
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
[root@bogon grep]# grep 'o\+o' /etc/passwd
root:x:0:0:root:/root:/bin/bash
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
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
? :0 개 또는 지정 한 문 자 를 걸 러 냅 니 다.
egrep 'o?o' filename=grep 'o\?o' filename
[root@bogon grep]# grep 'o\?o' /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
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
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-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/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:998:996::/var/lib/chrony:/sbin/nologin
user1:x:1005:1000::/home/user1:/bin/bash
nginx:x:997:995:Nginx web server:/var/lib/nginx:/sbin/nologin
[root@bogon grep]# egrep 'o?o' /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
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
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-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/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:998:996::/var/lib/chrony:/sbin/nologin
user1:x:1005:1000::/home/user1:/bin/bash
nginx:x:997:995:Nginx web server:/var/lib/nginx:/sbin/nologin
|: 하나 이상 의 문 자 를 걸 러 냅 니 다.egrep 'root|nologin' filename=grep -E 'root|nologin' filename
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
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-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/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:998:996::/var/lib/chrony:/sbin/nologin
nginx:x:997:995:Nginx web server:/var/lib/nginx:/sbin/nologin
2 、 set 도구 사용
명령 형식: sed - n 'n' p filename, 작은 따옴표 안의 n 은 하나의 숫자 로 몇 번 째 줄 을 대표 합 니 다.
- n p: 인쇄 할 줄 보이 기 (n) - e: 매 칭 조건 - i: 파일 내용 수정 d: 현재 화면의 줄 삭제 하기;용법 + i, 파일 의 줄 삭제;
- r: 확장 식 지원;사용 () 반드시 - r
[root@bogon sed]# sed -n '2'p test.txt
bin:x:1:1:bin:/bin:/sbin/nologin
[root@bogon sed]# sed -n '2,5'p test.txt
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
모든 내용 인쇄: - n '1, $' p
[root@bogon sed]# sed -n '1,$'p test.txt
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-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/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:998:996::/var/lib/chrony:/sbin/nologin
user1:x:1005:1000::/home/user1:/bin/bash
nginx:x:997:995:Nginx web server:/var/lib/nginx:/sbin/nologin
- e 여러 조건 일치;예:
[root@bogon sed]# sed -e '1'p -e 'root'p -e '/oo*/'p -n test.txt
root:x:0:0:root:/root:/bin/bash
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
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
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-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/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:998:996::/var/lib/chrony:/sbin/nologin
user1:x:1005:1000::/home/user1:/bin/bash
nginx:x:997:995:Nginx web server:/var/lib/nginx:/sbin/nologin
d : 현재 화면의 줄 삭제 하기;용법 + i, 파일 의 줄 삭제;
[root@bogon sed]# egrep -n ':' test.txt
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
4:adm:x:3:4:adm:/var/adm:/sbin/nologin
5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
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
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-network:x:192:192:systemd Network Management:/:/sbin/nologin
15:dbus:x:81:81:System message bus:/:/sbin/nologin
16:polkitd:x:999:997:User for polkitd:/:/sbin/nologin
17:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
18:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
19:chrony:x:998:996::/var/lib/chrony:/sbin/nologin
20:user1:x:1005:1000::/home/user1:/bin/bash
21:nginx:x:997:995:Nginx web server:/var/lib/nginx:/sbin/nologin
[root@bogon sed]# sed '1,15'd test.txt 15 ;
polkitd:x:999:997:User for polkitd:/:/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:998:996::/var/lib/chrony:/sbin/nologin
user1:x:1005:1000::/home/user1:/bin/bash
nginx:x:997:995:Nginx web server:/var/lib/nginx:/sbin/nologin
[root@bogon sed]# wc -l test.txt
21 test.txt
[root@bogon sed]# cp test.txt test1.txt
[root@bogon sed]# sed -i '1,15'd test1.txt 15 ;
[root@bogon sed]# wc -l test1.txt
6 test1.txt
바꾸다
[root@bogon sed]# sed '1,2s/root/boot/g' test.txt
boot:x:0:0:boot:/boot:/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-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/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:998:996::/var/lib/chrony:/sbin/nologin
user1:x:1005:1000::/home/user1:/bin/bash
nginx:x:997:995:Nginx web server:/var/lib/nginx:/sbin/nologin
상례 의 's' 는 바로 교 체 된 명령 입 니 다.' g '는 본 줄 의 전역 으로 교 체 됩 니 다.' g '을 추가 하지 않 으 면 이 줄 에 나타 난 첫 번 째 줄 만 바 꿉 니 다.구분자 로 '/' 를 사용 할 수 있 는 것 외 에 도 '\ #' 이나 '@' 같은 다른 특수 문 자 를 사용 할 수 있 습 니 다.
일치 하 는 + 번호 앞 문자 바 꾸 기
[root@bogon sed]# sed '1,2s/bo+/root/g' test.txt |head -2
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
문자열 의 위치 바 꾸 기;인용
[root@bogon sed]# head -2 test.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@bogon sed]# head -2 test.txt |sed -r 's/([^:]+):(.*):([^:]+)/\3:\2:\1:/'
/bin/bash:x:0:0:root:/root:root:
/sbin/nologin:x:1:1:bin:/bin:bin:
3 、 awk 도구 사용
테스트:
[root@bogon sed]# cd
[root@bogon ~]# mkdir awk
[root@bogon ~]# cp /etc/passwd awk/test.txt
[root@bogon ~]# cd awk
[root@bogon awk]# ls
test.txt
문서 의 한 필드 를 캡 처 합 니 다. -F 옵션 의 역할 은 구분 자 를 지정 하 는 것 입 니 다.- F 옵션 을 추가 하지 않 으 면 빈 칸 이나 tab 를 구분자 로 합 니 다.print 는 필드 를 인쇄 하 는 동작 입 니 다. $1 번 째 필드; $0 은 전체 줄 을 나타 낸다.
[root@bogon awk]# awk -F ':' '{print $1}' test.txt 1
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
systemd-network
dbus
polkitd
postfix
sshd
chrony
user1
nginx
[root@bogon awk]# awk -F ':' '{print $0}' test.txt
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-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/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:998:996::/var/lib/chrony:/sbin/nologin
user1:x:1005:1000::/home/user1:/bin/bash
nginx:x:997:995:Nginx web server:/var/lib/nginx:/sbin/nologin
구분 번 호 를 지정 하여 캡 처 하고 지정 한 기호 로 구분 합 니 다.
파일 생 성 및 편집
[root@bogon awk]# touch test1.txt
[root@bogon awk]# vim test1.txt
파일 내용 인쇄
[root@bogon awk]# awk '{print $0}' test1.txt
11 22 33 44 55 66
aa bb cc dd ee ff
gg hh dd ww rr tt
"\ #" 로 "빈 칸" 을 대체 합 니 다.
[root@bogon awk]# awk -F ' ' '{print $1"#"$2"#"$3}' test1.txt
11#22#33
aa#bb#cc
gg#hh#dd
일치 하 는 문자 나 문자열
[root@bogon awk]# awk '/oo/' test.txt oo
root:x:0:0:root:/root:/bin/bash
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
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
[root@bogon awk]# awk -F ':' '$1 ~ /oo/' test.txt oo
root:x:0:0:root:/root:/bin/bash
정규 표현 식
[root@bogon awk]# awk -F ':' '$1 ~ /oo+/' test.txt
root:x:0:0:root:/root:/bin/bash
[root@bogon awk]# awk -F ':' '$1 ~ /o?/' test.txt
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-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/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:998:996::/var/lib/chrony:/sbin/nologin
user1:x:1005:1000::/home/user1:/bin/bash
nginx:x:997:995:Nginx web server:/var/lib/nginx:/sbin/nologin
다 중 표현 식 인쇄
[root@bogon awk]# awk -F ':' '/root/ {print $1,$3} /user/ {print $1,$3,$4}' test.txt root , ; user , ,
root 0
operator 11
user1 1005 1000
[root@bogon awk]# egrep 'root|user' test.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
user1:x:1005:1000::/home/user1:/bin/bash
수학 연산
[root@bogon awk]# awk -F ':' '$3==0' test.txt
root:x:0:0:root:/root:/bin/bash
[root@bogon awk]# awk -F ':' '$3==0 {print $1}' test.txt =0
root
[root@bogon awk]# awk -F ':' '$3>=0 {print $1}' test.txt 0
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
systemd-network
dbus
polkitd
postfix
sshd
chrony
user1
nginx
!= :같 지 않다
[root@bogon awk]# awk -F ':' '$7!="/sbin/nologin"' test.txt
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
user1:x:1005:1000::/home/user1:/bin/bash
다 조건
[root@bogon awk]# awk -F ':' '$3>"5" && $3 test.txt $3>"5" $3<"7"
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
[root@bogon awk]# awk -F ':' '$3>1000 || $7 ~ /bash/' test.txt $3>1000 $7 ~ /bash/
root:x:0:0:root:/root:/bin/bash
user1:x:1005:1000::/home/user1:/bin/bash
awk 에서 자주 사용 하 는 변 수 는 OFS, NF, NR 입 니 다.
NF: 구분자 로 구분 한 후 모두 몇 단락 이 있 는 지 표시 합 니 다.
NR: 줄 번 호 를 표시 합 니 다.
[root@bogon awk]# awk -F ':' '{OFS="#"} $3>1000 || $7 ~ /bash/ {print $1,$3,$7}' test.txt
root#0#/bin/bash
user1#1005#/bin/bash
주: {OFS = "\ #"} 조건 앞 에 정의 합 니 다.무조건
판단
[root@bogon awk]# awk -F ':' '{OFS="#"} {if ($3>1000) {print $1,$2,$3,$4}}' test.txt
user1#x#1005#1000
NR
[root@bogon awk]# awk -F ':' '{print NR":"$0}' test.txt
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
4:adm:x:3:4:adm:/var/adm:/sbin/nologin
5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
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
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-network:x:192:192:systemd Network Management:/:/sbin/nologin
15:dbus:x:81:81:System message bus:/:/sbin/nologin
16:polkitd:x:999:997:User for polkitd:/:/sbin/nologin
17:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
18:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
19:chrony:x:998:996::/var/lib/chrony:/sbin/nologin
20:user1:x:1005:1000::/home/user1:/bin/bash
21:nginx:x:997:995:Nginx web server:/var/lib/nginx:/sbin/nologin
NF
[root@bogon awk]# awk -F ':' '{print NF":"$0}' test.txt
7:root:x:0:0:root:/root:/bin/bash
7:bin:x:1:1:bin:/bin:/sbin/nologin
7:daemon:x:2:2:daemon:/sbin:/sbin/nologin
7:adm:x:3:4:adm:/var/adm:/sbin/nologin
7:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
7:sync:x:5:0:sync:/sbin:/bin/sync
7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
7:halt:x:7:0:halt:/sbin:/sbin/halt
7:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
7:operator:x:11:0:operator:/root:/sbin/nologin
7:games:x:12:100:games:/usr/games:/sbin/nologin
7:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
7:nobody:x:99:99:Nobody:/:/sbin/nologin
7:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
7:dbus:x:81:81:System message bus:/:/sbin/nologin
7:polkitd:x:999:997:User for polkitd:/:/sbin/nologin
7:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
7:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
7:chrony:x:998:996::/var/lib/chrony:/sbin/nologin
7:user1:x:1005:1000::/home/user1:/bin/bash
7:nginx:x:997:995:Nginx web server:/var/lib/nginx:/sbin/nologi
tot 필드 의 총화
[root@bogon awk]# awk -F ':' '{(tot=tot+$3)}; END {print tot}' test.txt
4607
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.