首页 > 其他分享 >四剑客第六关

四剑客第六关

时间:2024-03-15 15:45:49浏览次数:22  
标签:bin sed STREAM 第六 unix 剑客 home bash

四剑客第六关

  1. 使用find将目前目录及字目录所有扩展名是c 的列出来
find . -name "*.c"
  1. 使用find 将当前目录和字目录所有一般文件列出
find . -type f






[root@master ~]# find . -type f
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./.bash_history
./nwq1.6.sh
./hosts
./.1.txt.swp
./CentOS6-Base-163.repo
./motd
./profile
./2.txt
./3.txt
./4.txt
./5.txt
./6.txt
./7.txt
./8.txt
./9.txt
./10.txt
./11.txt
./12.txt
./13.txt
./14.txt
./15.txt
./16.txt
./17.txt
./18.txt
./19.txt
./20.txt
./21.txt
./22.txt
./2024-03-14.abc.tar.gz
./abc
./1.txt
./file.txt

  1. 使用find查找/usr/最近7天修改过的文件复制到/dir


find /usr/ -type f -mtime -7 -exec cp {} /dir/ \;


find /usr/ -mtime -7 -exec cp {} /dir/ \;


4.使用grep找出/etc/passwd文件中以root开头以bash结尾的行,并显示行号

grep -n '^root.*bash$' /etc/passwd
-n 在显示符合范本样式的那一列之前,标示出该列的编号


 ~]# grep -n '^root.*bash$' /etc/passwd
1:root:x:0:0:root:/root:/bin/bash

5.使用grep匹配file文件中etc那行及后5行

grep -A 5 'etc' file


awk '/etc/{print; n=NR+5} NR<=n' file


-A<显示列数> 除了显示符合范本样式的那一行之外,并显示该行之后的内容




[root@master ~]# awk '/etc/{print; n=NR+5} NR<=n' file
etcdfkljfsdjf
etcdfkljfsdjf

sdfkljsdlfl;sdkfsdjfs

sdfljsdklfs
lsdhjfsdj


[root@master ~]# cat file
sdfjhsdjfsdf

sdfklsdfsd

sfksjfljsd

sdfklsdkhjf
etcdfkljfsdjf

sdfkljsdlfl;sdkfsdjfs

sdfljsdklfs
lsdhjfsdj
sdlhfksdf
sdlhfsdflksd
sdfhjsd
sdkfhsdf
sfshjflsdf
sfmsdkf
saflsdjfjsf
sfosefje
smfkhsefhase
,sfjsefase
rfeshfesjr
ewrjkwehrwe
'

eklrjkesfs
fnshfjkse
hbejn

[root@master ~]# grep -A 5 'etc' file
etcdfkljfsdjf

sdfkljsdlfl;sdkfsdjfs

sdfljsdklfs
lsdhjfsdj




6.使用grep显示etutxt文件中不区分大小的s开头行

grep -i '^s' etutxt


-i 忽略字符大小写的差别



[root@master ~]# grep -i '^s' etu.txt 
sdhjfsf
sdfhshf
sfkls
sgfjheskhf
Sskhfkles
sdlklsdjgjsd
Sdfgljdlgklsdfgjiriomd
sdgjkdjfg
sdfdklghjjergm
sdrgurkutrhbt
sdrkgherhirtbgerui


7.显示/etc/inittab中包含了:一个数字:(即两个冒号中间一个数字)的行;

grep -E ':\d+:' /etc/inittab
-E 将范本样式为延伸的普通表示法来使用,意味着使用能使用扩展正则表达式

grep '[0-9]:' /etc/inittab


::匹配冒号字符
[0-9]:匹配任意一个数字字符(0到9之间的任意一个)
::再次匹配冒号字符



8 使用sed 修改test.txt 的23行abet为etc

sed -i '23s/abet/etc/g' test.txt

 -i 在当前行上面插入文本
使用 -i 选项时,sed 会直接编辑指定的文件,并保存更改。




[root@master ~]# cat test.txt 
dsjkfh
fsdfhsd
sdfjs
sdkfhherg
dfghksdhg
sdfksdh
sfhshef

afjnshdf
fkhsekf
sefhke
efnkehuf
sfneskfhes'f

dflsdfkhs
sefjef
sdhfrkhg
erkthwe
rkshfkser
ekrhse
flehise
ege
abet
sdfksdf
sdfsdkfs
sdfsldf
sdfjsl'
se
ekhfe
efkhesh

[root@master ~]# sed -i '23s/abet/etc/g' test.txt 
[root@master ~]# cat test.txt 
dsjkfh
fsdfhsd
sdfjs
sdkfhherg
dfghksdhg
sdfksdh
sfhshef

afjnshdf
fkhsekf
sefhke
efnkehuf
sfneskfhes'f

dflsdfkhs
sefjef
sdhfrkhg
erkthwe
rkshfkser
ekrhse
flehise
ege
etc
sdfksdf
sdfsdkfs
sdfsldf
sdfjsl'
se
ekhfe
efkhesh



9 使用sed删除文件filetxt中所有带有数字的行

sed -i '/[0-9]/d' file.txt


-i 在当前行上面插入文本
使用 -i 选项时,sed 会直接编辑指定的文件,并保存更改。

[root@master ~]# cat file.txt 
sdhfks
345sdfsdg
sdkjfhsdjh
dsf235
35

35235
23452
sdjfsdgf
abck453453lsdfjksegfeabc
ekhkeerjkg32534sekfiabc
ekfhkehhie2355
dfisdgjfsd
fsdfjsdfs
eiofhjeihfe
esjkfhekhfe34534
erjkhekfhie
asekfhiweh
ekhreuikehikfhab345345c
erjkfhei
ekrhksehfguiefg
agkhjdigrjkfthew45345
'esjfhe
ejnkrhehf34534
aejkthehfgwe
kwerjehuirhe34534
ejkrheukyruiew
abcxeklhrkehhabc
erkhef
wekrheh
erjkhekre
kwerhkweh
rjkheghuehjk
[root@master ~]# sed -i '/[0-9]/d' file.txt 
[root@master ~]# cat file.txt 
sdhfks
sdkjfhsdjh

sdjfsdgf
dfisdgjfsd
fsdfjsdfs
eiofhjeihfe
erjkhekfhie
asekfhiweh
erjkfhei
ekrhksehfguiefg
'esjfhe
aejkthehfgwe
ejkrheukyruiew
abcxeklhrkehhabc
erkhef
wekrheh
erjkhekre
kwerhkweh
rjkheghuehjk

10 使用sed 把etc.txt的第1-3行内容另存 testx.txt

sed -n '1,3p' etc.txt > testx.txt


n 读取下一个输入行,用下一个命令处理新的行而不是用第一个命令


[root@master ~]# cat etc.txt
efeg
isd
dskfhjsd
sdfsh

esfhjef
sekfghjse
eskjfhes
sekufhse
awekfhse

ertherfgihwe
elthewhr
rlejrkewf
efkhasfawe

erhjsekejkhfe
sejkrhesf
etjeghwebrgweraw
ankehf

[root@master ~]# sed -n '1,3p' etc.txt > testx.txt
[root@master ~]# cat testx.txt 
efeg
isd
dskfhjsd

11使用sed把etx.txt的第二行替换为hello

sed -i '2s/.*/hello/' etx.txt





-i: 直接编辑文件,而不是将结果输出到标准输出
'2s/.*/hello/': 是一个 sed 脚本,用于定义要执行的替换操作
2: 表示在第二行上执行此操作
s: 表示替换操作
.*: 是一个正则表达式,匹配任何字符(.)零次或多次(*)。在这里,它用于匹配第二行的整行内容
hello: 是替换文本,即要将匹配到的内容替换为 hello




