Home  >  Article  >  Operation and Maintenance  >  Can linux export file contents?

Can linux export file contents?

藏色散人
藏色散人Original
2023-03-13 11:05:142495browse

Linux can export the file content. In Linux, you can use the cat command to export the file content; the cat command is commonly used to display the file content, or to connect several files for display, or to read the content from the standard input and display it. The usage syntax is "cat [xuanxiang] [wenjian]".

Can linux export file contents?

#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

Can Linux export file contents?

Can.

Use cat to output file contents in Linux

Function: Connect one or more files (or standard input) together and output to standard. (Concatenate FILE(s), or standard input, to standard output.)

The cat command is often used to display the contents of a file, or to connect several files for display, or to read from standard input. Get the content and display it. It is often used in conjunction with the redirection symbol. cat is the abbreviation of Concatenate.

Linux has three special files, respectively
-Standard input is STDIN, in /dev/stdin
Generally refers to keyboard input, the code number in shell is 0
-Standard output STDOUT, in /dev/stdout
Generally refers to the terminal, which is the monitor. The code name in the shell is 1
-Standard error STDERR, in /dev/stderr
also refers to the terminal, the difference is that the error message is sent here
The code name in the shell is 2
语法:cat   [选项]   [文件]
##-b--number-nonblankFor non-empty output line numbers##-e -E##-n--number Number all lines of output-s--squeeze-blankDo not output multiple blank lines-t is equivalent to -vT-T --show-tabsDisplay tab characters as ^I-v--show-nonprinting Use ^ and M- for citations, except LFD and TAB
输出文件内容 

显示文件FILE的内容 cat FILE 

显示文件FILE的内容加上行号,需要加上参数-n。行号从1开始。使用-n参数时,所有空行也会显示行号 cat -n FILE 

忽略掉空行,用-b cat -b FILE 

当遇到有连续两行以上的空白行,就代换为一行的空白行,可以使用-s参数 cat -s FILE 
 保存内容 

将标准输入保存到文件FILE中,如果文件已经存在,则覆盖掉原来的 cat >FILE 

将标准输入追加到文件FILE末尾 cat >>FILE  
 合并文件 
  
将两个文件FILE1和FILE2的内容合并为一个文件FILE cat FILE1 FILE2 >FILE
Short option Long option Meaning
-A --show-all equals-vET
is equal to -vE
--show-ends Display "$" at the end of each line
cat usage example
从标准输入创建文件 
[root@web setup]# code>cat >1.txt 
Hello 
Bash 
Linux 
键盘(快捷键)Ctrl+D 保存文件 
 
[root@web setup]# ls -l 1.txt 
-rw-r--r-- 1 root root 17 11-02 21:32 1.txt 
[root@web setup]# cat 1.txt   显示1.txt文本内容。标准输出 
Hello 
Bash 
Linux 
[root@web setup]# cat <1.txt 
Hello 
Bash 
Linux 
[root@web setup]# 
 
 
 使用heredoc来生成文件 
注意:粗体部分、here doc可以进行字符串替换 
[root@web setup]# cat >2.txt <<EOF 
> Hello 
> Bash 
> Linux 
> PWD=$(pwd) 
> EOF 
 
[root@web setup]# ls -l 2.txt 
-rw-r--r-- 1 root root 33 11-02 21:35 2.txt 
[root@web setup]# cat 2.txt 
Hello 
Bash 
Linux 
PWD=/root/setup  
[root@web setup]# 
 
 
 输出行号 
[root@web setup]# cat -n 1.txt 
     1  Hello 
     2  Bash 
     3  Linux 
[root@web setup]# nl 1.txt 
     1  Hello 
     2  Bash 
     3  Linux 
[root@web setup]# 
 
 
 在bash脚本中把文件内容加载到变量中 
[root@web ~]# TEXT=$(cat .bash_profile) 
[root@web ~]# 
[root@web ~]# echo "$TEXT" 
# .bash_profile 
 
# Get the aliases and functions 
if [ -f ~/.bashrc ]; then 
        . ~/.bashrc 
fi 
 
# User specific environment and startup programs 
 
PATH=$PATH:$HOME/bin 
 
export PATH 
unset USERNAME 
[root@web ~]#

heredoc documentIn linux shell scripts we often see something similar to cat << EOF Those who are not familiar with the statement may find it strange: EOF seems to be the end of the file. What role does it play here? EOF is "end of file", indicating the end of text character.

< (Content)

EOF



EOF has no special meaning here, you can use FOE or OOO, etc. (of course there are no restrictions in three characters or uppercase characters).

    You can replace EOF with something else, which means passing the content as standard input to the process.
  • By combining these two identifiers, you can avoid using the multi-line echo command and implement Multi-line output results.
  • Next, briefly describe several common usage methods and their functions:

cat <

    cat >filename, creates a file, and outputs the standard input to the filename file, using ctrl d as input.
  • cat >filename <
  • # cat >test.sh < #!/bin/bash
    > #you Shell script writes here.
    > EOF
     
  • Other writing methods1. Append files

    # cat <>test.sh


    2. Append the file, change the writing method

    #cat >>test.sh <
    3. EOF is just a mark, not fixed. The "HHH" here replaces the function of "EOF". The result is the same.

    #cat <iii.txt


    > sdlkfjksl > sdkjflk > asdlfj
    > HHH

    4. Not in script. If it is not in the script, we can use Ctrl D to output the EOF identification

    #cat >iii.txt


    skldjfklj sdkfjkl kljkljklj
    kljlk
    Ctrl D


    Related recommendations: "

    Linux Video Tutorial
"

The above is the detailed content of Can linux export file contents?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn