栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Linux文本处理命令

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux文本处理命令

目录

echo 标准输出命令(打印命令)

          > 输出重定向符

>> 追加输出重定向符

< 输入重定向符

<< 指定终止结束符

文件数据流描述符  :

| 管道符

cut文本内容剪切命令

sort  排序命令

uniq   去重

grep文本过滤命令


echo 标准输出命令(打印命令)

echo可直接输出

[root@localhost ~]# echo  shu chu
shu chu
[root@localhost ~]# 

也可用于显示变量($引用变量)

[root@localhost ~]# name=lisi
[root@localhost ~]# echo $name
lisi
[root@localhost ~]# 
 

显示系统SHELL

[root@localhost ~]# echo $SHELL
/bin/bash
#当前系统正在使用的bash shell

> 输出重定向符

配合echo可将编辑内容写入指定文件

(此时若继续使用>写入,文件原内容会被覆盖)

[root@localhost ~]# echo 123 > aaa.txt           -- - -- - - 若该文件不存在,则会自创建该文件
[root@localhost ~]# cat aaa.txt
123
[root@localhost ~]# 
 

>> 追加输出重定向符

使用>>编辑文件内容  原内容不会覆盖  会追加写入内容

[root@localhost ~]# echo 456 >> aaa.txt
[root@localhost ~]# echo 789 >> aaa.txt
[root@localhost ~]# cat aaa.txt
123
456
789
[root@localhost ~]# 
 

< 输入重定向符

有查看的一个作用    可以直接用cat 查看

[root@localhost ~]# cat < aaa.txt
123
456
789
[root@localhost ~]#

<< 指定终止结束符

终止结束符 可以为任何字符   一般为EOF    

在遇见终止结束符交后   互式结束  会将刚刚写入内容打印出来

[root@localhost ~]# cat << haha
> 123
> 456
> haha
123
456
[root@localhost ~]# 
 

若指定输出文件后 也可将交互式中内容写入文件

[root@localhost ~]# cat << EOF > /f.txt
> 1
> 2
> 3
> EOF
[root@localhost ~]# cat /f.txt
1
2
3
[root@localhost ~]# 

文件数据流描述符  :

0标准输入

1标准输出

2错误输出

&与 表示1和2

[root@localhost ~]# data 1> t.txt
[root@localhost ~]# data  2>f.txt
[root@localhost ~]# data  &>h.txt

[root@localhost ~]# data 1> t.txt 2> f.txt
 

/dev/null      (数据黑洞文件--垃圾箱)

把所有错误输出结果 重定向到/dev/null

[root@localhost ~]# date 2>/dev/null


| 管道符

将前面命令的标准输出做后面命令的标准输入

[root@localhost ~]# date
Mon Jun  6 08:25:34 PDT 2022
[root@localhost ~]# date | cut -d " " -f 5 
08:25:55
[root@localhost ~]# 

        |tee   横向输出是将结果写入指定文件

         纵向输出是将前面命令结果打印到终端

[root@localhost ~]# cat file | tee file.txt

123

[root@localhost ~]#   cat file.txt

123

[root@localhost ~]#  echo this is file >file

[root@localhost ~]#  cat file | tee -a file.txt

this is file

[root@localhost ~]# cat file.txt

123

this is file

创建管道符文件    命令mkfifo

例如    mkfifo /aaa

开启两个终端   可 在终端一  创建管道文件mkfifo /aaa 

  在aaa中写入  echo this a  >  /aaa

然后在终端二 里输出  cat /aaa

(需要有输入 有输出)

cut文本内容剪切命令

特点:  1.基于行进行处理

             2.不会对源文件进行更改 ,会将处理结果显示到终端

[root@localhost ~]# cut -c 3     passwd                      #将每一行第三个文件进行剪切
[root@localhost ~]# cut -c 1-3  passwd                     #将每一行第一到三的文件剪切

                                    字符       文件名
 

 

 [root@localhost ~]# cut  -d   :  -f1   passwd               #将每一行的第一个字段剪切出来

                          -d 指定分隔符   此处为:      -f指定字段   

 [root@localhost ~]# cut  -d:  -f     1   passwd       将passwd文件第一字段截取显示在终端 

sort  排序命令

使用方法:sort   file(sort + 文件名)

根据字符作比较排序 

sort -n  file   (-n表示   启用数值排序)

sort -rn  file   (-r表示逆序    -rn为逆序按数值比较操作)

sort   -u    (-u 可以做去重 )

sort   -t      (指定分隔符)

sort   -k       (指定字段)  

应用:

[root@localhost ~]# sort  -t : -k 3 -n passwd

将指定文件显示 按照第三个字段数值  从小到大显示每行

uniq   去重

与sort配合使用功能变化较多

uniq  file  (uniq+文件名)

去重时    重复行必须相邻   

通过sort使重复行相邻  再通过uniq去重

[root@localhost ~]# sort file | uniq

                                sort -u file       #这两种命令效果大体相同

显示一个文件重复最多的一行 到终端

先使重复行相邻

[root@localhost ~]#sort file

再做重复行次数统计

[root@localhost ~]#sort file | uniq -c

再做一次排序

[root@localhost ~]#sort file | uniq -c | sort -r  |  head -1

uniq -c   统计每一行重复次数

uniq -d   仅显示重复的行

uniq -D  显示所有的重复行

grep文本过滤命令

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/964056.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号