[root@master ~]# cat etx.txt 
dhjfsh
sdfkhsdf
'sdfnksdhf
sdfkhjsdkfs
fkshfsd
sabjfhasd
fsakfghasdf
asmgfsda
faskgfwe
esjkfghesf
setgjasef
askfhaskf
asethbasekfase
efneasf
asefgmsejf
searfbjesf
asefhasef
asjnfsef
asejkfheas'
fnsejf
asefnasef
asekfgwebtwehf
asefksehf
esfgnejasf
asefgkasef
asetfwejrwea
srneasjf
aserneas
fawetnfase'

[root@master ~]# sed -i '2s/.*/hello/' etx.txt 
[root@master ~]# cat etx.txt 
dhjfsh
hello
'sdfnksdhf
sdfkhjsdkfs
fkshfsd
sabjfhasd
fsakfghasdf
asmgfsda
faskgfwe
esjkfghesf
setgjasef
askfhaskf
asethbasekfase
efneasf
asefgmsejf
searfbjesf
asefhasef
asjnfsef
asejkfheas'
fnsejf
asefnasef
asekfgwebtwehf
asefksehf
esfgnejasf
asefgkasef
asetfwejrwea
srneasjf
aserneas
fawetnfase'


12 使用在etx.txt的第二行的前一行,加2行内容hello和world

sed -i '2i\hello world' etx.txt 




'2i\hello world': 是传递给 sed 的编辑脚本
2: 表示第二行
i: 是一个 sed 命令,用于在指定行之前插入文本
\hello world: 这里的 \ 用于转义紧跟其后的换行符,使得 hello world 这一行文本被当作插入命令的一部分,而不是 sed 脚本的结束。

hello world 是要在第二行之前插入的文本


[root@master ~]# sed -i '2i\hello world' etx.txt 
[root@master ~]# cat etx.txt 
dhjfsh
hello world
hello/world
hello/nworld
hello
'sdfnksdhf
sdfkhjsdkfs
fkshfsd
sabjfhasd
fsakfghasdf
asmgfsda
faskgfwe
esjkfghesf
setgjasef
askfhaskf
asethbasekfase
efneasf
asefgmsejf
searfbjesf
asefhasef
asjnfsef
asejkfheas'
fnsejf
asefnasef
asekfgwebtwehf
asefksehf
esfgnejasf
asefgkasef
asetfwejrwea
srneasjf
aserneas
fawetnfase'


13 使用sed在etc.txt 的第二行下一行加入hello


sed -i '3a\hello' etc.txt



'3a\hello': 这是传递给 sed 的编辑脚本
3: 表示第三行
a: 是一个 sed 命令,用于在指定行之后追加文本
\hello: \ 用于转义紧跟其后的换行符,使得 hello 这一行文本被当作追加命令的一部分
hello 是要在第三行之后追加的文本。



[root@master ~]# cat etc.txt 
efeg
isd
dskfhjsd
sdfsh

esfhjef
sekfghjse
eskjfhes
sekufhse
awekfhse

ertherfgihwe
elthewhr
rlejrkewf
efkhasfawe

erhjsekejkhfe
sejkrhesf
etjeghwebrgweraw
ankehf

[root@master ~]# sed -i '3a\hello' etc.txt 
[root@master ~]# cat etc.txt 
efeg
isd
dskfhjsd
hello
sdfsh

esfhjef
sekfghjse
eskjfhes
sekufhse
awekfhse

ertherfgihwe
elthewhr
rlejrkewf
efkhasfawe

erhjsekejkhfe
sejkrhesf
etjeghwebrgweraw
ankehf




14 使用sed 把etx.txt的所有abx改为def

sed -i 's/abx/def/g' etx.txt

全局替换   s/原内容/新内容/g


[root@master ~]# vi etc.txt 
[root@master ~]# cat etc.txt 
efeg
isd
dskfhjsd
hello
sdfsh
abx
esfhjef
sekfghjse
eskjfhes
sekufhse
awekfhse
abx
ertherfgihwe
elthewhr
rlejrkewf
efkhasfawe
abx
erhjsekejkhfe
sejkrhesf
etjeghwebrgweraw
ankehf

[root@master ~]# sed -i 's/abx/def/g' etc.txt 
[root@master ~]# cat etc.txt 
efeg
isd
dskfhjsd
hello
sdfsh
def
esfhjef
sekfghjse
eskjfhes
sekufhse
awekfhse
def
ertherfgihwe
elthewhr
rlejrkewf
efkhasfawe
def
erhjsekejkhfe
sejkrhesf
etjeghwebrgweraw
ankehf



15 使用awk查询file abc结尾的行

awk '/abc$/' file




[root@master ~]# cat file
sdfjhsdjfsdf

sdfklsdfsd

sfksjfljsd

sdfklsdkhjf
etcdfkljfsdjf

sdfkljsdlfl;sdkfsdjfs

sdfljsdklfs
lsdhjfsdj
sdlhfksdf
sdlhfsdflksd
sdfhjsd
sdkfhsdf
sfshjflsdfabc
sfmsdkf
saflsdjfjsf
sfosefje
smfkhsefhase
,sfjsefase
rfeshfesjr
ewrjkwehrweabc
'

eklrjkesfsabc
fnshfjkse
hbejn

[root@master ~]# awk '/abc$/' file
sfshjflsdfabc
ewrjkwehrweabc
eklrjkesfsabc

16 使用awk 取/etc/passwd 第一列数据

awk -F: '{print $1}' /etc/passwd

-F fs   fs指定输入分隔符,fs可以是字符串或正则表达式,如-F:


[root@master ~]# awk -F: '{print $1}' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
avahi-autoipd
dbus
polkitd
tss
postfix
sshd
wang
systemd-network
it101
it1
it2
it3
it4
it5
it6
it7
it8
it9
it10
it11
it12
it13
it14
it15
it16
it17
it18
it19
it20
it21
it22
it23
it24
it25
it26
it27
it28
it29
it30
it31
it32
it33
it34
it35
it36
it37
it38
it39
it40
it41
it42
it43
it44
it45
it46
it47
it48
it49
it50
it51
it52
it53
it54
it55
it56
it57
it58
it59
it60
it61
it62
it63
it64
it65
it66
it67
it68
it69
it70
it71
it72
it73
it74
it75
it76
it77
it78
it79
it80
it81
it82
it83
it84
it85
it86
it87
it88
it89
it90
it91
it92
it93
it94
it95
it96
it97
it98
it99
it100
apache





17 使用awk调换/etc/passwd 第一列和最后一列

awk -F: '{OFS=":"; print $NF,$0}' /etc/passwd |awk -F: '{print $2,$3,$4,$5,$6,$7,$1}'


awk -F: '{temp=$1; $1=$NF; $NF=TEMP; print}' /etc/passwd


awk -F: '{t=$1; $1=$NF; $NF=t; print}' /etc/passwd

{t=$1; $1=$NF; $NF=t; print} 是一个 awk 脚本,
其中t=$1; 将第一列的值存储到临时变量 t 中
$1=$NF; 将最后一列的值赋给第一列
$NF=t; 将原来第一列的值(现在存储在 t 中)赋给最后一列
print 打印修改后的行





awk -F: '{print $1 $NF}' /etc/passwd

-F: 指定了字段分隔符为冒号,$1 表示第一列,$NF 表示最后一列










{t=$1; $1=$NF; $NF=t; print} 是一个 awk 脚本,
其中t=$1; 将第一列的值存储到临时变量 t 中
$1=$NF; 将最后一列的值赋给第一列
$NF=t; 将原来第一列的值(现在存储在 t 中)赋给最后一列
print 打印修改后的行




