007_linux显示一个文件的某几行(中间几行)

<1>从第3000行开始,显示1000行。即显示3000~3999行

cat -n filename | tail -n +3000 | head -n 1000

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行

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行(效果同上)。

原文地址:https://www.cnblogs.com/itcomputer/p/7237414.html