Linux file content viewing and editing

Linux file content viewing and editing

Keywords: cat, head, tail, more, less, sed, vi,grep

1. Linux file content viewing and editing points

  • Concatenate files and print to stdout - use cat

  • Display the first few lines of the specified file - use head

  • Display the last few lines of the specified file, often used to print the contents of the log file in real time - use tail

  • Display the contents of the file, one screen at a time - use more

  • Display file contents, one screen at a time - use less

  • Automatically edit one or more files; simplify repeated operations on files; write conversion programs, etc. - using sed

  • Text editor - use vi

  • Use regular expressions to search for text and print the matching lines - use grep

2. Common usage of commands

2.1. cat

The cat command is used to concatenate files and print to the standard output device.

Reference: http://man.linuxde.net/cat

Example:

cat m1 #Display the contents of the file ml on the screen 
cat m1 m2 #Display the contents of the files ml and m2 at the same time 
cat m1 m2 > file #Merge the files ml and m2 into the file file

2.2. head

The head command is used to display the beginning of the file. By default, the head command displays the first 10 lines of the file.

Reference: http://man.linuxde.net/head

2.3. tail

The tail command is used to display the tail content of the file. By default, the tail command displays the last 10 lines of the file. If more than one file is given, a filename header is prepended to each file displayed. If no file is specified or the filename is "-", standard input is read.

Reference: http://man.linuxde.net/tail

Example:

tail file # Display the last 10 lines of the file file 
tail -n +20 file # Display the content of the file file, from the 20th line to the end of the file 
tail -c 10 file # Display the last 10 characters of the file file

2.4. more

The more command is a text filter based on the vi editor. It displays the content of the text file page by page in a full-screen mode, and supports the keyword positioning operation in vi. There are several shortcut keys built into the more list, the commonly used ones are H (get help information), Enter (scroll down one line), space (scroll down one screen), Q (quit command).

This command displays a screen of text at a time, stops when the screen is full, and a prompt message appears at the bottom of the screen, giving the percentage of the file that has been displayed so far: --More-- (XX%) can use the following different methods Answer the prompts:

  • Press the Space key: Display the next screen of text.

  • Press the Enier key: only the next line of text will be displayed.

  • Press the slash |: Then enter a pattern to find the next matching pattern in the text.

  • Press H key: display the help screen with related help information.

  • Press B key: display the content of the previous screen.

  • Press the Q key: exit the rnore command.

Reference: http://man.linuxde.net/more

Example:

# Display the content of the file file, but clear the screen before displaying, and display the percentage of complete core at the bottom of the screen. 
more -dc file 
​#
Display the content of the file file, every 10 lines, and clear the screen before displaying. 
more -c -10 file

2.5. less

The function of the less command is very similar to that of more, both of which can be used to browse the content of text files. The difference is that the less command allows users to browse files forward or backward, while the more command can only browse forward. When using the less command to display files, use the PageUp key to page up and the PageDown key to page down. To exit the less program, press the Q key.

Example:

less /var/log/shadowsocks.log

2.6. sed

sed is a stream editor, it is a text processing tool, it can be perfectly used with regular expressions, and its functions are extraordinary. When processing, store the currently processed line in a temporary buffer, which is called "pattern space", and then use the sed command to process the contents of the buffer, and after the processing is completed, send the contents of the buffer to the screen. Then process the next line, and repeat until the end of the file. The file contents are not changed unless you use redirected storage output. Sed is mainly used to automatically edit one or more files; simplify repeated operations on files; write conversion programs, etc.

Reference: http://man.linuxde.net/sed

Example:

# Replace the string in the text 
sed 's/book/books/' file 
​#
The -n option is used together with the p command to print only those lines that have been replaced 
sed -n 's/test/TEST/p' file 
​#
Directly edit the file option -i, which will match the first book of each line in the file file and replace it with books 
sed -i 's/book/books/g' file 
​#
Use the suffix /g flag to replace all matches in each line 
sed 's/book/books/g' file 
​#
Delete blank lines 
sed '/^$/d' file 
​#
Delete the second line of the file 
sed '2d' file 
​#
Delete all lines from the second line to the end of the file 
sed '2,$d' file 
​#
Delete the last line of the file 
sed '$d' file 
​#
Delete all lines that start with test in the file 
sed '/^test/'d file

2.7. vi

The vi command is the most versatile full-screen plain text editor for UNIX and UNIX-like operating systems. The vi editor in Linux is called vim, which is an enhanced version of vi (vi Improved), fully compatible with vi editor, and has achieved many enhanced functions.

Reference: http://man.linuxde.net/vi

Further reading: Getting started with Vim

2.8. grep

grep (global search regular expression (RE) and print out the line, a comprehensive search for regular expressions and print out the line) is a powerful text search tool that can use regular expressions to search for text and print out matching lines come out.

Reference: http://man.linuxde.net/grep

Example:

# Recursive search for text in multi-level directories (programmers' favorite code search): 
$ grep "class" . -R -n 
​#
Ignore character case in matching patterns 
$ echo "hello world" | grep -i "HELLO" 
​#
Match multiple patterns: 
$ grep -e "class" -e "vitural" file 
​#
Only recursively search for the character "main()" in all .php and .html files in the directory 
$ grep "main ()" . -r --include *.{php,html} 
​#
Exclude all README files from search results 
$ grep "main()" . -r --exclude "README" 
​#
Exclude filelist from search results Files in filelist 
$ grep "main()" . -r --exclude-from filelist

3. References

Guess you like

Origin blog.csdn.net/zy_dreamer/article/details/132177856