Linux文件过滤及内容编辑处理

Linux文件过滤及内容编辑处理

1.vi/vim纯文本编辑器

Vim是一个高度可配置的文本编辑器,其构建目的是使创建和更改任何类型的文本非常高效。它被作为“vi”包含在大多数UNIX系统和苹果OS X中。

Vim是非常稳定的,它的特点包括:

  • 持久的、多层次的撤销树

  • 广泛的插件系统

  • 支持数百种编程语言和文件格式

  • 强大的搜索和替换功能

  • 与许多集成工具

【vim的三种模式】:

  • 普通模式
  • 编辑模式
  • 命令模式

(1):进入编辑器

linfei@ubuntu:~$ vim test.txt
# 使用vim编辑test.txt

在这里插入图片描述

此时为普通模式,在这个模式下不能进行编辑操作,但是可以移动光标。

(2):编辑模式

在普通模式下,按下“i”键进入编辑模式:(编辑模式下按“ESC”键进入普通模式)
在这里插入图片描述

(3)命令模式

在普通模式下,输入“ : ”进入命令模式:(wq为保存,q直接退出,wq!强制保存退出)
在这里插入图片描述

三种模式转换图:

在这里插入图片描述

2.echo:显示输出文本内容

echo命令能将指定文本显示在Linux命令行上或者通过重定向符写入到指定文件中

linfei@ubuntu:~$ echo hello world
hello world
linfei@ubuntu:~$ echo 'hello world'
hello world
linfei@ubuntu:~$ echo "hello world"
hello world
linfei@ubuntu:~$ echo -e hello\tworld
hellotworld
linfei@ubuntu:~$ echo -e 'hello\tworld' #-e:可以识别特殊字符
hello	world

将单行文本追加到某个文件中:

linfei@ubuntu:~$ echo "hello world" >> hello.txt
linfei@ubuntu:~$ ls
anaconda  Documents  hello.txt  Pictures  shell-study  Templates
Desktop   Downloads  Music      Public    snap         Videos
linfei@ubuntu:~$ cat hello.txt 
hello world
linfei@ubuntu:~$ 

3.cat:合并文件或查看文件内容

  • 查看文件

  • 合并文件

    linfei@ubuntu:~$ ls
    anaconda  Documents  hello.txt  Pictures  shell-study  Templates
    Desktop   Downloads  Music      Public    snap         Videos
    linfei@ubuntu:~$ touch hello1.txt
    linfei@ubuntu:~$ ls
    anaconda   Downloads   Music     shell-study  Videos
    Desktop    hello1.txt  Pictures  snap
    Documents  hello.txt   Public    Templates
    linfei@ubuntu:~$ cat hello.txt hello1.txt > hello2.txt
    linfei@ubuntu:~$ ls
    anaconda   Downloads   hello.txt  Public       Templates
    Desktop    hello1.txt  Music      shell-study  Videos
    Documents  hello2.txt  Pictures   snap
    linfei@ubuntu:~$ 
    
    linfei@ubuntu:~$ cat -n hello2.txt # -n:显示行号
         1	hello world
    linfei@ubuntu:~$ 
    

    4.more/less:分页显示文件内容

    子命令说明
    空格键向下翻一页
    回车向下翻一行
    b向上翻一页
    /查找指定文本

5.head/tail:显示文件头部/尾部内容

linfei@ubuntu:~$ head hello.txt 
hello world
编钟礼乐的嘈杂
  黄金的夺目
  耀眼的长钉深深楔入了我们的耳朵
  布衣粗食的墨家子弟仍在盼望
  多么古老的部族
  铭记着早已风干的历史
  埋藏着多少记忆的国度
  
  脚踏染满鲜血的边疆
linfei@ubuntu:~$ tail hello.txt
  你要我记住什么?
  青天,
  你要我记住什么?
  
  青天无言,大地轻叹
  不肯回答
  我们依然迷茫
  我们长久哀叹
  因为我们记住了先贤的语句
  却得不到新颖的回答
