"Viewing the contents of a file specified line" In a common Linux command during testing

Source: Internet
Author: User

In the process of testing, usually to contact the server, for the Linux server, summed up some common commands.


    • Preparatory work

      In order to visually display the command results, a script is used to create a file that displays the contents of the file as well as a visual display of the line number.

#!/bin/bashfilename=testfile.logtouch./$FileNamei =1while [$i-le]do echo "The line number is $i" >> $Fi Lename let "i= $i +1" done


are displayed by default
Command Options Example
head -N, display the number of rows
[[email protected]  scripts]# head -n 5 testfile.log the line number is 1the  line number is 2the line number is 3the line number is  4the line number is 5 

#显示文件的前5行

[[email protected] scripts]#  head -n -6 TestFile.log the line number is 1the line  number is 2the line number is 3the line number is 4the  line number is 5the line number is 6the line number is  7the line number is 8the line number is 9the line number  is 10the line number is 11the line number is 12the  line number is 13the line number is 14 

#截去后6行, show what's left


[[email  protected] scripts]# head testfile.log the line number is 1the  line number is 2the line number is 3the line number is  4the line number is 5the line number is 6the line  number is 7the line number is 8the line number is 9the  line number is 10 

#当没有选项参数时, the first 10 rows

-v, print file name in first line
[[email protected] scripts]# head -n 5 -v  Testfile.log ==> testfile.log <==the line number is 1the line  number is 2the line number is 3the line number is 4the  line number is 5 

#在首行打印文件名称

tail -n, display rows
[[email protected] scripts]# tail  -n 4 testfile.log the line number is 17the line number  is 18the line number is 19the line number is 20 

#查看messages文件的最后4行内容


[[email protected] scripts]# tail -n +5 testfile.log  the line number is 5the line number is 6the line  number is 7the line number is 8the line number is 9the  Line number is 10the line number is 11the line number is  12the line number is 13the line number is 14the line  Number is 15the line number is 16the line number is 17the  line number is 18the line number is 19the line number  is 20 

#截去前4行, show what's left


[[email protected] scripts]# Tail TestFile.log The line number was 11the line number is 12the line number is 13the line num  ber is 14the line number was 15the line number are 16the line number are 17the line number are 18the line number are 19the line Number is 20

#当没有选项参数时, the last 10 rows are displayed by default

-F

Tail-f/var/log/messages

#当文件内容有更新时, dynamic display of the latest content

-V, print the file name on the first line
[[email protected] scripts]# tail-v-N 3 TestFile.log ==> TestFile.log <==the line number was 18the line number is 1 9the line number is 20

#在首行打印文件名称


[Email protected] scripts]# head-n TestFile.log |tail-n 5the line number was 11the line number is 12the line number I s 13the line number was 14the line number is 15

#查看中间行第11 the contents of the ~15 line

Sed -N, used with P
[[email protected] scripts]# sed-n ' 10p ' TestFile.log the line number is 10
#查看第10行的内容

[Email protected] scripts]# sed-n ' 11,14p ' TestFile.log the line number was 11the line number is 12the line number is 13t He line number is 14

#查看第11 the contents of the ~14 line

Vi
[Email protected] scripts]# VI +12 TestFile.log

#文件打开后, the cursor is positioned directly on line 12th

More
[[email protected] scripts]# more-4 TestFile.log The line number was 1the line number is 2the line number is 3the line num ber is 4--more--(19%)

#显示前4行


[[email protected] scripts]# more +4 testfile.log the  line number is 4the line number is 5the line number is  6the line number is 7the line number is 8the line  Number is 9the line number is 10the line number is 11the  line number is 12the line number is 13the line number  Is 14the line number is 15the line number is 16the line  number is 17the line number is 18the line number is  19the line number is 20 

#截去前3行, show what's left

Less
[email protected] scripts]# less +4 Testfile.lo

#截去前3行, show what's left



This article is from the "Le Gakuen" blog, please be sure to keep this source http://2338053.blog.51cto.com/2328053/1970849

"Viewing the contents of a file specified line" In a common Linux command during testing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.