007_linux显示一个文件的某几行(中间几行)
<1>从第3000行开始,显示1000行。即显示3000~3999行
cat -n filename | tail -n +3000 | head -n 1000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | cat -n anaconda-ks.cfg 1 #version=RHEL7 2 # System authorization information 3 auth --enableshadow --passalgo=sha512 4 5 # Use CDROM installation media 6 cdrom 7 # Use graphical install 8 graphical 9 # Run the Setup Agent on first boot 10 firstboot -- enable 11 ignoredisk --only-use=vda 12 # Keyboard layouts 13 keyboard --vckeymap=us --xlayouts= 'us' 14 # System language 15 lang en_US.UTF-8 16 17 # Network information 18 network --bootproto=dhcp --device=eth0 --onboot=off --ipv6=auto 19 network -- hostname =localhost.localdomain 20 # Root password 21 rootpw --iscrypted $6$15wKoUruErTMLJxh$ow1ekpUcBsNJp6TriJ7F08RQXk.tG.K3uxBm7X7pTV93IvlxA41x8B81qu6HbZazv77SKTwXPVEiGz4Ky9r /h1 22 # System timezone 23 timezone Asia /Shanghai --isUtc --nontp 24 # System bootloader configuration 25 bootloader --append= " crashkernel=auto" --location=mbr --boot-drive=vda 26 # Partition clearing information 27 clearpart --none --initlabel 28 # Disk partitioning information 29 part /boot --fstype= "ext4" --ondisk=vda --size=1024 --label= /boot 30 part swap --fstype= "swap" --ondisk=vda --size=16383 31 part / --fstype= "ext4" --ondisk=vda --size=30720 --label=/ 32 part /data --fstype= "ext4" --ondisk=vda --size=156670 --label= /data 33 34 %packages 35 @compat-libraries 36 @core 37 @debugging 38 @development 39 kexec-tools 40 41 %end 42 43 %addon com_redhat_kdump -- enable --reserve-mb= 'auto' 44 45 %end |
<2>显示1000行到3000行
cat filename| head -n 3000 | tail -n +1000
*注意两种方法的顺序
分解:
tail -n 1000:显示最后1000行
tail -n +1000:从1000行开始显示,显示1000行以后的
head -n 1000:显示前面1000行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | cat -n anaconda-ks.cfg | head -n 41| tail -n +32 #第一种(明显这种效率高) 32 part /data --fstype= "ext4" --ondisk=vda --size=156670 --label= /data 33 34 %packages 35 @compat-libraries 36 @core 37 @debugging 38 @development 39 kexec-tools 40 41 %end cat -n anaconda-ks.cfg | tail -n +32| head -n 10 #第二种(不建议使用这种) 32 part /data --fstype= "ext4" --ondisk=vda --size=156670 --label= /data 33 34 %packages 35 @compat-libraries 36 @core 37 @debugging 38 @development 39 kexec-tools 40 41 %end |
<3>用sed命令
sed -n '32,41p' anaconda-ks.cfg 这样你就可以只查看文件的第32行到第41行(效果同上)。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步