[root@master ~]# awk -F: '{print $1 $NF}' /etc/passwd
root/bin/bash
bin/sbin/nologin
daemon/sbin/nologin
adm/sbin/nologin
lp/sbin/nologin
sync/bin/sync
shutdown/sbin/shutdown
halt/sbin/halt
mail/sbin/nologin
operator/sbin/nologin
games/sbin/nologin
ftp/sbin/nologin
nobody/sbin/nologin
avahi-autoipd/sbin/nologin
dbus/sbin/nologin
polkitd/sbin/nologin
tss/sbin/nologin
postfix/sbin/nologin
sshd/sbin/nologin
wang/bin/bash
systemd-network/sbin/nologin
it101/bin/bash
it1/bin/bash
it2/bin/bash
it3/bin/bash
it4/bin/bash
it5/bin/bash
it6/bin/bash
it7/bin/bash
it8/bin/bash
it9/bin/bash
it10/bin/bash
it11/bin/bash
it12/bin/bash
it13/bin/bash
it14/bin/bash
it15/bin/bash
it16/bin/bash
it17/bin/bash
it18/bin/bash
it19/bin/bash
it20/bin/bash
it21/bin/bash
it22/bin/bash
it23/bin/bash
it24/bin/bash
it25/bin/bash
it26/bin/bash
it27/bin/bash
it28/bin/bash
it29/bin/bash
it30/bin/bash
it31/bin/bash
it32/bin/bash
it33/bin/bash
it34/bin/bash
it35/bin/bash
it36/bin/bash
it37/bin/bash
it38/bin/bash
it39/bin/bash
it40/bin/bash
it41/bin/bash
it42/bin/bash
it43/bin/bash
it44/bin/bash
it45/bin/bash
it46/bin/bash
it47/bin/bash
it48/bin/bash
it49/bin/bash
it50/bin/bash
it51/bin/bash
it52/bin/bash
it53/bin/bash
it54/bin/bash
it55/bin/bash
it56/bin/bash
it57/bin/bash
it58/bin/bash
it59/bin/bash
it60/bin/bash
it61/bin/bash
it62/bin/bash
it63/bin/bash
it64/bin/bash
it65/bin/bash
it66/bin/bash
it67/bin/bash
it68/bin/bash
it69/bin/bash
it70/bin/bash
it71/bin/bash
it72/bin/bash
it73/bin/bash
it74/bin/bash
it75/bin/bash
it76/bin/bash
it77/bin/bash
it78/bin/bash
it79/bin/bash
it80/bin/bash
it81/bin/bash
it82/bin/bash
it83/bin/bash
it84/bin/bash
it85/bin/bash
it86/bin/bash
it87/bin/bash
it88/bin/bash
it89/bin/bash
it90/bin/bash
it91/bin/bash
it92/bin/bash
it93/bin/bash
it94/bin/bash
it95/bin/bash
it96/bin/bash
it97/bin/bash
it98/bin/bash
it99/bin/bash
it100/bin/bash
apache/sbin/nologin



[root@master ~]# awk -F: '{t=$1; $1=$NF; $NF=t; print}' /etc/passwd
/bin/bash x 0 0 root /root root
/sbin/nologin x 1 1 bin /bin bin
/sbin/nologin x 2 2 daemon /sbin daemon
/sbin/nologin x 3 4 adm /var/adm adm
/sbin/nologin x 4 7 lp /var/spool/lpd lp
/bin/sync x 5 0 sync /sbin sync
/sbin/shutdown x 6 0 shutdown /sbin shutdown
/sbin/halt x 7 0 halt /sbin halt
/sbin/nologin x 8 12 mail /var/spool/mail mail
/sbin/nologin x 11 0 operator /root operator
/sbin/nologin x 12 100 games /usr/games games
/sbin/nologin x 14 50 FTP User /var/ftp ftp
/sbin/nologin x 99 99 Nobody / nobody
/sbin/nologin x 170 170 Avahi IPv4LL Stack /var/lib/avahi-autoipd avahi-autoipd
/sbin/nologin x 81 81 System message bus / dbus
/sbin/nologin x 999 998 User for polkitd / polkitd
/sbin/nologin x 59 59 Account used by the trousers package to sandbox the tcsd daemon /dev/null tss
/sbin/nologin x 89 89  /var/spool/postfix postfix
/sbin/nologin x 74 74 Privilege-separated SSH /var/empty/sshd sshd
/bin/bash x 1000 1000 wang /home/wang wang
/sbin/nologin x 192 192 systemd Network Management / systemd-network
/bin/bash x 1001 1001  /home/it101 it101
/bin/bash x 1002 1002  /home/it1 it1
/bin/bash x 1003 1003  /home/it2 it2
/bin/bash x 1004 1004  /home/it3 it3
/bin/bash x 1005 1005  /home/it4 it4
/bin/bash x 1006 1006  /home/it5 it5
/bin/bash x 1007 1007  /home/it6 it6
/bin/bash x 1008 1008  /home/it7 it7
/bin/bash x 1009 1009  /home/it8 it8
/bin/bash x 1010 1010  /home/it9 it9
/bin/bash x 1011 1011  /home/it10 it10
/bin/bash x 1012 1012  /home/it11 it11
/bin/bash x 1013 1013  /home/it12 it12
/bin/bash x 1014 1014  /home/it13 it13
/bin/bash x 1015 1015  /home/it14 it14
/bin/bash x 1016 1016  /home/it15 it15
/bin/bash x 1017 1017  /home/it16 it16
/bin/bash x 1018 1018  /home/it17 it17
/bin/bash x 1019 1019  /home/it18 it18
/bin/bash x 1020 1020  /home/it19 it19
/bin/bash x 1021 1021  /home/it20 it20
/bin/bash x 1022 1022  /home/it21 it21
/bin/bash x 1023 1023  /home/it22 it22
/bin/bash x 1024 1024  /home/it23 it23
/bin/bash x 1025 1025  /home/it24 it24
/bin/bash x 1026 1026  /home/it25 it25
/bin/bash x 1027 1027  /home/it26 it26
/bin/bash x 1028 1028  /home/it27 it27
/bin/bash x 1029 1029  /home/it28 it28
/bin/bash x 1030 1030  /home/it29 it29
/bin/bash x 1031 1031  /home/it30 it30
/bin/bash x 1032 1032  /home/it31 it31
/bin/bash x 1033 1033  /home/it32 it32
/bin/bash x 1034 1034  /home/it33 it33
/bin/bash x 1035 1035  /home/it34 it34
/bin/bash x 1036 1036  /home/it35 it35
/bin/bash x 1037 1037  /home/it36 it36
/bin/bash x 1038 1038  /home/it37 it37
/bin/bash x 1039 1039  /home/it38 it38
/bin/bash x 1040 1040  /home/it39 it39
/bin/bash x 1041 1041  /home/it40 it40
/bin/bash x 1042 1042  /home/it41 it41
/bin/bash x 1043 1043  /home/it42 it42
/bin/bash x 1044 1044  /home/it43 it43
/bin/bash x 1045 1045  /home/it44 it44
/bin/bash x 1046 1046  /home/it45 it45
/bin/bash x 1047 1047  /home/it46 it46
/bin/bash x 1048 1048  /home/it47 it47
/bin/bash x 1049 1049  /home/it48 it48
/bin/bash x 1050 1050  /home/it49 it49
/bin/bash x 1051 1051  /home/it50 it50
/bin/bash x 1052 1052  /home/it51 it51
/bin/bash x 1053 1053  /home/it52 it52
/bin/bash x 1054 1054  /home/it53 it53
/bin/bash x 1055 1055  /home/it54 it54
/bin/bash x 1056 1056  /home/it55 it55
/bin/bash x 1057 1057  /home/it56 it56
/bin/bash x 1058 1058  /home/it57 it57
/bin/bash x 1059 1059  /home/it58 it58
/bin/bash x 1060 1060  /home/it59 it59
/bin/bash x 1061 1061  /home/it60 it60
/bin/bash x 1062 1062  /home/it61 it61
/bin/bash x 1063 1063  /home/it62 it62
/bin/bash x 1064 1064  /home/it63 it63
/bin/bash x 1065 1065  /home/it64 it64
/bin/bash x 1066 1066  /home/it65 it65
/bin/bash x 1067 1067  /home/it66 it66
/bin/bash x 1068 1068  /home/it67 it67
/bin/bash x 1069 1069  /home/it68 it68
/bin/bash x 1070 1070  /home/it69 it69
/bin/bash x 1071 1071  /home/it70 it70
/bin/bash x 1072 1072  /home/it71 it71
/bin/bash x 1073 1073  /home/it72 it72
/bin/bash x 1074 1074  /home/it73 it73
/bin/bash x 1075 1075  /home/it74 it74
/bin/bash x 1076 1076  /home/it75 it75
/bin/bash x 1077 1077  /home/it76 it76
/bin/bash x 1078 1078  /home/it77 it77
/bin/bash x 1079 1079  /home/it78 it78
/bin/bash x 1080 1080  /home/it79 it79
/bin/bash x 1081 1081  /home/it80 it80
/bin/bash x 1082 1082  /home/it81 it81
/bin/bash x 1083 1083  /home/it82 it82
/bin/bash x 1084 1084  /home/it83 it83
/bin/bash x 1085 1085  /home/it84 it84
/bin/bash x 1086 1086  /home/it85 it85
/bin/bash x 1087 1087  /home/it86 it86
/bin/bash x 1088 1088  /home/it87 it87
/bin/bash x 1089 1089  /home/it88 it88
/bin/bash x 1090 1090  /home/it89 it89
/bin/bash x 1091 1091  /home/it90 it90
/bin/bash x 1092 1092  /home/it91 it91
/bin/bash x 1093 1093  /home/it92 it92
/bin/bash x 1094 1094  /home/it93 it93
/bin/bash x 1095 1095  /home/it94 it94
/bin/bash x 1096 1096  /home/it95 it95
/bin/bash x 1097 1097  /home/it96 it96
/bin/bash x 1098 1098  /home/it97 it97
/bin/bash x 1099 1099  /home/it98 it98
/bin/bash x 1100 1100  /home/it99 it99
/bin/bash x 1101 1101  /home/it100 it100
/sbin/nologin x 48 48 Apache /usr/share/httpd apache



