wanlifeipeng

  博客园 :: 首页 :: 博问 :: 闪存 :: :: 联系 :: 订阅 订阅 :: 管理 ::

cat - concatenate files and print on the standard output

连/拼接多个文件,然后将这些文件的内容打印到标准输出

一. cat 主要功能:

1.显示文件全部内容:

形如: cat 文件名

[hupeng@hupeng-vm shell]$cat 1.txt

2.创建新的文件

形如: cat > 文件名

[hupeng@hupeng-vm shell]$cat > 1.txt  #输入ctrl+D退出输入
hello 
world
[hupeng@hupeng-vm shell]$cat 1.txt 
hello
world

3.将多个文件的内容合并到新的文件

形如: cat 文件1 文件2 > 新文件名

[hupeng@hupeng-vm shell]$echo "hello world" > 1.txt
[hupeng@hupeng-vm shell]$echo "hello shell" > 2.txt
[hupeng@hupeng-vm shell]$cat 1.txt 2.txt > 3.txt
[hupeng@hupeng-vm shell]$cat 3.txt 
hello world
hello shell

二.常用参数

-n 给输出行编号(包括空行)

-b 给输出行编号(忽略空行)

[hupeng@hupeng-vm shell]$echo -e "hello world\nhello shell\n\nhello linux" > out.txt
[hupeng@hupeng-vm shell]$cat out.txt 
hello world
hello shell

hello linux
[hupeng@hupeng-vm shell]$cat -n out.txt  #输出带编号
     1    hello world
     2    hello shell
     3    
     4    hello linux
[hupeng@hupeng-vm shell]$cat -b out.txt  #跳过空行编号
     1    hello world
     2    hello shell

     3    hello linux

 

三、cat << EOF使用

EOF(end of file),其实它只是一个标志,可以用其他合法字符替换,如HHH

[hupeng@hupeng-vm shell]$cat << EOF  #从终端获取多行输入,直接打印到标准输出
> hello world
> hello linux
> hello c/c++
> hello shell
> EOF
hello world
hello linux
hello c/c++
hello shell
[hupeng@hupeng-vm shell]$cat << EOF > out.txt #将标准输入中个内容从定向到文件
> hi linux
> hi shll
> EOF
[hupeng@hupeng-vm shell]$cat out.txt 
hi linux
hi shll

使用HHH替换EOF

[hupeng@hupeng-vm shell]$cat << HHH
> hello
> world
> end with HHH
> HHH
hello
world
end with HHH

 

 

    

posted on 2017-04-20 13:21  wanlifeipeng  阅读(985)  评论(0编辑  收藏  举报