Linux outputs content to a specified file

1. Record terminal output to text file

1.1 Solution 1: Exploits >and >>Commands

  1. the difference:

>Is to redirect the output to the specified file. Note: If the file already exists, it will be rewritten and the original content of the file will not be retained.
>>The output is appended to the end of the file, and the original content of the file will be retained.

  1. example:
ls>ls.txt    #或者ls-->ls.txt,把ls命令的运行结果保存到文件ls.txt中
ls>>ls.txt   #把ls命令的运行结果附加到文件ls.txt中

1.2 Solution 2: Use teethe command

  1. Command function:
    Record the information to a file while outputting it.
  2. example:
ls | tee ls.txt   # 将会在终端上显示ls命令的执行结果,并把执行结果输出到ls.txt 文件中
ls | tee -a ls.txt # 把ls命令的执行结果添加到ls.txt文件的后面。
 # 参数说明: -a, --append,“append to the given FILEs, do not overwrite“,附加至给出的文件

1.3 Solution 3: Use script the command

  1. Command function:
    Record all contents on the terminal to a file
  2. example:
script -a /home/sky1/sc.txt	# 将接下来的内容全部记录到/home/sky1/sc.txt中
# 操作1
# 输出1
# 操作2
# 输出2
# 操作3
# 输出3
# ... ...
exit # 退出记录

Insert image description here

2. Practice

Exercise content: Intercept the content of specified consecutive lines in a large file to a small file.
Problem: It is known that there is a large file big.txt. One line has the content "start point" and one line has the content "end point". Please start point" is the starting line, and "end point" is the ending line. Please intercept the content between the two lines (including the starting and ending lines) to the small file little.txt.
Solutions:

  1. Use cat -nthe and |grepcommand to locate the starting and ending line numbers
  2. Use sedthe command to intercept (note the quotation marks and p) and use >the or >>command to write to the file

Screenshots of experimental steps:
Insert image description here

Guess you like

Origin blog.csdn.net/qq_38662733/article/details/131942619