[root@master ~]# awk -F: '{ temp=$1; $1=$NF; $NF=temp; print }' /etc/passwd
/bin/bash x 0 0 root /root root
/sbin/nologin x 1 1 bin /bin bin
/sbin/nologin x 2 2 daemon /sbin daemon
/sbin/nologin x 3 4 adm /var/adm adm
/sbin/nologin x 4 7 lp /var/spool/lpd lp
/bin/sync x 5 0 sync /sbin sync
/sbin/shutdown x 6 0 shutdown /sbin shutdown
/sbin/halt x 7 0 halt /sbin halt
/sbin/nologin x 8 12 mail /var/spool/mail mail
/sbin/nologin x 11 0 operator /root operator
/sbin/nologin x 12 100 games /usr/games games
/sbin/nologin x 14 50 FTP User /var/ftp ftp
/sbin/nologin x 99 99 Nobody / nobody
/sbin/nologin x 170 170 Avahi IPv4LL Stack /var/lib/avahi-autoipd avahi-autoipd
/sbin/nologin x 81 81 System message bus / dbus
/sbin/nologin x 999 998 User for polkitd / polkitd
/sbin/nologin x 59 59 Account used by the trousers package to sandbox the tcsd daemon /dev/null tss
/sbin/nologin x 89 89  /var/spool/postfix postfix
/sbin/nologin x 74 74 Privilege-separated SSH /var/empty/sshd sshd
/bin/bash x 1000 1000 wang /home/wang wang
/sbin/nologin x 192 192 systemd Network Management / systemd-network
/bin/bash x 1001 1001  /home/it101 it101
/bin/bash x 1002 1002  /home/it1 it1
/bin/bash x 1003 1003  /home/it2 it2
/bin/bash x 1004 1004  /home/it3 it3
/bin/bash x 1005 1005  /home/it4 it4
/bin/bash x 1006 1006  /home/it5 it5
/bin/bash x 1007 1007  /home/it6 it6
/bin/bash x 1008 1008  /home/it7 it7
/bin/bash x 1009 1009  /home/it8 it8
/bin/bash x 1010 1010  /home/it9 it9
/bin/bash x 1011 1011  /home/it10 it10
/bin/bash x 1012 1012  /home/it11 it11
/bin/bash x 1013 1013  /home/it12 it12
/bin/bash x 1014 1014  /home/it13 it13
/bin/bash x 1015 1015  /home/it14 it14
/bin/bash x 1016 1016  /home/it15 it15
/bin/bash x 1017 1017  /home/it16 it16
/bin/bash x 1018 1018  /home/it17 it17
/bin/bash x 1019 1019  /home/it18 it18
/bin/bash x 1020 1020  /home/it19 it19
/bin/bash x 1021 1021  /home/it20 it20
/bin/bash x 1022 1022  /home/it21 it21
/bin/bash x 1023 1023  /home/it22 it22
/bin/bash x 1024 1024  /home/it23 it23
/bin/bash x 1025 1025  /home/it24 it24
/bin/bash x 1026 1026  /home/it25 it25
/bin/bash x 1027 1027  /home/it26 it26
/bin/bash x 1028 1028  /home/it27 it27
/bin/bash x 1029 1029  /home/it28 it28
/bin/bash x 1030 1030  /home/it29 it29
/bin/bash x 1031 1031  /home/it30 it30
/bin/bash x 1032 1032  /home/it31 it31
/bin/bash x 1033 1033  /home/it32 it32
/bin/bash x 1034 1034  /home/it33 it33
/bin/bash x 1035 1035  /home/it34 it34
/bin/bash x 1036 1036  /home/it35 it35
/bin/bash x 1037 1037  /home/it36 it36
/bin/bash x 1038 1038  /home/it37 it37
/bin/bash x 1039 1039  /home/it38 it38
/bin/bash x 1040 1040  /home/it39 it39
/bin/bash x 1041 1041  /home/it40 it40
/bin/bash x 1042 1042  /home/it41 it41
/bin/bash x 1043 1043  /home/it42 it42
/bin/bash x 1044 1044  /home/it43 it43
/bin/bash x 1045 1045  /home/it44 it44
/bin/bash x 1046 1046  /home/it45 it45
/bin/bash x 1047 1047  /home/it46 it46
/bin/bash x 1048 1048  /home/it47 it47
/bin/bash x 1049 1049  /home/it48 it48
/bin/bash x 1050 1050  /home/it49 it49
/bin/bash x 1051 1051  /home/it50 it50
/bin/bash x 1052 1052  /home/it51 it51
/bin/bash x 1053 1053  /home/it52 it52
/bin/bash x 1054 1054  /home/it53 it53
/bin/bash x 1055 1055  /home/it54 it54
/bin/bash x 1056 1056  /home/it55 it55
/bin/bash x 1057 1057  /home/it56 it56
/bin/bash x 1058 1058  /home/it57 it57
/bin/bash x 1059 1059  /home/it58 it58
/bin/bash x 1060 1060  /home/it59 it59
/bin/bash x 1061 1061  /home/it60 it60
/bin/bash x 1062 1062  /home/it61 it61
/bin/bash x 1063 1063  /home/it62 it62
/bin/bash x 1064 1064  /home/it63 it63
/bin/bash x 1065 1065  /home/it64 it64
/bin/bash x 1066 1066  /home/it65 it65
/bin/bash x 1067 1067  /home/it66 it66
/bin/bash x 1068 1068  /home/it67 it67
/bin/bash x 1069 1069  /home/it68 it68
/bin/bash x 1070 1070  /home/it69 it69
/bin/bash x 1071 1071  /home/it70 it70
/bin/bash x 1072 1072  /home/it71 it71
/bin/bash x 1073 1073  /home/it72 it72
/bin/bash x 1074 1074  /home/it73 it73
/bin/bash x 1075 1075  /home/it74 it74
/bin/bash x 1076 1076  /home/it75 it75
/bin/bash x 1077 1077  /home/it76 it76
/bin/bash x 1078 1078  /home/it77 it77
/bin/bash x 1079 1079  /home/it78 it78
/bin/bash x 1080 1080  /home/it79 it79
/bin/bash x 1081 1081  /home/it80 it80
/bin/bash x 1082 1082  /home/it81 it81
/bin/bash x 1083 1083  /home/it82 it82
/bin/bash x 1084 1084  /home/it83 it83
/bin/bash x 1085 1085  /home/it84 it84
/bin/bash x 1086 1086  /home/it85 it85
/bin/bash x 1087 1087  /home/it86 it86
/bin/bash x 1088 1088  /home/it87 it87
/bin/bash x 1089 1089  /home/it88 it88
/bin/bash x 1090 1090  /home/it89 it89
/bin/bash x 1091 1091  /home/it90 it90
/bin/bash x 1092 1092  /home/it91 it91
/bin/bash x 1093 1093  /home/it92 it92
/bin/bash x 1094 1094  /home/it93 it93
/bin/bash x 1095 1095  /home/it94 it94
/bin/bash x 1096 1096  /home/it95 it95
/bin/bash x 1097 1097  /home/it96 it96
/bin/bash x 1098 1098  /home/it97 it97
/bin/bash x 1099 1099  /home/it98 it98
/bin/bash x 1100 1100  /home/it99 it99
/bin/bash x 1101 1101  /home/it100 it100
/sbin/nologin x 48 48 Apache /usr/share/httpd apache

