linux-cmd-cat内容

发布时间 2023-05-17 15:31:05作者: 天生帅才

linux-cmd-cat内容

1.查看文本内容

[root@zuiyoujie tools] cat aaa.txt 
I am a good boy.

[root@zuiyoujie tools]# cat -n file.txt        # 输出所有行的编号
     1    欢迎来到路飞学城
     2    学生还有五分钟到达战场
     3
     4
     5    全军出击

[root@zuiyoujie tools]# cat -b file.txt            # 输出非空行的编号
     1    欢迎来到路飞学城
     2    学生还有五分钟到达战场


     3    全军出击

[root@zuiyoujie tools]# cat -E file.txt        #显示出每行的结束符,$符号
欢迎来到路飞学城$
学生还有五分钟到达战场$
$
$
全军出击$

[root@zuiyoujie tools]# cat -s file.txt        # -s参数把多个空行,换成一个,可以让文件更精炼阅读
欢迎来到路飞学城
学生还有五分钟到达战场

全军出击

2.创建文本并写入内容

[root@zuiyoujie tools]# cat >aaa.txt
I am a good boy .

按crtl+D结束输入

3.多行内容追加

[root@zuiyoujie tools]# cat >>test1.txt<<EOF
> 1 2 3 4
> 5 6 7
> 8 9
> 10
> EOF
[root@zuiyoujie tools]# cat test1.txt
1 2 3 4
5 6 7
8 9
10
----------------------

后面的EOF可以更换成其他,相应的结尾也要使用相同的标记
----------------------
[root@zuiyoujie tools]# cat >>test2.txt<<AAA
> asd
> qwe
> zxc
> AAA
[root@zuiyoujie tools]# cat test2.txt 
asd
qwe
zxc
[root@zuiyoujie tools]# 
-------------------------------------------------------
  • 其他实例
cat << EOF | kubectl apply -f -
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: ops-deploy
  namespace: kube-system

---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: ops-deploy
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: ops-deploy
  namespace: kube-system
EOF

tac命令

与cat命令作用相反,按行从后往前读取文件内容