Seven ways to read Linux text files

Keywords: Linux Operation & Maintenance CentOS

Linux text file reading

describe

Text viewing and reading of any operating system is the most basic function;
There are mainly seven commands (cat/tac/nl/more/less/head/tail) involved in text reading in linux system

cat command

  • Command purpose

    • Read all information from the file
    • Link and print the contents of multiple files to standard output
  • Command syntax
    cat [OPTION]... [FILE]...

  • Common options

    optiondescribe
    -ADisplays invisible characters (control characters, line breaks, tabs) in the contents of the file
    -nAll line numbers are displayed while displaying the content, starting from 1
    -bDisplay the non blank line number while displaying the content, starting from 1
  • Example

    [root@centos-36_2 data]# cat myfile
    first line
            second line 
    
     have space
    last line
    
    [root@centos-36_2 data]# cat -A myfile
    first line$
    ^Isecond line $
    $
     have space$
    last line$
    $
    [root@centos-36_2 data]# cat -n myfile
         1  first line
         2          second line 
         3
         4   have space
         5  last line
         6
    [root@centos-36_2 data]# cat -b myfile
         1  first line
         2          second line 
    
         3   have space
         4  last line
    
    [root@centos-36_2 data]# 
    

tac command

  • Command purpose

    • When reading a file, all information is displayed in reverse lines
  • Command syntax
    tac [OPTION]... [FILE]...

  • Example

    [root@centos-36_2 data]# cat myfile
    first line
            second line 
    
     have space
    last line
    
    [root@centos-36_2 data]# 
    [root@centos-36_2 data]# tac myfile
    
    last line
     have space
    
            second line 
    first line
    [root@centos-36_2 data]# 
    

nl command

  • Command purpose

    • Add line number for non empty line when reading file
  • Command syntax
    nl [OPTION]... [FILE]...

  • Example

    [root@centos-36_2 data]# cat myfile
    first line
            second line 
    
     have space
    last line
    
    [root@centos-36_2 data]# nl myfile
         1  first line
         2          second line 
           
         3   have space
         4  last line
           
    [root@centos-36_2 data]# 
    

more command

  • Command purpose

    • Displays one screen at a time when reading a file
    • Text viewing tool based on vi editor, which can flip the screen up and down or find keywords
  • Command syntax
    more [OPTION]... [FILE]...

  • Common options

    optiondescribe
    -<num>Set the number of lines displayed per screen
    +<num>Sets the display from the specified number of rows
  • Example

    [root@centos-36_2 data]# seq 10
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [root@centos-36_2 data]# seq 10 | more +5 -3
    5
    6
    7
    --More--
    

less command

  • Command purpose

    • Displays one screen at a time when reading a file
    • It is similar to the more command, but supports flipping back and forth
  • Command syntax
    less [OPTION]... [FILE]

  • Common options

    optiondescribe
    -eAutomatically exit after displaying content
    -NAll line numbers are displayed while displaying the content, starting from 1

head command

  • Command purpose

    • When reading a file, the first few lines of information are displayed, and the first 10 lines are displayed by default
  • Command syntax
    head [OPTION]... [FILE]

  • Common options

    optiondescribe
    -n <num>Sets the number of rows to display header content
    -c <num>Sets the number of characters to display the header content
    -vDisplay file name information
  • Example

    [root@centos-36_2 data]# head -n 3 -v  myfile
    ==> myfile <==
    first line
            second line 
    
    [root@centos-36_2 data]# 
    [root@centos-36_2 data]# head -c 10 myfile
    first line[root@centos-36_2 data]# 
    [root@centos-36_2 data]# 
    [root@centos-36_2 data]# cat myfile
    first line
            second line 
    
     have space
    last line
    
    [root@centos-36_2 data]# head -n 3 -v  myfile
    ==> myfile <==
    first line
            second line 
    
    [root@centos-36_2 data]# head -c 10 myfile
    first line[root@centos-36_2 data]# 
    

tail command

  • Command purpose

    • When reading a file, several lines of information at the end are displayed, and the last 10 lines are displayed by default
  • Command syntax
    tail [OPTION]... [FILE]

  • Common options

    optiondescribe
    -n <num>Sets the number of lines to display trailing content
    -c <num>Sets the number of characters to display the trailing content
    -fDisplay the newly added content in the file in real time
    -vDisplay file name information
  • Example

    [root@centos-36_2 data]# cat myfile
    first line
            second line 
    
     have space
    last line
    
    [root@centos-36_2 data]# tail -n2 -v myfile
    ==> myfile <==
    last line
    
    [root@centos-36_2 data]# tail -c10 -v myfile
    ==> myfile <==
    ast line
    
    [root@centos-36_2 data]# 
    [root@centos-36_2 data]# tail -n3 -vf myfile
    ==> myfile <==
     have space
    last line
    
    

Posted by Frag on Thu, 30 Sep 2021 12:00:56 -0700