18 查看tcp连接去情况:


netstat -an
netstat -tulpn
ss -tulpn




[root@master ~]# netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp        0     36 10.0.1.134:22           10.0.1.1:8846           ESTABLISHED
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
udp        0      0 0.0.0.0:68              0.0.0.0:*                          
udp        0      0 0.0.0.0:26927           0.0.0.0:*                          
udp6       0      0 :::14999                :::*                               
raw6       0      0 :::58                   :::*                    7          
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     7948     /run/systemd/journal/stdout
unix  5      [ ]         DGRAM                    7951     /run/systemd/journal/socket
unix  12     [ ]         DGRAM                    7953     /dev/log
unix  2      [ ACC ]     STREAM     LISTENING     17758    private/proxywrite
unix  2      [ ACC ]     STREAM     LISTENING     17761    private/smtp
unix  2      [ ACC ]     STREAM     LISTENING     17764    private/relay
unix  2      [ ACC ]     STREAM     LISTENING     17770    private/error
unix  2      [ ACC ]     STREAM     LISTENING     17773    private/retry
unix  2      [ ACC ]     STREAM     LISTENING     17776    private/discard
unix  2      [ ACC ]     STREAM     LISTENING     17779    private/local
unix  2      [ ACC ]     STREAM     LISTENING     17782    private/virtual
unix  2      [ ACC ]     STREAM     LISTENING     17785    private/lmtp
unix  2      [ ACC ]     STREAM     LISTENING     17788    private/anvil
unix  2      [ ACC ]     STREAM     LISTENING     17791    private/scache
unix  2      [ ACC ]     STREAM     LISTENING     12850    /run/systemd/private
unix  2      [ ACC ]     SEQPACKET  LISTENING     12866    /run/udev/control
unix  2      [ ]         DGRAM                    12868    /run/systemd/shutdownd
unix  2      [ ACC ]     STREAM     LISTENING     15687    /var/run/NetworkManager/private-dhcp
unix  2      [ ACC ]     STREAM     LISTENING     15488    /var/run/NetworkManager/private
unix  2      [ ACC ]     STREAM     LISTENING     17746    private/trace
unix  2      [ ACC ]     STREAM     LISTENING     17749    private/verify
unix  2      [ ACC ]     STREAM     LISTENING     14733    /var/run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     17755    private/proxymap
unix  2      [ ACC ]     STREAM     LISTENING     17737    private/rewrite
unix  2      [ ACC ]     STREAM     LISTENING     17740    private/bounce
unix  2      [ ACC ]     STREAM     LISTENING     17743    private/defer
unix  2      [ ACC ]     STREAM     LISTENING     17734    private/tlsmgr
unix  2      [ ACC ]     STREAM     LISTENING     17723    public/pickup
unix  2      [ ACC ]     STREAM     LISTENING     17727    public/cleanup
unix  2      [ ACC ]     STREAM     LISTENING     17730    public/qmgr
unix  2      [ ACC ]     STREAM     LISTENING     17752    public/flush
unix  2      [ ACC ]     STREAM     LISTENING     17767    public/showq
unix  3      [ ]         DGRAM                    7928     /run/systemd/notify
unix  2      [ ]         DGRAM                    7930     /run/systemd/cgroups-agent
unix  3      [ ]         STREAM     CONNECTED     17774    
unix  3      [ ]         STREAM     CONNECTED     14998    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     17721    
unix  3      [ ]         STREAM     CONNECTED     17793    
unix  3      [ ]         STREAM     CONNECTED     17759    
unix  3      [ ]         STREAM     CONNECTED     15734    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     14850    
unix  3      [ ]         STREAM     CONNECTED     13570    
unix  3      [ ]         STREAM     CONNECTED     17736    
unix  3      [ ]         STREAM     CONNECTED     17722    
unix  3      [ ]         STREAM     CONNECTED     17789    
unix  3      [ ]         STREAM     CONNECTED     17748    
unix  3      [ ]         STREAM     CONNECTED     17731    
unix  3      [ ]         STREAM     CONNECTED     16385    /run/systemd/journal/stdout
unix  2      [ ]         DGRAM                    13628    
unix  3      [ ]         STREAM     CONNECTED     15765    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     17778    
unix  3      [ ]         STREAM     CONNECTED     15086    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     14794    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     17744    
unix  3      [ ]         STREAM     CONNECTED     17763    
unix  3      [ ]         STREAM     CONNECTED     15491    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     17756    
unix  3      [ ]         STREAM     CONNECTED     17739    
unix  3      [ ]         STREAM     CONNECTED     17790    
unix  3      [ ]         STREAM     CONNECTED     13571    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     16474    
unix  2      [ ]         DGRAM                    15610    
unix  3      [ ]         STREAM     CONNECTED     17786    
unix  3      [ ]         STREAM     CONNECTED     17769    
unix  3      [ ]         STREAM     CONNECTED     14793    
unix  3      [ ]         STREAM     CONNECTED     17735    
unix  3      [ ]         STREAM     CONNECTED     17724    
unix  3      [ ]         STREAM     CONNECTED     17747    
unix  3      [ ]         STREAM     CONNECTED     17781    
unix  3      [ ]         DGRAM                    13712    
unix  3      [ ]         STREAM     CONNECTED     15216    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     15764    
unix  2      [ ]         DGRAM                    15475    
unix  3      [ ]         STREAM     CONNECTED     17751    
unix  3      [ ]         STREAM     CONNECTED     17732    
unix  2      [ ]         DGRAM                    17894    
unix  3      [ ]         STREAM     CONNECTED     17777    
unix  3      [ ]         STREAM     CONNECTED     17760    
unix  3      [ ]         STREAM     CONNECTED     15490    
unix  3      [ ]         STREAM     CONNECTED     15085    
unix  3      [ ]         STREAM     CONNECTED     15215    
unix  3      [ ]         STREAM     CONNECTED     17757    
unix  3      [ ]         STREAM     CONNECTED     16384    
unix  3      [ ]         STREAM     CONNECTED     17742    
unix  3      [ ]         STREAM     CONNECTED     17729    
unix  3      [ ]         STREAM     CONNECTED     14753    
unix  3      [ ]         STREAM     CONNECTED     16475    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     17787    
unix  3      [ ]         STREAM     CONNECTED     17753    
unix  3      [ ]         STREAM     CONNECTED     17768    
unix  3      [ ]         STREAM     CONNECTED     17772    
unix  3      [ ]         STREAM     CONNECTED     17725    
unix  3      [ ]         STREAM     CONNECTED     14997    
unix  3      [ ]         STREAM     CONNECTED     17738    
unix  3      [ ]         STREAM     CONNECTED     17765    
unix  3      [ ]         STREAM     CONNECTED     15320    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     14851    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     17750    
unix  3      [ ]         STREAM     CONNECTED     17784    
unix  2      [ ]         DGRAM                    15196    
unix  3      [ ]         STREAM     CONNECTED     14849    
unix  2      [ ]         DGRAM                    48047    
unix  3      [ ]         STREAM     CONNECTED     15495    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     17780    
unix  3      [ ]         STREAM     CONNECTED     15041    /run/systemd/journal/stdout
unix  2      [ ]         DGRAM                    17696    
unix  2      [ ]         DGRAM                    15432    
unix  3      [ ]         STREAM     CONNECTED     15040    
unix  3      [ ]         STREAM     CONNECTED     17741    
unix  3      [ ]         STREAM     CONNECTED     17792    
unix  3      [ ]         STREAM     CONNECTED     17775    
unix  2      [ ]         DGRAM                    14645    
unix  3      [ ]         STREAM     CONNECTED     15733    
unix  3      [ ]         DGRAM                    13711    
unix  3      [ ]         STREAM     CONNECTED     17771    
unix  2      [ ]         DGRAM                    15735    
unix  3      [ ]         STREAM     CONNECTED     17754    
unix  3      [ ]         STREAM     CONNECTED     17728    
unix  2      [ ]         DGRAM                    15739    
unix  3      [ ]         STREAM     CONNECTED     15319    
unix  3      [ ]         STREAM     CONNECTED     17766    
unix  3      [ ]         STREAM     CONNECTED     17783    
unix  2      [ ]         DGRAM                    13124    
unix  3      [ ]         STREAM     CONNECTED     15494    
unix  2      [ ]         DGRAM                    18281    
unix  2      [ ]         DGRAM                    13248    
unix  3      [ ]         STREAM     CONNECTED     17762    
unix  3      [ ]         STREAM     CONNECTED     17745    
[root@master ~]# netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      969/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2077/master         
tcp6       0      0 :::22                   :::*                    LISTEN      969/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      2077/master         
udp        0      0 0.0.0.0:68              0.0.0.0:*                           776/dhclient        
udp        0      0 0.0.0.0:26927           0.0.0.0:*                           776/dhclient        
udp6       0      0 :::14999                :::*                                776/dhclient        
[root@master ~]# ss -tulpn
Netid  State      Recv-Q Send-Q             Local Address:Port               Peer Address:Port 
tcp    UNCONN     0      0                              *:68                            *:*      users:(("dhclient",776,6))
tcp    UNCONN     0      0                              *:26927                         *:*      users:(("dhclient",776,20))
tcp    UNCONN     0      0                             :::14999                        :::*      users:(("dhclient",776,21))
tcp    LISTEN     0      128                            *:22                            *:*      users:(("sshd",969,3))
tcp    LISTEN     0      100                    127.0.0.1:25                            *:*      users:(("master",2077,13))
tcp    LISTEN     0      128                           :::22                           :::*      users:(("sshd",969,4))
tcp    LISTEN     0      100                          ::1:25                           :::*      users:(("master",2077,14))


