四剑客面试真题
1、linux系统中,什么命令可以从文本文件的每一行中截取指定的内容的数据
用awk awk可以截取
awk '{print $1}' 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
[root@master ~]# awk '{print $1}' 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
2、在每一行后增加一空行?
sed 'G' test.txt
awk '1;{print ""}' test.txt
实际效果演示
[root@master ~]# sed 'G' 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
[root@master ~]# cat test
test.txt testx.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
[root@master ~]# awk '1;{print ""}' 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
3、在匹配regex的行之后插入一空行?
sed '/regex/a\\' test.txt
'/regex/': 这是一个模式匹配部分。它告诉 sed 查找所有匹配 regex 的行。这里的 regex 应该被替换为具体的正则表达式。
a\\: 在这里,a 表示“追加”,而 \\ 表示命令的结束。因为 sed 命令通常跨越多行,所以使用 \ 来表示命令的延续,而 \\ 是因为在 shell 中 \ 本身是一个转义字符,所以需要用两个反斜杠来表示一个。
实际效果演示
[root@master ~]# sed '/regex/a\\' test.txt
dsjkfh
fsdfhsd
sdfjs
sdkfhherg
dfghksdhg
sdfksdh
sfhshef
afjnshdf
fkhsekf
sefhke
efnkehuf
sfneskfhes'f
regex
sdfhj
sdfhjsd
sdkhfs
regex
sdjhfs
dshkfs
regex
sdkhfas
sdfkhas
sdkhfas
jnsd
fas
nk
sdfklhas'
regex
sdfgsd
redslfs
sds'
:`
dflsdfkhs
sefjef
sdhfrkhg
erkthwe
rkshfkser
ekrhse
flehise
ege
etc
sdfksdf
sdfsdkfs
sdfsldf
sdfjsl'
se
ekhfe
efkhesh
4、计算文件行数?
wc -l test.txt
grep -c '^' test.txt
awk 'END {print NR}' test.txt
awk 读取 test.txt 文件的每一行,并在内部维护一个变量 NR,该变量记录了当前已经读取的行数。当 awk 读取完文件的最后一行时,它会执行 END 块中的命令,即 print NR。这会打印出 NR 的值,也就是文件的总行数
实际效果演示
[root@master ~]# grep -c '^' test.txt
57
[root@master ~]# wc -l test.txt
57 test.txt
[root@master ~]# awk 'END {print NR}' test.txt
57
5、sed将文件test中第50行中的haiwao改为haiwai?
sed -i '50s/haiwao/haiwai/' test.txt
实际效果演示
[root@master ~]# cat test.txt
haiwao
dsjkfh
fsdfhsd
sdfjs
sdkfhherg
dfghksdhg
sdfksdh
sfhshef
afjnshdf
fkhsekf
sefhke
efnkehuf
sfneskfhes'f
regex
[root@master ~]# sed -i '1s/haiwao/haiwai/' test.txt
[root@master ~]# cat test.txt
haiwai
dsjkfh
fsdfhsd
sdfjs
sdkfhherg
dfghksdhg
sdfksdh
sfhshef
6 打印/etc/passwd的奇数行?
awk 'NR%2 == 1' /etc/passwd
sed -n '1~2p' /etc/passwd
awk 'NR%2 == 1' /etc/passwd
NR%2 == 1 是一个条件判断,表示当行号 NR 除以 2 的余数为 1 时执行后面的动作(即打印当前行)
sed -n '1~2p' /etc/passwd
-n 选项告诉 sed 不要自动打印模式空间的内容。
1~2p 是一个地址范围,表示从第一行开始,每隔两行打印一行(即打印奇数行)。~ 符号在 sed 中用于指定步长
实际效果演示
[root@master ~]# awk 'NR%2 == 1' /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/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
games:x:12:100:games:/usr/games:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
it1:x:1002:1002::/home/it1:/bin/bash
it3:x:1004:1004::/home/it3:/bin/bash
it5:x:1006:1006::/home/it5:/bin/bash
it7:x:1008:1008::/home/it7:/bin/bash
it9:x:1010:1010::/home/it9:/bin/bash
it11:x:1012:1012::/home/it11:/bin/bash
it13:x:1014:1014::/home/it13:/bin/bash
it15:x:1016:1016::/home/it15:/bin/bash
it17:x:1018:1018::/home/it17:/bin/bash
it19:x:1020:1020::/home/it19:/bin/bash
it21:x:1022:1022::/home/it21:/bin/bash
it23:x:1024:1024::/home/it23:/bin/bash
it25:x:1026:1026::/home/it25:/bin/bash
it27:x:1028:1028::/home/it27:/bin/bash
it29:x:1030:1030::/home/it29:/bin/bash
it31:x:1032:1032::/home/it31:/bin/bash
it33:x:1034:1034::/home/it33:/bin/bash
it35:x:1036:1036::/home/it35:/bin/bash
it37:x:1038:1038::/home/it37:/bin/bash
it39:x:1040:1040::/home/it39:/bin/bash
it41:x:1042:1042::/home/it41:/bin/bash
it43:x:1044:1044::/home/it43:/bin/bash
it45:x:1046:1046::/home/it45:/bin/bash
it47:x:1048:1048::/home/it47:/bin/bash
it49:x:1050:1050::/home/it49:/bin/bash
it51:x:1052:1052::/home/it51:/bin/bash
it53:x:1054:1054::/home/it53:/bin/bash
it55:x:1056:1056::/home/it55:/bin/bash
it57:x:1058:1058::/home/it57:/bin/bash
it59:x:1060:1060::/home/it59:/bin/bash
it61:x:1062:1062::/home/it61:/bin/bash
it63:x:1064:1064::/home/it63:/bin/bash
it65:x:1066:1066::/home/it65:/bin/bash
it67:x:1068:1068::/home/it67:/bin/bash
it69:x:1070:1070::/home/it69:/bin/bash
it71:x:1072:1072::/home/it71:/bin/bash
it73:x:1074:1074::/home/it73:/bin/bash
it75:x:1076:1076::/home/it75:/bin/bash
it77:x:1078:1078::/home/it77:/bin/bash
it79:x:1080:1080::/home/it79:/bin/bash
it81:x:1082:1082::/home/it81:/bin/bash
it83:x:1084:1084::/home/it83:/bin/bash
it85:x:1086:1086::/home/it85:/bin/bash
it87:x:1088:1088::/home/it87:/bin/bash
it89:x:1090:1090::/home/it89:/bin/bash
it91:x:1092:1092::/home/it91:/bin/bash
it93:x:1094:1094::/home/it93:/bin/bash
it95:x:1096:1096::/home/it95:/bin/bash
it97:x:1098:1098::/home/it97:/bin/bash
it99:x:1100:1100::/home/it99:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@master ~]# sed -n '1~2p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/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
games:x:12:100:games:/usr/games:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
it1:x:1002:1002::/home/it1:/bin/bash
it3:x:1004:1004::/home/it3:/bin/bash
it5:x:1006:1006::/home/it5:/bin/bash
it7:x:1008:1008::/home/it7:/bin/bash
it9:x:1010:1010::/home/it9:/bin/bash
it11:x:1012:1012::/home/it11:/bin/bash
it13:x:1014:1014::/home/it13:/bin/bash
it15:x:1016:1016::/home/it15:/bin/bash
it17:x:1018:1018::/home/it17:/bin/bash
it19:x:1020:1020::/home/it19:/bin/bash
it21:x:1022:1022::/home/it21:/bin/bash
it23:x:1024:1024::/home/it23:/bin/bash
it25:x:1026:1026::/home/it25:/bin/bash
it27:x:1028:1028::/home/it27:/bin/bash
it29:x:1030:1030::/home/it29:/bin/bash
it31:x:1032:1032::/home/it31:/bin/bash
it33:x:1034:1034::/home/it33:/bin/bash
it35:x:1036:1036::/home/it35:/bin/bash
it37:x:1038:1038::/home/it37:/bin/bash
it39:x:1040:1040::/home/it39:/bin/bash
it41:x:1042:1042::/home/it41:/bin/bash
it43:x:1044:1044::/home/it43:/bin/bash
it45:x:1046:1046::/home/it45:/bin/bash
it47:x:1048:1048::/home/it47:/bin/bash
it49:x:1050:1050::/home/it49:/bin/bash
it51:x:1052:1052::/home/it51:/bin/bash
it53:x:1054:1054::/home/it53:/bin/bash
it55:x:1056:1056::/home/it55:/bin/bash
it57:x:1058:1058::/home/it57:/bin/bash
it59:x:1060:1060::/home/it59:/bin/bash
it61:x:1062:1062::/home/it61:/bin/bash
it63:x:1064:1064::/home/it63:/bin/bash
it65:x:1066:1066::/home/it65:/bin/bash
it67:x:1068:1068::/home/it67:/bin/bash
it69:x:1070:1070::/home/it69:/bin/bash
it71:x:1072:1072::/home/it71:/bin/bash
it73:x:1074:1074::/home/it73:/bin/bash
it75:x:1076:1076::/home/it75:/bin/bash
it77:x:1078:1078::/home/it77:/bin/bash
it79:x:1080:1080::/home/it79:/bin/bash
it81:x:1082:1082::/home/it81:/bin/bash
it83:x:1084:1084::/home/it83:/bin/bash
it85:x:1086:1086::/home/it85:/bin/bash
it87:x:1088:1088::/home/it87:/bin/bash
it89:x:1090:1090::/home/it89:/bin/bash
it91:x:1092:1092::/home/it91:/bin/bash
it93:x:1094:1094::/home/it93:/bin/bash
it95:x:1096:1096::/home/it95:/bin/bash
it97:x:1098:1098::/home/it97:/bin/bash
it99:x:1100:1100::/home/it99:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
7. 取出ifconfIg ens33命令中本机的IPv4地址
ifconfig ens33 |grep -oE 'inet (addr:)?([0-9\.]+)' |grep -Eo '([0-9\.]+)'
ip addr show ens33 | grep -oP '(inet \d+(\.\d+){3})'
ifconfig ens33 |grep -oE 'inet (addr:)?([0-9\.]+)' |grep -Eo '([0-9\.]+)'
grep -oE 'inet (addr:)?([0-9\.]+)': 这个命令使用扩展的正则表达式(由-E指定)来搜索ifconfig的输出,并只输出匹配的部分。这里的正则表达式匹配以inet开始,后面可能跟着addr:,然后是IP地址(由数字和点组成)。
grep -Eo '([0-9\.]+)': 这个命令再次使用扩展的正则表达式来从上一个grep命令的输出中提取IP地址。它只匹配由数字和点组成的字符串
ip addr show ens33 | grep -oP '(inet \d+(\.\d+){3})'
grep -oP '(inet \d+(\.\d+){3})': 这个命令使用Perl兼容的正则表达式(由-P指定)来搜索ip addr show的输出,并只输出匹配的部分。这里的正则表达式匹配以inet开始,后面跟着IP地址(由数字和点组成
实际效果演示
[root@master ~]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.1.134 netmask 255.255.255.0 broadcast 10.0.1.255
inet6 fe80::20c:29ff:fe4c:2586 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:4c:25:86 txqueuelen 1000 (Ethernet)
RX packets 109465 bytes 13573922 (12.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 230284 bytes 46286278 (44.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 8 bytes 528 (528.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8 bytes 528 (528.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@master ~]# ifconfig eno^C
[root@master ~]# ifconfig eno16777736 |grep -oE 'inet (addr:)?([0-9\.]+)' |grep -Eo '([0-9\.]+)'
10.0.1.134
[root@master ~]# ip addr show eno16777736 |grep -oP '(inet \d+(\.\d+){3})'
inet 10.0.1.134
8.把/etc/httpd/conf/httpd.conf?件内的Linsten 80改为Listen 8081
sed -i 's/Listen 80/Listen 8081/g' /etc/httpd/conf/httpd.conf
实际效果演示
[root@master ~]# cat /etc/httpd/conf/
httpd.conf magic
[root@master ~]# cat /etc/httpd/conf/httpd.conf
#
# This is the main Apache HTTP server configuration file. It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# In particular, see
# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
# for a discussion of each configuration directive.
#
# Do NOT simply read the instructions in here without understanding
# what they do. They're here only as hints or reminders. If you are unsure
# consult the online docs. You have been warned.
#
# Configuration and logfile names: If the filenames you specify for many
# of the server's control files begin with "/" (or "drive:/" for Win32), the
# server will use that explicit path. If the filenames do *not* begin
# with "/", the value of ServerRoot is prepended -- so 'log/access_log'
# with ServerRoot set to '/www' will be interpreted by the
# server as '/www/log/access_log', where as '/log/access_log' will be
# interpreted as '/log/access_log'.
#
# ServerRoot: The top of the directory tree under which the server's
# configuration, error, and log files are kept.
#
# Do not add a slash at the end of the directory path. If you point
# ServerRoot at a non-local disk, be sure to specify a local disk on the
# Mutex directive, if file-based mutexes are used. If you wish to share the
# same ServerRoot for multiple httpd daemons, you will need to change at
# least PidFile.
#
ServerRoot "/etc/httpd"
#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 80
#
# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
#
Include conf.modules.d/*.conf
#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
User apache
Group apache
# 'Main' server configuration
#
# The directives in this section set up the values used by the 'main'
# server, which responds to any requests that aren't handled by a
# <VirtualHost> definition. These values also provide defaults for
# any <VirtualHost> containers you may define later in the file.
#
# All of these directives may appear inside <VirtualHost> containers,
# in which case these default settings will be overridden for the
# virtual host being defined.
#
#
# ServerAdmin: Your address, where problems with the server should be
# e-mailed. This address appears on some server-generated pages, such
# as error documents. e.g. admin@your-domain.com
#
ServerAdmin root@localhost
#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
#ServerName www.example.com:80
#
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
# <Directory> blocks below.
#
<Directory />
AllowOverride none
Require all denied
</Directory>
#
# Note that from this point forward you must specifically allow
# particular features to be enabled - so if something's not working as
# you might expect, make sure that you have specifically enabled it
# below.
#
#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html"
#
# Relax access to content within /var/www.
#
<Directory "/var/www">
AllowOverride None
# Allow open access:
Require all granted
</Directory>
# Further relax access to the default document root:
<Directory "/var/www/html">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
#
# The following lines prevent .htaccess and .htpasswd files from being
# viewed by Web clients.
#
<Files ".ht*">
Require all denied
</Files>
#
# ErrorLog: The location of the error log file.
# If you do not specify an ErrorLog directive within a <VirtualHost>
# container, error messages relating to that virtual host will be
# logged here. If you *do* define an error logfile for a <VirtualHost>
# container, that host's errors will be logged there and not here.
#
ErrorLog "logs/error_log"
#
# LogLevel: Control the number of messages logged to the error_log.
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
#
LogLevel warn
<IfModule log_config_module>
#
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
#
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
#
# The location and format of the access logfile (Common Logfile Format).
# If you do not define any access logfiles within a <VirtualHost>
# container, they will be logged here. Contrariwise, if you *do*
# define per-<VirtualHost> access logfiles, transactions will be
# logged therein and *not* in this file.
#
#CustomLog "logs/access_log" common
#
# If you prefer a logfile with access, agent, and referer information
# (Combined Logfile Format) you can use the following directive.
#
CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
#
# Redirect: Allows you to tell clients about documents that used to
# exist in your server's namespace, but do not anymore. The client
# will make a new request for the document at its new location.
# Example:
# Redirect permanent /foo http://www.example.com/bar
#
# Alias: Maps web paths into filesystem paths and is used to
# access content that does not live under the DocumentRoot.
# Example:
# Alias /webpath /full/filesystem/path
#
# If you include a trailing / on /webpath then the server will
# require it to be present in the URL. You will also likely
# need to provide a <Directory> section to allow access to
# the filesystem path.
#
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the target directory are treated as applications and
# run by the server when requested rather than as documents sent to the
# client. The same rules about trailing "/" apply to ScriptAlias
# directives as to Alias.
#
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
#
# "/var/www/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
<IfModule mime_module>
#
# TypesConfig points to the file containing the list of mappings from
# filename extension to MIME-type.
#
TypesConfig /etc/mime.types
#
# AddType allows you to add to or override the MIME configuration
# file specified in TypesConfig for specific file types.
#
#AddType application/x-gzip .tgz
#
# AddEncoding allows you to have certain browsers uncompress
# information on the fly. Note: Not all browsers support this.
#
#AddEncoding x-compress .Z
#AddEncoding x-gzip .gz .tgz
#
# If the AddEncoding directives above are commented-out, then you
# probably should define those extensions to indicate media types:
#
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
#
# AddHandler allows you to map certain file extensions to "handlers":
# actions unrelated to filetype. These can be either built into the server
# or added with the Action directive (see below)
#
# To use CGI scripts outside of ScriptAliased directories:
# (You will also need to add "ExecCGI" to the "Options" directive.)
#
#AddHandler cgi-script .cgi
# For type maps (negotiated resources):
#AddHandler type-map var
#
# Filters allow you to process content before it is sent to the client.
#
# To parse .shtml files for server-side includes (SSI):
# (You will also need to add "Includes" to the "Options" directive.)
#
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
#
# Specify a default charset for all content served; this enables
# interpretation of all content as UTF-8 by default. To use the
# default browser choice (ISO-8859-1), or to allow the META tags
# in HTML content to override this choice, comment out this
# directive:
#
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
#
# The mod_mime_magic module allows the server to use various hints from the
# contents of the file itself to determine its type. The MIMEMagicFile
# directive tells the module where the hint definitions are located.
#
MIMEMagicFile conf/magic
</IfModule>
#
# Customizable error responses come in three flavors:
# 1) plain text 2) local redirects 3) external redirects
#
# Some examples:
#ErrorDocument 500 "The server made a boo boo."
#ErrorDocument 404 /missing.html
#ErrorDocument 404 "/cgi-bin/missing_handler.pl"
#ErrorDocument 402 http://www.example.com/subscription_info.html
#
#
# EnableMMAP and EnableSendfile: On systems that support it,
# memory-mapping or the sendfile syscall may be used to deliver
# files. This usually improves server performance, but must
# be turned off when serving from networked-mounted
# filesystems or if support for these functions is otherwise
# broken on your system.
# Defaults if commented: EnableMMAP On, EnableSendfile Off
#
#EnableMMAP off
EnableSendfile on
# Supplemental configuration
#
# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf
[root@master ~]# sed -i 's/Listen 80/Listen 8081/g' /etc/httpd/conf/httpd.conf
[root@master ~]# cat /etc/httpd/conf/httpd.conf
#
# This is the main Apache HTTP server configuration file. It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# In particular, see
# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
# for a discussion of each configuration directive.
#
# Do NOT simply read the instructions in here without understanding
# what they do. They're here only as hints or reminders. If you are unsure
# consult the online docs. You have been warned.
#
# Configuration and logfile names: If the filenames you specify for many
# of the server's control files begin with "/" (or "drive:/" for Win32), the
# server will use that explicit path. If the filenames do *not* begin
# with "/", the value of ServerRoot is prepended -- so 'log/access_log'
# with ServerRoot set to '/www' will be interpreted by the
# server as '/www/log/access_log', where as '/log/access_log' will be
# interpreted as '/log/access_log'.
#
# ServerRoot: The top of the directory tree under which the server's
# configuration, error, and log files are kept.
#
# Do not add a slash at the end of the directory path. If you point
# ServerRoot at a non-local disk, be sure to specify a local disk on the
# Mutex directive, if file-based mutexes are used. If you wish to share the
# same ServerRoot for multiple httpd daemons, you will need to change at
# least PidFile.
#
ServerRoot "/etc/httpd"
#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 8081
#
# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
#
Include conf.modules.d/*.conf
#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
User apache
Group apache
# 'Main' server configuration
#
# The directives in this section set up the values used by the 'main'
# server, which responds to any requests that aren't handled by a
# <VirtualHost> definition. These values also provide defaults for
# any <VirtualHost> containers you may define later in the file.
#
# All of these directives may appear inside <VirtualHost> containers,
# in which case these default settings will be overridden for the
# virtual host being defined.
#
#
# ServerAdmin: Your address, where problems with the server should be
# e-mailed. This address appears on some server-generated pages, such
# as error documents. e.g. admin@your-domain.com
#
ServerAdmin root@localhost
#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
#ServerName www.example.com:80
#
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
# <Directory> blocks below.
#
<Directory />
AllowOverride none
Require all denied
</Directory>
#
# Note that from this point forward you must specifically allow
# particular features to be enabled - so if something's not working as
# you might expect, make sure that you have specifically enabled it
# below.
#
#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html"
#
# Relax access to content within /var/www.
#
<Directory "/var/www">
AllowOverride None
# Allow open access:
Require all granted
</Directory>
# Further relax access to the default document root:
<Directory "/var/www/html">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
#
# The following lines prevent .htaccess and .htpasswd files from being
# viewed by Web clients.
#
<Files ".ht*">
Require all denied
</Files>
#
# ErrorLog: The location of the error log file.
# If you do not specify an ErrorLog directive within a <VirtualHost>
# container, error messages relating to that virtual host will be
# logged here. If you *do* define an error logfile for a <VirtualHost>
# container, that host's errors will be logged there and not here.
#
ErrorLog "logs/error_log"
#
# LogLevel: Control the number of messages logged to the error_log.
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
#
LogLevel warn
<IfModule log_config_module>
#
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
#
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
#
# The location and format of the access logfile (Common Logfile Format).
# If you do not define any access logfiles within a <VirtualHost>
# container, they will be logged here. Contrariwise, if you *do*
# define per-<VirtualHost> access logfiles, transactions will be
# logged therein and *not* in this file.
#
#CustomLog "logs/access_log" common
#
# If you prefer a logfile with access, agent, and referer information
# (Combined Logfile Format) you can use the following directive.
#
CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
#
# Redirect: Allows you to tell clients about documents that used to
# exist in your server's namespace, but do not anymore. The client
# will make a new request for the document at its new location.
# Example:
# Redirect permanent /foo http://www.example.com/bar
#
# Alias: Maps web paths into filesystem paths and is used to
# access content that does not live under the DocumentRoot.
# Example:
# Alias /webpath /full/filesystem/path
#
# If you include a trailing / on /webpath then the server will
# require it to be present in the URL. You will also likely
# need to provide a <Directory> section to allow access to
# the filesystem path.
#
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the target directory are treated as applications and
# run by the server when requested rather than as documents sent to the
# client. The same rules about trailing "/" apply to ScriptAlias
# directives as to Alias.
#
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
#
# "/var/www/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
<IfModule mime_module>
#
# TypesConfig points to the file containing the list of mappings from
# filename extension to MIME-type.
#
TypesConfig /etc/mime.types
#
# AddType allows you to add to or override the MIME configuration
# file specified in TypesConfig for specific file types.
#
#AddType application/x-gzip .tgz
#
# AddEncoding allows you to have certain browsers uncompress
# information on the fly. Note: Not all browsers support this.
#
#AddEncoding x-compress .Z
#AddEncoding x-gzip .gz .tgz
#
# If the AddEncoding directives above are commented-out, then you
# probably should define those extensions to indicate media types:
#
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
#
# AddHandler allows you to map certain file extensions to "handlers":
# actions unrelated to filetype. These can be either built into the server
# or added with the Action directive (see below)
#
# To use CGI scripts outside of ScriptAliased directories:
# (You will also need to add "ExecCGI" to the "Options" directive.)
#
#AddHandler cgi-script .cgi
# For type maps (negotiated resources):
#AddHandler type-map var
#
# Filters allow you to process content before it is sent to the client.
#
# To parse .shtml files for server-side includes (SSI):
# (You will also need to add "Includes" to the "Options" directive.)
#
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
#
# Specify a default charset for all content served; this enables
# interpretation of all content as UTF-8 by default. To use the
# default browser choice (ISO-8859-1), or to allow the META tags
# in HTML content to override this choice, comment out this
# directive:
#
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
#
# The mod_mime_magic module allows the server to use various hints from the
# contents of the file itself to determine its type. The MIMEMagicFile
# directive tells the module where the hint definitions are located.
#
MIMEMagicFile conf/magic
</IfModule>
#
# Customizable error responses come in three flavors:
# 1) plain text 2) local redirects 3) external redirects
#
# Some examples:
#ErrorDocument 500 "The server made a boo boo."
#ErrorDocument 404 /missing.html
#ErrorDocument 404 "/cgi-bin/missing_handler.pl"
#ErrorDocument 402 http://www.example.com/subscription_info.html
#
#
# EnableMMAP and EnableSendfile: On systems that support it,
# memory-mapping or the sendfile syscall may be used to deliver
# files. This usually improves server performance, but must
# be turned off when serving from networked-mounted
# filesystems or if support for these functions is otherwise
# broken on your system.
# Defaults if commented: EnableMMAP On, EnableSendfile Off
#
#EnableMMAP off
EnableSendfile on
# Supplemental configuration
#
# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf
9.passwd文件每行内容前都编序号显示
seq 1 $(wc -l < /etc/passwd) | paste - /etc/passwd
awk 'BEGIN{cnt=1}{print cnt++"\t"$0}' /etc/passwd
seq 1 $(wc -l < /etc/passwd) | paste - /etc/passwd
wc -l < /etc/passwd:wc 命令用于统计文件中的行数、字数和字节数。-l 选项告诉 wc 只统计行数。< /etc/passwd 是一个输入重定向,将 /etc/passwd 文件的内容作为 wc 命令的输入。所以,这个子命令会输出 /etc/passwd 文件的总行数。
seq 1 $(wc -l < /etc/passwd):seq 命令用于生成一个数字序列。这里,它从1开始,到 /etc/passwd 文件的总行数结束。例如,如果 /etc/passwd 有10行,那么 seq 会输出 1 2 3 ... 10。
paste - /etc/passwd:paste 命令用于将多个文件的内容并列输出。这里,- 表示从标准输入(stdin)读取内容。所以,paste 会将 seq 命令生成的数字序列和 /etc/passwd 文件的内容并列输出
awk 'BEGIN{cnt=1}{print cnt++"\t"$0}' /etc/passwd
BEGIN{cnt=1}:在 awk 开始处理文件之前,这个 BEGIN 块会被执行。这里,它初始化一个变量 cnt 为1,这个变量用来计数行号。
{print cnt++"\t"$0}:对于 /etc/passwd 文件中的每一行,这个块都会被执行。$0 表示当前行的内容。cnt++ 会先返回 cnt 的当前值(并打印它),然后将 cnt 的值增加1。\t 是一个制表符,用来在行号和行内容之间添加一些间隔。
实际效果演示
[root@master ~]# seq 1 $(wc -l < /etc/passwd) |paste - /etc/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
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 avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
15 dbus:x:81:81:System message bus:/:/sbin/nologin
16 polkitd:x:999:998:User for polkitd:/:/sbin/nologin
17 tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
18 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
19 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
20 wang:x:1000:1000:wang:/home/wang:/bin/bash
21 systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
22 it101:x:1001:1001::/home/it101:/bin/bash
23 it1:x:1002:1002::/home/it1:/bin/bash
24 it2:x:1003:1003::/home/it2:/bin/bash
25 it3:x:1004:1004::/home/it3:/bin/bash
26 it4:x:1005:1005::/home/it4:/bin/bash
27 it5:x:1006:1006::/home/it5:/bin/bash
28 it6:x:1007:1007::/home/it6:/bin/bash
29 it7:x:1008:1008::/home/it7:/bin/bash
30 it8:x:1009:1009::/home/it8:/bin/bash
31 it9:x:1010:1010::/home/it9:/bin/bash
32 it10:x:1011:1011::/home/it10:/bin/bash
33 it11:x:1012:1012::/home/it11:/bin/bash
34 it12:x:1013:1013::/home/it12:/bin/bash
35 it13:x:1014:1014::/home/it13:/bin/bash
36 it14:x:1015:1015::/home/it14:/bin/bash
37 it15:x:1016:1016::/home/it15:/bin/bash
38 it16:x:1017:1017::/home/it16:/bin/bash
39 it17:x:1018:1018::/home/it17:/bin/bash
40 it18:x:1019:1019::/home/it18:/bin/bash
41 it19:x:1020:1020::/home/it19:/bin/bash
42 it20:x:1021:1021::/home/it20:/bin/bash
43 it21:x:1022:1022::/home/it21:/bin/bash
44 it22:x:1023:1023::/home/it22:/bin/bash
45 it23:x:1024:1024::/home/it23:/bin/bash
46 it24:x:1025:1025::/home/it24:/bin/bash
47 it25:x:1026:1026::/home/it25:/bin/bash
48 it26:x:1027:1027::/home/it26:/bin/bash
49 it27:x:1028:1028::/home/it27:/bin/bash
50 it28:x:1029:1029::/home/it28:/bin/bash
51 it29:x:1030:1030::/home/it29:/bin/bash
52 it30:x:1031:1031::/home/it30:/bin/bash
53 it31:x:1032:1032::/home/it31:/bin/bash
54 it32:x:1033:1033::/home/it32:/bin/bash
55 it33:x:1034:1034::/home/it33:/bin/bash
56 it34:x:1035:1035::/home/it34:/bin/bash
57 it35:x:1036:1036::/home/it35:/bin/bash
58 it36:x:1037:1037::/home/it36:/bin/bash
59 it37:x:1038:1038::/home/it37:/bin/bash
60 it38:x:1039:1039::/home/it38:/bin/bash
61 it39:x:1040:1040::/home/it39:/bin/bash
62 it40:x:1041:1041::/home/it40:/bin/bash
63 it41:x:1042:1042::/home/it41:/bin/bash
64 it42:x:1043:1043::/home/it42:/bin/bash
65 it43:x:1044:1044::/home/it43:/bin/bash
66 it44:x:1045:1045::/home/it44:/bin/bash
67 it45:x:1046:1046::/home/it45:/bin/bash
68 it46:x:1047:1047::/home/it46:/bin/bash
69 it47:x:1048:1048::/home/it47:/bin/bash
70 it48:x:1049:1049::/home/it48:/bin/bash
71 it49:x:1050:1050::/home/it49:/bin/bash
72 it50:x:1051:1051::/home/it50:/bin/bash
73 it51:x:1052:1052::/home/it51:/bin/bash
74 it52:x:1053:1053::/home/it52:/bin/bash
75 it53:x:1054:1054::/home/it53:/bin/bash
76 it54:x:1055:1055::/home/it54:/bin/bash
77 it55:x:1056:1056::/home/it55:/bin/bash
78 it56:x:1057:1057::/home/it56:/bin/bash
79 it57:x:1058:1058::/home/it57:/bin/bash
80 it58:x:1059:1059::/home/it58:/bin/bash
81 it59:x:1060:1060::/home/it59:/bin/bash
82 it60:x:1061:1061::/home/it60:/bin/bash
83 it61:x:1062:1062::/home/it61:/bin/bash
84 it62:x:1063:1063::/home/it62:/bin/bash
85 it63:x:1064:1064::/home/it63:/bin/bash
86 it64:x:1065:1065::/home/it64:/bin/bash
87 it65:x:1066:1066::/home/it65:/bin/bash
88 it66:x:1067:1067::/home/it66:/bin/bash
89 it67:x:1068:1068::/home/it67:/bin/bash
90 it68:x:1069:1069::/home/it68:/bin/bash
91 it69:x:1070:1070::/home/it69:/bin/bash
92 it70:x:1071:1071::/home/it70:/bin/bash
93 it71:x:1072:1072::/home/it71:/bin/bash
94 it72:x:1073:1073::/home/it72:/bin/bash
95 it73:x:1074:1074::/home/it73:/bin/bash
96 it74:x:1075:1075::/home/it74:/bin/bash
97 it75:x:1076:1076::/home/it75:/bin/bash
98 it76:x:1077:1077::/home/it76:/bin/bash
99 it77:x:1078:1078::/home/it77:/bin/bash
100 it78:x:1079:1079::/home/it78:/bin/bash
101 it79:x:1080:1080::/home/it79:/bin/bash
102 it80:x:1081:1081::/home/it80:/bin/bash
103 it81:x:1082:1082::/home/it81:/bin/bash
104 it82:x:1083:1083::/home/it82:/bin/bash
105 it83:x:1084:1084::/home/it83:/bin/bash
106 it84:x:1085:1085::/home/it84:/bin/bash
107 it85:x:1086:1086::/home/it85:/bin/bash
108 it86:x:1087:1087::/home/it86:/bin/bash
109 it87:x:1088:1088::/home/it87:/bin/bash
110 it88:x:1089:1089::/home/it88:/bin/bash
111 it89:x:1090:1090::/home/it89:/bin/bash
112 it90:x:1091:1091::/home/it90:/bin/bash
113 it91:x:1092:1092::/home/it91:/bin/bash
114 it92:x:1093:1093::/home/it92:/bin/bash
115 it93:x:1094:1094::/home/it93:/bin/bash
116 it94:x:1095:1095::/home/it94:/bin/bash
117 it95:x:1096:1096::/home/it95:/bin/bash
118 it96:x:1097:1097::/home/it96:/bin/bash
119 it97:x:1098:1098::/home/it97:/bin/bash
120 it98:x:1099:1099::/home/it98:/bin/bash
121 it99:x:1100:1100::/home/it99:/bin/bash
122 it100:x:1101:1101::/home/it100:/bin/bash
123 apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@master ~]# awk 'BEGIN{cnt=1}{print cnt++"\t"$0}' /etc/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
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 avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
15 dbus:x:81:81:System message bus:/:/sbin/nologin
16 polkitd:x:999:998:User for polkitd:/:/sbin/nologin
17 tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
18 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
19 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
20 wang:x:1000:1000:wang:/home/wang:/bin/bash
21 systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
22 it101:x:1001:1001::/home/it101:/bin/bash
23 it1:x:1002:1002::/home/it1:/bin/bash
24 it2:x:1003:1003::/home/it2:/bin/bash
25 it3:x:1004:1004::/home/it3:/bin/bash
26 it4:x:1005:1005::/home/it4:/bin/bash
27 it5:x:1006:1006::/home/it5:/bin/bash
28 it6:x:1007:1007::/home/it6:/bin/bash
29 it7:x:1008:1008::/home/it7:/bin/bash
30 it8:x:1009:1009::/home/it8:/bin/bash
31 it9:x:1010:1010::/home/it9:/bin/bash
32 it10:x:1011:1011::/home/it10:/bin/bash
33 it11:x:1012:1012::/home/it11:/bin/bash
34 it12:x:1013:1013::/home/it12:/bin/bash
35 it13:x:1014:1014::/home/it13:/bin/bash
36 it14:x:1015:1015::/home/it14:/bin/bash
37 it15:x:1016:1016::/home/it15:/bin/bash
38 it16:x:1017:1017::/home/it16:/bin/bash
39 it17:x:1018:1018::/home/it17:/bin/bash
40 it18:x:1019:1019::/home/it18:/bin/bash
41 it19:x:1020:1020::/home/it19:/bin/bash
42 it20:x:1021:1021::/home/it20:/bin/bash
43 it21:x:1022:1022::/home/it21:/bin/bash
44 it22:x:1023:1023::/home/it22:/bin/bash
45 it23:x:1024:1024::/home/it23:/bin/bash
46 it24:x:1025:1025::/home/it24:/bin/bash
47 it25:x:1026:1026::/home/it25:/bin/bash
48 it26:x:1027:1027::/home/it26:/bin/bash
49 it27:x:1028:1028::/home/it27:/bin/bash
50 it28:x:1029:1029::/home/it28:/bin/bash
51 it29:x:1030:1030::/home/it29:/bin/bash
52 it30:x:1031:1031::/home/it30:/bin/bash
53 it31:x:1032:1032::/home/it31:/bin/bash
54 it32:x:1033:1033::/home/it32:/bin/bash
55 it33:x:1034:1034::/home/it33:/bin/bash
56 it34:x:1035:1035::/home/it34:/bin/bash
57 it35:x:1036:1036::/home/it35:/bin/bash
58 it36:x:1037:1037::/home/it36:/bin/bash
59 it37:x:1038:1038::/home/it37:/bin/bash
60 it38:x:1039:1039::/home/it38:/bin/bash
61 it39:x:1040:1040::/home/it39:/bin/bash
62 it40:x:1041:1041::/home/it40:/bin/bash
63 it41:x:1042:1042::/home/it41:/bin/bash
64 it42:x:1043:1043::/home/it42:/bin/bash
65 it43:x:1044:1044::/home/it43:/bin/bash
66 it44:x:1045:1045::/home/it44:/bin/bash
67 it45:x:1046:1046::/home/it45:/bin/bash
68 it46:x:1047:1047::/home/it46:/bin/bash
69 it47:x:1048:1048::/home/it47:/bin/bash
70 it48:x:1049:1049::/home/it48:/bin/bash
71 it49:x:1050:1050::/home/it49:/bin/bash
72 it50:x:1051:1051::/home/it50:/bin/bash
73 it51:x:1052:1052::/home/it51:/bin/bash
74 it52:x:1053:1053::/home/it52:/bin/bash
75 it53:x:1054:1054::/home/it53:/bin/bash
76 it54:x:1055:1055::/home/it54:/bin/bash
77 it55:x:1056:1056::/home/it55:/bin/bash
78 it56:x:1057:1057::/home/it56:/bin/bash
79 it57:x:1058:1058::/home/it57:/bin/bash
80 it58:x:1059:1059::/home/it58:/bin/bash
81 it59:x:1060:1060::/home/it59:/bin/bash
82 it60:x:1061:1061::/home/it60:/bin/bash
83 it61:x:1062:1062::/home/it61:/bin/bash
84 it62:x:1063:1063::/home/it62:/bin/bash
85 it63:x:1064:1064::/home/it63:/bin/bash
86 it64:x:1065:1065::/home/it64:/bin/bash
87 it65:x:1066:1066::/home/it65:/bin/bash
88 it66:x:1067:1067::/home/it66:/bin/bash
89 it67:x:1068:1068::/home/it67:/bin/bash
90 it68:x:1069:1069::/home/it68:/bin/bash
91 it69:x:1070:1070::/home/it69:/bin/bash
92 it70:x:1071:1071::/home/it70:/bin/bash
93 it71:x:1072:1072::/home/it71:/bin/bash
94 it72:x:1073:1073::/home/it72:/bin/bash
95 it73:x:1074:1074::/home/it73:/bin/bash
96 it74:x:1075:1075::/home/it74:/bin/bash
97 it75:x:1076:1076::/home/it75:/bin/bash
98 it76:x:1077:1077::/home/it76:/bin/bash
99 it77:x:1078:1078::/home/it77:/bin/bash
100 it78:x:1079:1079::/home/it78:/bin/bash
101 it79:x:1080:1080::/home/it79:/bin/bash
102 it80:x:1081:1081::/home/it80:/bin/bash
103 it81:x:1082:1082::/home/it81:/bin/bash
104 it82:x:1083:1083::/home/it82:/bin/bash
105 it83:x:1084:1084::/home/it83:/bin/bash
106 it84:x:1085:1085::/home/it84:/bin/bash
107 it85:x:1086:1086::/home/it85:/bin/bash
108 it86:x:1087:1087::/home/it86:/bin/bash
109 it87:x:1088:1088::/home/it87:/bin/bash
110 it88:x:1089:1089::/home/it88:/bin/bash
111 it89:x:1090:1090::/home/it89:/bin/bash
112 it90:x:1091:1091::/home/it90:/bin/bash
113 it91:x:1092:1092::/home/it91:/bin/bash
114 it92:x:1093:1093::/home/it92:/bin/bash
115 it93:x:1094:1094::/home/it93:/bin/bash
116 it94:x:1095:1095::/home/it94:/bin/bash
117 it95:x:1096:1096::/home/it95:/bin/bash
118 it96:x:1097:1097::/home/it96:/bin/bash
119 it97:x:1098:1098::/home/it97:/bin/bash
120 it98:x:1099:1099::/home/it98:/bin/bash
121 it99:x:1100:1100::/home/it99:/bin/bash
122 it100:x:1101:1101::/home/it100:/bin/bash
123 apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
10.passwd文件不显示第2行
sed '2d' /etc/passwd
awk 'NR!=2' /etc/passwd
实际效果演示
[root@master ~]# cat /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
[root@master ~]# sed '2d' /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
11.把passwd件的第1-3内容,另存为test.txt
head -n 3 /etc/passwd >test.txt
sed -n '1,3p' /etc/passwd > test.txt
awk 'NR<=3' /etc/passwd > test.txt
awk 'NR<=3' /etc/passwd > test.txt
'NR<=3': 这是一个 awk 的模式动作表达式。
NR 是 awk 的内置变量,代表当前处理的记录号(对于文件来说,通常就是行号)。
NR<=3 这个条件检查当前处理的行号是否小于或等于3。
如果这个条件为真(即当前行是文件的前三行之一),则默认动作(即打印整行)会被执行
实际效果演示
[root@master ~]# head -n 3 /etc/passwd >testn.txt
[root@master ~]# cat testn.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
[root@master ~]# sed -n '1,2p' /etc/passwd >testw.txt
[root@master ~]# cat testw.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@master ~]# awk 'NR<=3' /etc/passwd >testq.txt
[root@master ~]# cat testq.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
12.显示passwd 文件奇数行显示
awk 'NR%2 == 1' /etc/passwd
sed -n '1~2p' /etc/passwd
sed -n 'n;n;p' /etc/passwd
awk 'NR%2 == 1' /etc/passwd
NR%2 == 1 是一个条件判断,表示当行号 NR 除以 2 的余数为 1 时执行后面的动作(即打印当前行)
sed -n '1~2p' /etc/passwd
-n 选项告诉 sed 不要自动打印模式空间的内容。
1~2p 是一个地址范围,表示从第一行开始,每隔两行打印一行(即打印奇数行)。~ 符号在 sed 中用于指定步长
sed -n 'n;n;p' /etc/passwd
n:读取下一行到模式空间,但不打印。
n:再次读取下一行到模式空间,还是不打印。
p:打印模式空间的内容。
实际效果演示
[root@master ~]# awk 'NR%2 == 1' /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/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
games:x:12:100:games:/usr/games:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
it1:x:1002:1002::/home/it1:/bin/bash
it3:x:1004:1004::/home/it3:/bin/bash
it5:x:1006:1006::/home/it5:/bin/bash
it7:x:1008:1008::/home/it7:/bin/bash
it9:x:1010:1010::/home/it9:/bin/bash
it11:x:1012:1012::/home/it11:/bin/bash
it13:x:1014:1014::/home/it13:/bin/bash
it15:x:1016:1016::/home/it15:/bin/bash
it17:x:1018:1018::/home/it17:/bin/bash
it19:x:1020:1020::/home/it19:/bin/bash
it21:x:1022:1022::/home/it21:/bin/bash
it23:x:1024:1024::/home/it23:/bin/bash
it25:x:1026:1026::/home/it25:/bin/bash
it27:x:1028:1028::/home/it27:/bin/bash
it29:x:1030:1030::/home/it29:/bin/bash
it31:x:1032:1032::/home/it31:/bin/bash
it33:x:1034:1034::/home/it33:/bin/bash
it35:x:1036:1036::/home/it35:/bin/bash
it37:x:1038:1038::/home/it37:/bin/bash
it39:x:1040:1040::/home/it39:/bin/bash
it41:x:1042:1042::/home/it41:/bin/bash
it43:x:1044:1044::/home/it43:/bin/bash
it45:x:1046:1046::/home/it45:/bin/bash
it47:x:1048:1048::/home/it47:/bin/bash
it49:x:1050:1050::/home/it49:/bin/bash
it51:x:1052:1052::/home/it51:/bin/bash
it53:x:1054:1054::/home/it53:/bin/bash
it55:x:1056:1056::/home/it55:/bin/bash
it57:x:1058:1058::/home/it57:/bin/bash
it59:x:1060:1060::/home/it59:/bin/bash
it61:x:1062:1062::/home/it61:/bin/bash
it63:x:1064:1064::/home/it63:/bin/bash
it65:x:1066:1066::/home/it65:/bin/bash
it67:x:1068:1068::/home/it67:/bin/bash
it69:x:1070:1070::/home/it69:/bin/bash
it71:x:1072:1072::/home/it71:/bin/bash
it73:x:1074:1074::/home/it73:/bin/bash
it75:x:1076:1076::/home/it75:/bin/bash
it77:x:1078:1078::/home/it77:/bin/bash
it79:x:1080:1080::/home/it79:/bin/bash
it81:x:1082:1082::/home/it81:/bin/bash
it83:x:1084:1084::/home/it83:/bin/bash
it85:x:1086:1086::/home/it85:/bin/bash
it87:x:1088:1088::/home/it87:/bin/bash
it89:x:1090:1090::/home/it89:/bin/bash
it91:x:1092:1092::/home/it91:/bin/bash
it93:x:1094:1094::/home/it93:/bin/bash
it95:x:1096:1096::/home/it95:/bin/bash
it97:x:1098:1098::/home/it97:/bin/bash
it99:x:1100:1100::/home/it99:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@master ~]# sed -n '1~2p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/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
games:x:12:100:games:/usr/games:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
it1:x:1002:1002::/home/it1:/bin/bash
it3:x:1004:1004::/home/it3:/bin/bash
it5:x:1006:1006::/home/it5:/bin/bash
it7:x:1008:1008::/home/it7:/bin/bash
it9:x:1010:1010::/home/it9:/bin/bash
it11:x:1012:1012::/home/it11:/bin/bash
it13:x:1014:1014::/home/it13:/bin/bash
it15:x:1016:1016::/home/it15:/bin/bash
it17:x:1018:1018::/home/it17:/bin/bash
it19:x:1020:1020::/home/it19:/bin/bash
it21:x:1022:1022::/home/it21:/bin/bash
it23:x:1024:1024::/home/it23:/bin/bash
it25:x:1026:1026::/home/it25:/bin/bash
it27:x:1028:1028::/home/it27:/bin/bash
it29:x:1030:1030::/home/it29:/bin/bash
it31:x:1032:1032::/home/it31:/bin/bash
it33:x:1034:1034::/home/it33:/bin/bash
it35:x:1036:1036::/home/it35:/bin/bash
it37:x:1038:1038::/home/it37:/bin/bash
it39:x:1040:1040::/home/it39:/bin/bash
it41:x:1042:1042::/home/it41:/bin/bash
it43:x:1044:1044::/home/it43:/bin/bash
it45:x:1046:1046::/home/it45:/bin/bash
it47:x:1048:1048::/home/it47:/bin/bash
it49:x:1050:1050::/home/it49:/bin/bash
it51:x:1052:1052::/home/it51:/bin/bash
it53:x:1054:1054::/home/it53:/bin/bash
it55:x:1056:1056::/home/it55:/bin/bash
it57:x:1058:1058::/home/it57:/bin/bash
it59:x:1060:1060::/home/it59:/bin/bash
it61:x:1062:1062::/home/it61:/bin/bash
it63:x:1064:1064::/home/it63:/bin/bash
it65:x:1066:1066::/home/it65:/bin/bash
it67:x:1068:1068::/home/it67:/bin/bash
it69:x:1070:1070::/home/it69:/bin/bash
it71:x:1072:1072::/home/it71:/bin/bash
it73:x:1074:1074::/home/it73:/bin/bash
it75:x:1076:1076::/home/it75:/bin/bash
it77:x:1078:1078::/home/it77:/bin/bash
it79:x:1080:1080::/home/it79:/bin/bash
it81:x:1082:1082::/home/it81:/bin/bash
it83:x:1084:1084::/home/it83:/bin/bash
it85:x:1086:1086::/home/it85:/bin/bash
it87:x:1088:1088::/home/it87:/bin/bash
it89:x:1090:1090::/home/it89:/bin/bash
it91:x:1092:1092::/home/it91:/bin/bash
it93:x:1094:1094::/home/it93:/bin/bash
it95:x:1096:1096::/home/it95:/bin/bash
it97:x:1098:1098::/home/it97:/bin/bash
it99:x:1100:1100::/home/it99:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
13.passwd文件偶数行显示
awk 'NR%2==0' /etc/passwd
sed -n 'n;p' /etc/passwd
awk 'NR%2==0' /etc/passwd
NR 是行号
NR%2==0 这个条件检查当前行号 NR 是否是偶数。如果是偶数,则默认动作(即打印整行)会被执行
sed -n 'n;p' /etc/passwd
-n 选项告诉 sed 不要自动打印每一行。
n;p 是一个 sed 脚本,它包含两个命令。
n:读取下一行到模式空间,但不打印。
p:打印当前模式空间的内容(即刚才读取的下一行)。
这个 sed 命令的逻辑是:对于文件的每一行,它都会跳过当前行并打印下一行。因此,它会打印出所有的偶数行(第二行、第四行、第六行等)
实际效果演示
[root@master ~]# sed -n 'n;p' /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
halt:x:7:0:halt:/sbin:/sbin/halt
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
wang:x:1000:1000:wang:/home/wang:/bin/bash
it101:x:1001:1001::/home/it101:/bin/bash
it2:x:1003:1003::/home/it2:/bin/bash
it4:x:1005:1005::/home/it4:/bin/bash
it6:x:1007:1007::/home/it6:/bin/bash
it8:x:1009:1009::/home/it8:/bin/bash
it10:x:1011:1011::/home/it10:/bin/bash
it12:x:1013:1013::/home/it12:/bin/bash
it14:x:1015:1015::/home/it14:/bin/bash
it16:x:1017:1017::/home/it16:/bin/bash
it18:x:1019:1019::/home/it18:/bin/bash
it20:x:1021:1021::/home/it20:/bin/bash
it22:x:1023:1023::/home/it22:/bin/bash
it24:x:1025:1025::/home/it24:/bin/bash
it26:x:1027:1027::/home/it26:/bin/bash
it28:x:1029:1029::/home/it28:/bin/bash
it30:x:1031:1031::/home/it30:/bin/bash
it32:x:1033:1033::/home/it32:/bin/bash
it34:x:1035:1035::/home/it34:/bin/bash
it36:x:1037:1037::/home/it36:/bin/bash
it38:x:1039:1039::/home/it38:/bin/bash
it40:x:1041:1041::/home/it40:/bin/bash
it42:x:1043:1043::/home/it42:/bin/bash
it44:x:1045:1045::/home/it44:/bin/bash
it46:x:1047:1047::/home/it46:/bin/bash
it48:x:1049:1049::/home/it48:/bin/bash
it50:x:1051:1051::/home/it50:/bin/bash
it52:x:1053:1053::/home/it52:/bin/bash
it54:x:1055:1055::/home/it54:/bin/bash
it56:x:1057:1057::/home/it56:/bin/bash
it58:x:1059:1059::/home/it58:/bin/bash
it60:x:1061:1061::/home/it60:/bin/bash
it62:x:1063:1063::/home/it62:/bin/bash
it64:x:1065:1065::/home/it64:/bin/bash
it66:x:1067:1067::/home/it66:/bin/bash
it68:x:1069:1069::/home/it68:/bin/bash
it70:x:1071:1071::/home/it70:/bin/bash
it72:x:1073:1073::/home/it72:/bin/bash
it74:x:1075:1075::/home/it74:/bin/bash
it76:x:1077:1077::/home/it76:/bin/bash
it78:x:1079:1079::/home/it78:/bin/bash
it80:x:1081:1081::/home/it80:/bin/bash
it82:x:1083:1083::/home/it82:/bin/bash
it84:x:1085:1085::/home/it84:/bin/bash
it86:x:1087:1087::/home/it86:/bin/bash
it88:x:1089:1089::/home/it88:/bin/bash
it90:x:1091:1091::/home/it90:/bin/bash
it92:x:1093:1093::/home/it92:/bin/bash
it94:x:1095:1095::/home/it94:/bin/bash
it96:x:1097:1097::/home/it96:/bin/bash
it98:x:1099:1099::/home/it98:/bin/bash
it100:x:1101:1101::/home/it100:/bin/bash
[root@master ~]# awk 'NR%2==0' /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
halt:x:7:0:halt:/sbin:/sbin/halt
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
wang:x:1000:1000:wang:/home/wang:/bin/bash
it101:x:1001:1001::/home/it101:/bin/bash
it2:x:1003:1003::/home/it2:/bin/bash
it4:x:1005:1005::/home/it4:/bin/bash
it6:x:1007:1007::/home/it6:/bin/bash
it8:x:1009:1009::/home/it8:/bin/bash
it10:x:1011:1011::/home/it10:/bin/bash
it12:x:1013:1013::/home/it12:/bin/bash
it14:x:1015:1015::/home/it14:/bin/bash
it16:x:1017:1017::/home/it16:/bin/bash
it18:x:1019:1019::/home/it18:/bin/bash
it20:x:1021:1021::/home/it20:/bin/bash
it22:x:1023:1023::/home/it22:/bin/bash
it24:x:1025:1025::/home/it24:/bin/bash
it26:x:1027:1027::/home/it26:/bin/bash
it28:x:1029:1029::/home/it28:/bin/bash
it30:x:1031:1031::/home/it30:/bin/bash
it32:x:1033:1033::/home/it32:/bin/bash
it34:x:1035:1035::/home/it34:/bin/bash
it36:x:1037:1037::/home/it36:/bin/bash
it38:x:1039:1039::/home/it38:/bin/bash
it40:x:1041:1041::/home/it40:/bin/bash
it42:x:1043:1043::/home/it42:/bin/bash
it44:x:1045:1045::/home/it44:/bin/bash
it46:x:1047:1047::/home/it46:/bin/bash
it48:x:1049:1049::/home/it48:/bin/bash
it50:x:1051:1051::/home/it50:/bin/bash
it52:x:1053:1053::/home/it52:/bin/bash
it54:x:1055:1055::/home/it54:/bin/bash
it56:x:1057:1057::/home/it56:/bin/bash
it58:x:1059:1059::/home/it58:/bin/bash
it60:x:1061:1061::/home/it60:/bin/bash
it62:x:1063:1063::/home/it62:/bin/bash
it64:x:1065:1065::/home/it64:/bin/bash
it66:x:1067:1067::/home/it66:/bin/bash
it68:x:1069:1069::/home/it68:/bin/bash
it70:x:1071:1071::/home/it70:/bin/bash
it72:x:1073:1073::/home/it72:/bin/bash
it74:x:1075:1075::/home/it74:/bin/bash
it76:x:1077:1077::/home/it76:/bin/bash
it78:x:1079:1079::/home/it78:/bin/bash
it80:x:1081:1081::/home/it80:/bin/bash
it82:x:1083:1083::/home/it82:/bin/bash
it84:x:1085:1085::/home/it84:/bin/bash
it86:x:1087:1087::/home/it86:/bin/bash
it88:x:1089:1089::/home/it88:/bin/bash
it90:x:1091:1091::/home/it90:/bin/bash
it92:x:1093:1093::/home/it92:/bin/bash
it94:x:1095:1095::/home/it94:/bin/bash
it96:x:1097:1097::/home/it96:/bin/bash
it98:x:1099:1099::/home/it98:/bin/bash
it100:x:1101:1101::/home/it100:/bin/bash