linfei@ubuntu:~$ head -n 5 hello.txt
hello world
编钟礼乐的嘈杂
  黄金的夺目
  耀眼的长钉深深楔入了我们的耳朵
  布衣粗食的墨家子弟仍在盼望
linfei@ubuntu:~$ tail -n 5 hello.txt 
  不肯回答
  我们依然迷茫
  我们长久哀叹
  因为我们记住了先贤的语句
  却得不到新颖的回答

6.grep:文本过滤工具

grep是Linux系统中最重要的命令之一,其功能是从文本文件或管道数据流中筛选匹配的行或数据。

如果配合正则表达式使用,功能非常强大,是运维人员必须要掌握的命令

参数含义
-v显示不匹配的行
-n显示匹配行及行号
-i不区分大小写,默认是区分大小写
-c只统计匹配的行数
–color==auto给grep过滤的匹配字符串添加颜色
-w只匹配过滤的单词
-o只输出匹配的内容
linfei@ubuntu:~$ grep -n '不肯回答' hello.txt 
24:  不肯回答
linfei@ubuntu:~$ grep -c '不肯回答' hello.txt 
1
linfei@ubuntu:~$ grep -o '不肯回答' hello.txt 
不肯回答
linfei@ubuntu:~$ grep -Ei --color=auto '不肯回答' hello.txt

7.tr:替换或删除字符

tr命令从标准输入中替换,缩减或者删除字符,并将结果输出

linfei@ubuntu:~$ tr --help
Usage: tr [OPTION]... SET1 [SET2]
Translate, squeeze, and/or delete characters from standard input,
writing to standard output.

  -c, -C, --complement    #使用第一个字符串(SET1)的补集,取反
  -d, --delete            delete characters in SET1, do not translate
  -s, --squeeze-repeats   # 保留连续字符的第一个字符,删除其他字符
  -t, --truncate-set1     first truncate SET1 to length of SET2
      --help     display this help and exit
      --version  output version information and exit
linfei@ubuntu:~$ cat tr-test.txt 
1.Remember what should be remembered, and forget what should be forgotten.Alter what is changeable, and accept what is mutable
2.Apart from tears, only time could wear everything away. While feeling is being processed by time, conflicts would be reconciled as time goes by, just like a cup of tea that is being continuously diluted

linfei@ubuntu:~$ tr '12' '34' < tr-test.txt 
3.Remember what should be remembered, and forget what should be forgotten.Alter what is changeable, and accept what is mutable
4.Apart from tears, only time could wear everything away. While feeling is being processed by time, conflicts would be reconciled as time goes by, just like a cup of tea that is being continuously diluted
linfei@ubuntu:~$ tr '[0-9]' '[a-j]' < tr-test.txt 
b.Remember what should be remembered, and forget what should be forgotten.Alter what is changeable, and accept what is mutable
c.Apart from tears, only time could wear everything away. While feeling is being processed by time, conflicts would be reconciled as time goes by, just like a cup of tea that is being continuously diluted

8.重定向符号

  • Linux里的重定向:将数据从一个地方(文件或工具)无损地留到另一个地方(文件或工具)
  • 标准输入:表示数据地一个流入方向,通常表示数据从文件等流入到处理地工具或命令中,用代码0表示,使用<或<<符号来指示数据朝箭头所指方向流动
  • 标准输出:表示数据地一个流入方向,用代码1表示,使用>或>>符号来指示数据朝箭头所指方向流动

s being continuously diluted


## 8.重定向符号

* Linux里的重定向:将数据从一个地方(文件或工具)无损地留到另一个地方(文件或工具)
* 标准输入:表示数据地一个流入方向,通常表示数据从文件等流入到处理地工具或命令中,用代码0表示,使用<或<<符号来指示数据朝箭头所指方向流动
* 标准输出:表示数据地一个流入方向,用代码1表示,使用>或>>符号来指示数据朝箭头所指方向流动

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值