grep命令用法

-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 只输出文件中匹配到的部分。



sed命令用法

a\ 在当前行下面插入文本。
i\ 在当前行上面插入文本。
c\ 把选定的行改为新的文本。
d 删除,删除选择的行。
D 删除模板块的第一行。
s 替换指定字符
h 拷贝模板块的内容到内存中的缓冲区。
H 追加模板块的内容到内存中的缓冲区。
g 获得内存缓冲区的内容,并替代当前模板块中的文本。
G 获得内存缓冲区的内容,并追加到当前模板块文本的后面。
l 列表不能打印字符的清单。
n 读取下一个输入行,用下一个命令处理新的行而不是用第一个命令。
N 追加下一个输入行到模板块后面并在二者间嵌入一个新行,改变当前行号码。
p 打印模板块的行。
P(大写) 打印模板块的第一行。
q 退出Sed。
b lable 分支到脚本中带有标记的地方,如果分支不存在则分支到脚本的末尾。
r file 从file中读行。
t label if分支,从最后一行开始,条件一旦满足或者T,t命令,将导致分支到带有标号的命令处,或者到脚本的末尾。
T label 错误分支,从最后一行开始,一旦发生错误或者T,t命令,将导致分支到带有标号的命令处,或者到脚本的末尾。
w file 写并追加模板块到file末尾。  
W file 写并追加模板块的第一行到file末尾。  
! 表示后面的命令对所有没有被选定的行发生作用。  
= 打印当前行号码。  
# 把注释扩展到下一个换行符以前。  




sed替换标记
g 表示行内全面替换。  
p 表示打印行。  
w 表示把行写入一个文件。  
x 表示互换模板块中的文本和缓冲区中的文本。  
y 表示把一个字符翻译为另外的字符(但是不用于正则表达式)
\1 子串匹配标记
& 已匹配字符串标记





sed元字符集
^ 匹配行开始,如:/^sed/匹配所有以sed开头的行。
$ 匹配行结束,如:/sed$/匹配所有以sed结尾的行。
. 匹配一个非换行符的任意字符,如:/s.d/匹配s后接一个任意字符,最后是d。
* 匹配0个或多个字符,如:/*sed/匹配所有模板是一个或多个空格后紧跟sed的行。
[] 匹配一个指定范围内的字符,如/[ss]ed/匹配sed和Sed。  
[^] 匹配一个不在指定范围内的字符,如:/[^A-RT-Z]ed/匹配不包含A-R和T-Z的一个字母开头,紧跟ed的行。
\(..\) 匹配子串,保存匹配的字符,如s/\(love\)able/\1rs,loveable被替换成lovers。
& 保存搜索字符用来替换其他字符,如s/love/**&**/,love这成**love**。
\< 匹配单词的开始,如:/\<love/匹配包含以love开头的单词的行。
\> 匹配单词的结束,如/love\>/匹配包含以love结尾的单词的行。
x\{m\} 重复字符x,m次,如:/0\{5\}/匹配包含5个0的行。
x\{m,\} 重复字符x,至少m次,如:/0\{5,\}/匹配至少有5个0的行。
x\{m,n\} 重复字符x,至少m次,不多于n次,如:/0\{5,10\}/匹配5~10个0的行。


sed用法实例

sed用法实例
替换操作:s命令
替换文本中的字符串:

sed 's/book/books/' file
-n选项和p命令一起使用表示只打印那些发生替换的行:

sed -n 's/test/TEST/p' file

直接编辑文件选项-i,会匹配file文件中每一行的第一个book替换为books:

sed -i 's/book/books/g' file
全面替换标记g
使用后缀 /g 标记会替换每一行中的所有匹配:

sed 's/book/books/g' file
当需要从第N处匹配开始替换时,可以使用 /Ng:

echo sksksksksksk | sed 's/sk/SK/2g'
skSKSKSKSKSK

echo sksksksksksk | sed 's/sk/SK/3g'
skskSKSKSKSK

echo sksksksksksk | sed 's/sk/SK/4g'
skskskSKSKSK
定界符
以上命令中字符 / 在sed中作为定界符使用,也可以使用任意的定界符:

sed 's:test:TEXT:g'
sed 's|test|TEXT|g'
定界符出现在样式内部时,需要进行转义:

sed 's/\/bin/\/usr\/local\/bin/g'
删除操作:d命令
删除空白行:

sed '/^$/d' file
删除文件的第2行:

sed '2d' file
删除文件的第2行到末尾所有行:

sed '2,$d' file
删除文件最后一行:

sed '$d' file
删除文件中所有开头是test的行:

sed '/^test/'d file
已匹配字符串标记&
正则表达式 \w\+ 匹配每一个单词,使用 [&] 替换它,& 对应于之前所匹配到的单词:

echo this is a test line | sed 's/\w\+/[&]/g'
[this] [is] [a] [test] [line]
所有以192.168.0.1开头的行都会被替换成它自已加localhost:

sed 's/^192.168.0.1/&localhost/' file
192.168.0.1localhost
子串匹配标记\1
匹配给定样式的其中一部分:

echo this is digit 7 in a number | sed 's/digit \([0-9]\)/\1/'
this is 7 in a number
命令中 digit 7,被替换成了 7。样式匹配到的子串是 7,\(..\) 用于匹配子串,对于匹配到的第一个子串就标记为 \1,依此类推匹配到的第二个结果就是 \2,例如:

echo aaa BBB | sed 's/\([a-z]\+\) \([A-Z]\+\)/\2 \1/'
BBB aaa
love被标记为1,所有loveable会被替换成lovers,并打印出来:

sed -n 's/\(love\)able/\1rs/p' file
组合多个表达式
sed '表达式' | sed '表达式'

等价于:

sed '表达式; 表达式'
引用
sed表达式可以使用单引号来引用,但是如果表达式内部包含变量字符串,就需要使用双引号。

test=hello
echo hello WORLD | sed "s/$test/HELLO"
HELLO WORLD
选定行的范围:,(逗号)
所有在模板test和check所确定的范围内的行都被打印:

sed -n '/test/,/check/p' file
打印从第5行开始到第一个包含以test开始的行之间的所有行:

sed -n '5,/^test/p' file
对于模板test和west之间的行,每行的末尾用字符串aaa bbb替换:

sed '/test/,/west/s/$/aaa bbb/' file
多点编辑:e命令
-e选项允许在同一行里执行多条命令:

sed -e '1,5d' -e 's/test/check/' file
上面sed表达式的第一条命令删除1至5行,第二条命令用check替换test。命令的执行顺序对结果有影响。如果两个命令都是替换命令,那么第一个替换命令将影响第二个替换命令的结果。

和 -e 等价的命令是 --expression:

sed --expression='s/test/check/' --expression='/love/d' file
从文件读入:r命令
file里的内容被读进来,显示在与test匹配的行后面,如果匹配多行,则file的内容将显示在所有匹配行的下面:

sed '/test/r file' filename
写入文件:w命令  
在example中所有包含test的行都被写入file里:

sed -n '/test/w file' example
追加(行下):a\命令
将 this is a test line 追加到 以test 开头的行后面:

sed '/^test/a\this is a test line' file
在 test.conf 文件第2行之后插入 this is a test line:

sed -i '2a\this is a test line' test.conf
插入(行上):i\命令
将 this is a test line 追加到以test开头的行前面:

sed '/^test/i\this is a test line' file
在test.conf文件第5行之前插入this is a test line:

sed -i '5i\this is a test line' test.conf
下一个:n命令
如果test被匹配,则移动到匹配行的下一行,替换这一行的aa,变为bb,并打印该行,然后继续:

sed '/test/{ n; s/aa/bb/; }' file
变形:y命令
把1~10行内所有abcde转变为大写,注意,正则表达式元字符不能使用这个命令:

sed '1,10y/abcde/ABCDE/' file
退出:q命令
打印完第10行后,退出sed

sed '10q' file
保持和获取:h命令和G命令
在sed处理文件的时候,每一行都被保存在一个叫模式空间的临时缓冲区中,除非行被删除或者输出被取消,否则所有被处理的行都将 打印在屏幕上。接着模式空间被清空,并存入新的一行等待处理。

sed -e '/test/h' -e '$G' file
在这个例子里,匹配test的行被找到后,将存入模式空间,h命令将其复制并存入一个称为保持缓存区的特殊缓冲区内。第二条语句的意思是,当到达最后一行后,G命令取出保持缓冲区的行,然后把它放回模式空间中,且追加到现在已经存在于模式空间中的行的末尾。在这个例子中就是追加到最后一行。简单来说,任何包含test的行都被复制并追加到该文件的末尾。

保持和互换:h命令和x命令
互换模式空间和保持缓冲区的内容。也就是把包含test与check的行互换:

sed -e '/test/h' -e '/check/x' file
脚本scriptfile
sed脚本是一个sed的命令清单,启动Sed时以-f选项引导脚本文件名。Sed对于脚本中输入的命令非常挑剔,在命令的末尾不能有任何空白或文本,如果在一行中有多个命令,要用分号分隔。以#开头的行为注释行,且不能跨行。

sed [options] -f scriptfile file(s)
打印奇数行或偶数行
方法1:

sed -n 'p;n' test.txt  #奇数行
sed -n 'n;p' test.txt  #偶数行
方法2:

sed -n '1~2p' test.txt  #奇数行
sed -n '2~2p' test.txt  #偶数行
打印匹配字符串的下一行
grep -A 1 SCC URFILE
sed -n '/SCC/{n;p}' URFILE
awk '/SCC/{getline; print}' URFILE


awk命令用法

常用命令选项

-F fs   fs指定输入分隔符,fs可以是字符串或正则表达式,如-F:
-v var=value   赋值一个用户定义变量,将外部变量传递给awk
-f scripfile  从脚本文件中读取awk命令
-m[fr] val   对val值设置内在限制,-mf选项限制分配给val的最大块数目;-mr选项限制记录的最大数目。这两个功能是Bell实验室版awk的扩展功能,在标准awk中不适用。
awk模式和操作





find命令用法






`find` 命令在Linux中是一个极其强大的工具,用于在文件系统中搜索符合特定条件的文件和目录。以下是 `find` 命令一些常用的选项:

