Common text operations in linux

cat concatenates files and prints to standard output

Grammar introduction

cat [OPTION]... [FILE]...
-A --show-all is equivalent to -vET
-b show numbers without blank lines
-n show numbers including blank lines
-E show line terminators
-s continuous multiple Line blank, show line
-T show tab
-v Use ^ and M- symbols, except for LFD and TAB.

scenes to be used:

show line number

[root@localhost ~]# cat -n anaconda-ks.cfg

connection view

[root@localhost ~]# cat -n anaconda-ks.cfg /etc/hosts

Merge files

[root@localhost ~]# cat t1 t2 > t3

Redirect text input

[root@localhost ~]# cat << EOF
hello world
EOF
hello world

Redirect file input

[root@localhost ~]# cat < anaconda-ks.cfg


more

Display files by percentage, with filter, search function, easier to read than cat for large files

parameter:

-num The number of lines displayed at a time
-d Prompt the user to display [Press space to continue, 'q' to quit.] at the bottom of the screen, if the user presses the wrong key, it will display [Press 'h' for instructions.] Instead of 'beep'
-l cancel the function that will pause when encountering a special character ^L (feed character)
-f When calculating the number of lines, use the actual number of lines, not the number of lines after word wrap (some If the number of words in a single line is too long, it will be expanded to two or more lines)
-p does not display each page in a scrolling manner, but first clears the screen and then displays the content
-c is similar to -p, except that it is displayed first Clear other old data after the content
-s When encountering a blank line with more than two consecutive lines, replace it with a blank line
-u Do not display the lower quotation mark (it varies according to the terminal specified by the environment variable TERM)
+/pattern Search for the string (pattern) before each document is displayed, then start to display after the string
+num Display fileNames from line num
The documents to be displayed can be plural

Operation command:

Enter goes down n lines, needs to be defined. The default is 1 line
Ctrl+F Scroll down one screen
Spacebar Scroll down one screen
Ctrl+B Return to the previous screen
= output the line number of the current line
: f output the file name and the line number of the current line
V call the vi editor
! command Call Shell and execute the command
/keyword to search for the keyword
q to exit more

Common operations:

[root@localhost ~]# more -s -20 anaconda-ks.cfg


less

More flexible than more, the mouse can scroll back and forth, browse at will, or lazy load, the parameter usage is similar to more

Common operations:

[root@localhost ~]# less -s -20 anaconda-ks.cfg


head

View the first few lines of the file

[root@localhost ~]# head -2 anaconda-ks.cfg


tail

View the last few lines of the file, as well as the monitoring file function

parameter:

-n show number of lines
-f monitor file

Common operations

[root@localhost ~]# tail -2 anaconda-ks.cfg
[root@localhost ~]# tail -f anaconda-ks.cfg


Based on python to implement tail to view the last few lines of a file

def read_last_lines(filename,lastlines=1):

    f = open(filename, 'rb')
    first_line_data,first_tell = f.readline(),f.tell()
    f.seek(0)
    seek = 0

    #第一层循环判断是否为空文件
    for i in f:
        while True:
            f.seek(seek,2)
            current_tell = f.tell()
            seek -= 1
            data = f.readlines()

            if(first_tell == current_tell and not data):
                f.close()
                return  '1',[first_line_data]
            elif(first_tell == current_tell and data):
                if(len(data)<lastlines):
                    data.insert(0,first_line_data)
                f.close()
                return  '2',data
            else:
                if(lastlines != 1):
                    if(len(data) == lastlines):
                        return  '3',data
                else:
                    if (len(data) == 1):
                        if (first_tell == f.tell()):
                            f.close()
                            return '4', data[-1:]

                    if (len(data) > 1):
                        f.close()
                        return  '5', data[-1:]

data = read_last_lines('test2.txt',lastlines=10)
print(data)

To be continued....

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324604334&siteId=291194637