1. **-name**:按名称查找文件。例如,查找所有扩展名为 `.txt` 的文件:
   ```bash
   find /path/to/search -name "*.txt"
  1. -type:按照文件类型查找。比如查找所有普通文件:

    find /path/to/search -type f
    

    或者查找所有目录:

    find /path/to/search -type d
    
  2. -size:按照文件大小查找。例如查找大于1MB的文件:

    find /path/to/search -size +1M
    
  3. -mtime/-atime/-ctime:按文件修改时间、访问时间和状态改变时间查找。例如查找过去7天内修改过的文件:

    find /path/to/search -mtime -7
    
  4. -perm:按权限查找。例如查找权限位为755的所有文件:

    find /path/to/search -perm 755
    
  5. -user-group:按照文件所有者和组查找。例如查找属于特定用户的所有文件:

    find /path/to/search -user username
    
  6. -exec-ok:执行命令对搜索结果进行操作。例如删除所有临时文件:

    find /tmp -name "temp*" -exec rm -f {} +
    
  7. -empty:查找空文件或空目录:

    find /path/to/search -empty
    
  8. -maxdepth-mindepth:限制搜索深度。例如只查找当前目录下的文件,不进入子目录:

    find . -maxdepth 1 -name "*.log"
    
  9. -newer:查找比指定文件新(或者旧)的文件:

    find . -newer reference_file
    
  10. -depth:先处理子目录中的文件,再处理目录自身。

这些只是 find 命令众多选项中的一部分,实际应用中可根据需要组合不同选项实现复杂的文件搜索和处理任务。

-amin<分钟>:查找在指定时间曾被存取过的文件或目录,单位以分钟计算;
-anewer<参考文件或目录>:查找其存取时间较指定文件或目录的存取时间更接近现在的文件或目录;
-atime<24小时数>:查找在指定时间曾被存取过的文件或目录,单位以24小时计算;
-cmin<分钟>:查找在指定时间之时被更改过的文件或目录;
-cnewer<参考文件或目录>查找其更改时间较指定文件或目录的更改时间更接近现在的文件或目录;
-ctime<24小时数>:查找在指定时间之时被更改的文件或目录,单位以24小时计算;
-daystart:从本日开始计算时间;
-depth:从指定目录下最深层的子目录开始查找;
-empty:寻找文件大小为0 Byte的文件,或目录下没有任何子目录或文件的空目录;
-exec<执行指令>:假设find指令的回传值为True,就执行该指令;
-false:将find指令的回传值皆设为False;
-fls<列表文件>:此参数的效果和指定“-ls”参数类似,但会把结果保存为指定的列表文件;
-follow:排除符号连接;
-fprint<列表文件>:此参数的效果和指定“-print”参数类似,但会把结果保存成指定的列表文件;
-fprint0<列表文件>:此参数的效果和指定“-print0”参数类似,但会把结果保存成指定的列表文件;
-fprintf<列表文件><输出格式>:此参数的效果和指定“-printf”参数类似,但会把结果保存成指定的列表文件;
-fstype<文件系统类型>:只寻找该文件系统类型下的文件或目录;
-gid<群组识别码>:查找符合指定之群组识别码的文件或目录;
-group<群组名称>:查找符合指定之群组名称的文件或目录;
-help或——help:在线帮助;
-ilname<范本样式>:此参数的效果和指定“-lname”参数类似,但忽略字符大小写的差别;
-iname<范本样式>:此参数的效果和指定“-name”参数类似,但忽略字符大小写的差别;
-inum<inode编号>:查找符合指定的inode编号的文件或目录;
-ipath<范本样式>:此参数的效果和指定“-path”参数类似,但忽略字符大小写的差别;
-iregex<范本样式>:此参数的效果和指定“-regexe”参数类似,但忽略字符大小写的差别;
-links<连接数目>:查找符合指定的硬连接数目的文件或目录;
-iname<范本样式>:指定字符串作为寻找符号连接的范本样式;
-ls:假设find指令的回传值为Ture,就将文件或目录名称列出到标准输出;
-maxdepth<目录层级>:设置最大目录层级;
-mindepth<目录层级>:设置最小目录层级;
-mmin<分钟>:查找在指定时间曾被更改过的文件或目录,单位以分钟计算;
-mount:此参数的效果和指定“-xdev”相同;
-mtime<24小时数>:查找在指定时间曾被更改过的文件或目录,单位以24小时计算;
-name<范本样式>:指定字符串作为寻找文件或目录的范本样式;
-newer<参考文件或目录>:查找其更改时间较指定文件或目录的更改时间更接近现在的文件或目录;
-nogroup:找出不属于本地主机群组识别码的文件或目录;
-noleaf:不去考虑目录至少需拥有两个硬连接存在;
-nouser:找出不属于本地主机用户识别码的文件或目录;
-ok<执行指令>:此参数的效果和指定“-exec”类似,但在执行指令之前会先询问用户,若回答“y”或“Y”,则放弃执行命令;
-path<范本样式>:指定字符串作为寻找目录的范本样式;
-perm<权限数值>:查找符合指定的权限数值的文件或目录;
-print:假设find指令的回传值为Ture,就将文件或目录名称列出到标准输出。格式为每列一个名称,每个名称前皆有“./”字符串;
-print0:假设find指令的回传值为Ture,就将文件或目录名称列出到标准输出。格式为全部的名称皆在同一行;
-printf<输出格式>:假设find指令的回传值为Ture,就将文件或目录名称列出到标准输出。格式可以自行指定;
-prune:不寻找字符串作为寻找文件或目录的范本样式;
-regex<范本样式>:指定字符串作为寻找文件或目录的范本样式;
-size<文件大小>:查找符合指定的文件大小的文件;
-true:将find指令的回传值皆设为True;
-type<文件类型>:只寻找符合指定的文件类型的文件;
-uid<用户识别码>:查找符合指定的用户识别码的文件或目录;
-used<日数>:查找文件或目录被更改之后在指定时间曾被存取过的文件或目录,单位以日计算;
-user<拥有者名称>:查找符和指定的拥有者名称的文件或目录;
-version或——version:显示版本信息;
-xdev:将范围局限在先行的文件系统中;
-xtype<文件类型>:此参数的效果和指定“-type”参数类似,差别在于它针对符号连接检查。

标签:bin,sed,STREAM,第六,unix,剑客,home,bash
From: https://www.cnblogs.com/nwq1101/p/18075559

相关文章

  • 3月14-第五讲复习回顾和第六讲TCP协议
    结合gpt和其他方面的资料,对于昨天的网络层和链路层做出补充:1.数据传输时两层基本都经过,网络层规划路由表(路由器跳转路径),装配ip地址(用来规划线路),封装和传输数据包在节点与目标设备之间,把链路层的数据帧转换为数据包。链路层则是节点之间的,物理层面,使用Mac这种物理地址定位。2.注......
  • 第六章-Sentinel 流量守卫
    第六章Sentinel流量守卫在微服务远程调用的过程中,还存在几个问题需要解决:业务健壮性问题:consumer调用provider,provider故障会导致consumer也出现故障在cart-service中,需要调用item-service中的查询商品信息接口。如果查询商品信息接口出现故障,会导致cart-service也出现故障......
  • 第六章二叉树——二叉树的最大深度
    吾日三省吾身还记得的梦想吗正在努力实现它吗可以坚持下去吗目录吾日三省吾身力扣题号:104.二叉树的最大深度-力扣(LeetCode)题目描述:思路解法一:递归实现思路代码逻辑解释注意事项代码实现内存优化总结(╯°□°)╯︵┻━┻(⌐■_■)(¬‿¬)(´・ω・`)(͡°͜......
  • 四剑客第五关
    四剑客第五关1查询file.txt以abc结尾的行grep'abc$'file.txt^开头$结尾[root@master~]#catfile.txtsdhfkssdfsdgsdkjfhsdjhsdjfsdgfabcklsdfjksegfeabcekhkeerjkgsekfiabcekfhkehhiedfisdgjfsdfsdfjsdfseiofhjeihfeesjkfhekhfeerjkhekfhieasekfhi......
  • 四剑客第四关
    四剑客第四关1、说一些你比较常用linux指令lsls-llsblkcatechottyhostnamehostnamectl-set-nameipapingifconfignetstat-tulpnss-tulpnpsauxkillkill-9lsofcdmkdirrm-rfrm-fcpmvfindgreppspaa......
  • 四剑客第三关
    四剑客第三关1find使用方法:根据文件大小查找命令:根据文件权限查找数据:find/etc/passwd-typef-size+10Mfind/etc/passwd-perm755[root@master~]#find/etc/passwd-typef-size+10M[root@master~]#find/etc/passwd-perm7552把abc压缩成格式......
  • 四剑客第一关
    四剑客第一关1.修改当前时间为明天日期date-s"2024-03-14"date-s"$(date-dtomorrow+%Y-%m-%d)"修改date-s"$(date-dtomorrow+%Y-%m-%d)"[root@master~]#date-s"2024-03-13"WedMar1300:00:00CST2024[root@master~]#d......
  • 第六组 第二周需求分析2
    标题:便捷速享——“易餐宝”引领外卖新风尚副标题:专为忙碌的都市人打造,一键下单,美食速达,享受无忧生活总结:“易餐宝”是一款高效便捷的外卖软件,为忙碌的都市人提供了一站式的美食订购体验,让您的生活更加轻松无忧。在繁忙的都市生活中,许多人常常为了吃饭问题而烦恼。排队等待、繁......
  • 第六组
    在开发小型超市管理系统的征程中,用户所期望的“多快好省”的需求确实为我们带来了不小的挑战与限制。为了妥善应对并平衡这些多元的需求,我们采取了以下策略,力求与用户有效沟通并解释那些无法立即实现的部分:首先,我们深入剖析超市的运营方式与管理核心需求,明确了系统中最不可或缺的......
  • 估计的练习,第六组
    这个问题要求我们估计徒步遍历中国陆地边界所需的时间。首先,我们需要明确几个关键信息点:中国陆地边界的长度:中国的陆地边界总长约为2.2万多公里。这是徒步遍历的基础数据。日均徒步距离:一个健康的徒步者每天能够行走的距离是有限的,这取决于地形、天气、负重等多种因素。假设一个......