Linux基础_文件管理命令(三)

文件管理命令三

文件过滤命令

|:管道符
# 作用:将管道符左边命令的标准输出,交给管道符右边命令的标准输入来处理
举例: 
[root@localhost ~]# head /work6/777.txt | tail -3
www.sina.com
www.baidu.com
www.123.com

# 命令:
grep (过滤输出内容)

^:以...开头
$:以...结尾
*:匹配*前面的内容至少0次或多次
.:匹配任意字符


## 选项
-n:打印行号 number
-A:after 查看过滤内容的后N行
-B:before 查看过滤内容的前N行
-C:center 查看过滤内容的前、后N行
-v:取反
-E:可以过滤多个结果
-o:只打印出过滤内容
-i:不区分大小写

举例:
[root@localhost ~]# cat /work6/hxs
一、《浣溪沙.一曲新词酒一杯》——宋.晏殊

一曲新词酒一杯,

I compose a new song and drink a cup of wine

去年天气旧亭台。

In the bower of last year when weather is as fine.

夕阳西下几时回。

When will you come back like the sun on the decline?

无可奈何花落去,

Deeply I sigh for the fallen flowers in vain.

似曾相识燕归来。

Vaguely I seem to know the swallows come again.

小园香径独徘徊。

Loitering on the garden path, I alone remain.

# ^
[root@localhost ~]# grep '^一' /work6/hxs
一、《浣溪沙.一曲新词酒一杯》——宋.晏殊
一曲新词酒一杯,

[root@localhost ~]# grep '^W' /work6/hxs
When will you come back like the sun on the decline?

# $
[root@localhost ~]# grep 'e$' /work6/hxs
I compose a new song and drink a cup of wine

[root@localhost ~]# grep '.$' /work6/hxs
一、《浣溪沙.一曲新词酒一杯》——宋.晏殊
一曲新词酒一杯,
I compose a new song and drink a cup of wine
去年天气旧亭台。
In the bower of last year when weather is as fine.
夕阳西下几时回。
When will you come back like the sun on the decline?
无可奈何花落去,
Deeply I sigh for the fallen flowers in vain.
似曾相识燕归来。
Vaguely I seem to know the swallows come again.
小园香径独徘徊。
Loitering on the garden path, I alone remain.

[root@localhost ~]# grep '杯,$' /work6/hxs
一曲新词酒一杯,

# 例3:反义符让$失去原本的意义,只是过滤含$的内容
[root@localhost ~]# grep '\$' /work6/hxs
无可奈何$花落去,
Vaguely I seem to know$ the swallows come again.

# . 和 *
举例:'f.''f'的区别 少了一个27行的内容  这就是 f. 是要f匹配一个字符的才会被过滤下来并且f和匹配的字符都高亮显示

[root@localhost ~]# grep -n 'f.' /work6/hxs
5:I compose a new song and drink a cup of wine
9:In the bower of last year when weather is as fine.
17:Deeply I sigh for the fallen flowers in vain.

[root@localhost ~]# grep -n 'f' /work6/hxs
5:I compose a new song and drink a cup of wine
9:In the bower of last year when weather is as fine.
17:Deeply I sigh for the fallen flowers in vain.
27:宋.晏殊 f

含f的行出现,不含f的也会出现,只不过含f的会高亮显示,不同于f.和 f
[root@localhost ~]# grep -n 'f*' /work6/hxs
1:一、《浣溪沙.一曲新词酒一杯》——宋.晏殊
2:
3:一曲新词酒一杯,
4:
5:I compose a new song and drink a cup of wine
6:
7:去年天气旧亭台。
8:
9:In the bower of last year when weather is as fine.
10:
11:夕阳西下几时回。
12:
13:When will you come back like the sun on the decline?
14:
15:无可奈何$花落去,
16:
17:Deeply I sigh for the fallen flowers in vain.
18:
19:似曾相识燕归来。
20:
21:Vaguely I seem to know$ the swallows come again.
22:
23:小园香径独徘徊。
24:
25:oLoitering on the garden path, I alone remain.
26:
27:宋.晏殊 f

[root@localhost ~]# grep -n '.*' /work6/hxs
这样都会高亮显示


# -A -B -C

举例:
[root@localhost ~]# grep -n '回' /work6/hxs
11:夕阳西下几时回。

-A
[root@localhost ~]# grep -A 3 -n '回' /work6/hxs
11:夕阳西下几时回。
12-
13-When will you come back like the sun on the decline?
14-


-B
[root@localhost ~]# grep -B 2 -n '回' /work6/hxs
9-In the bower of last year when weather is as fine.
10-
11:夕阳西下几时回。


-C
[root@localhost ~]# grep -A 4 -B 2 -n '回' /work6/hxs
9-In the bower of last year when weather is as fine.
10-
11:夕阳西下几时回。
12-
13-When will you come back like the sun on the decline?
14-
15-无可奈何$花落去,


-A 4 -B 2
[root@localhost ~]# grep -A 4 -B 2 -n '回' /work6/hxs
9-In the bower of last year when weather is as fine.
10-
11:夕阳西下几时回。
12-
13-When will you come back like the sun on the decline?
14-
15-无可奈何$花落去,



# -v
[root@localhost ~]# grep -v '回' /work6/hxs
一、《浣溪沙.一曲新词酒一杯》——宋.晏殊

一曲新词酒一杯,

I compose a new song and drink a cup of wine

去年天气旧亭台。

In the bower of last year when weather is as fine.


When will you come back like the sun on the decline?

无可奈何$花落去,

Deeply I sigh for the fallen flowers in vain.

似曾相识燕归来。

Vaguely I seem to know$ the swallows come again.

小园香径独徘徊。

oLoitering on the garden path, I alone remain.

宋.晏殊 f


# -E
## 在grep命令中'|' 是 “或” 的意思
[root@localhost ~]# grep -E 'a|回' /work6/hxs
I compose a new song and drink a cup of wine
In the bower of last year when weather is as fine.
夕阳西下几时回。
When will you come back like the sun on the decline?
Deeply I sigh for the fallen flowers in vain.
Vaguely I seem to know$ the swallows come again.
oLoitering on the garden path, I alone remain.

[root@localhost ~]# grep -E 'a|回|a|来' /work6/hxs
I compose a new song and drink a cup of wine
In the bower of last year when weather is as fine.
夕阳西下几时回。
When will you come back like the sun on the decline?
Deeply I sigh for the fallen flowers in vain.
似曾相识燕归来。
Vaguely I seem to know$ the swallows come again.
oLoitering on the garden path, I alone remain.



# -o
[root@localhost ~]# grep -o '来' /work6/hxs
来
来
来
来
来

[root@localhost ~]# grep '来' /work6/hxs
似曾相识燕归来。
来一个
来两个
来来



# -i
[root@localhost ~]# grep -i 'w' /work6/hxs
I compose a new song and drink a cup of wine
In the bower of last year when weather is as fine.
When will you come back like the sun on the decline?
Deeply I sigh for the fallen flowers in vain.
Vaguely I seem to know$ the swallows come again.

[root@localhost ~]# grep 'W' /work6/hxs
When will you come back like the sun on the decline?




# [a-zA-Z] []是一个范围的意思
[root@localhost ~]# grep -n '[a-z]' /work6/hxs
5:I compose a new song and drink a cup of wine
9:In the bower of last year when weather is as fine.
13:When will you come back like the sun on the decline?
17:Deeply I sigh for the fallen flowers in vain.
21:Vaguely I seem to know$ the swallows come again.
25:oLoitering on the garden path, I alone remain.
27:宋.晏殊 f

[root@localhost ~]# grep -n -E 'a|z' /work6/hxs
5:I compose a new song and drink a cup of wine
9:In the bower of last year when weather is as fine.
13:When will you come back like the sun on the decline?
17:Deeply I sigh for the fallen flowers in vain.
21:Vaguely I seem to know$ the swallows come again.
25:oLoitering on the garden path, I alone remain.

[root@localhost ~]# grep -n '[A-Z]' /work6/hxs
5:I compose a new song and drink a cup of wine
9:In the bower of last year when weather is as fine.
13:When will you come back like the sun on the decline?
17:Deeply I sigh for the fallen flowers in vain.
21:Vaguely I seem to know$ the swallows come again.
25:oLoitering on the garden path, I alone remain.

[root@localhost ~]# grep -n '[a-zA-Z]' /work6/hxs
5:I compose a new song and drink a cup of wine
9:In the bower of last year when weather is as fine.
13:When will you come back like the sun on the decline?
17:Deeply I sigh for the fallen flowers in vain.
21:Vaguely I seem to know$ the swallows come again.
25:oLoitering on the garden path, I alone remain.
27:宋.晏殊 f

[root@localhost ~]# grep -n -E '[a-c]' /work6/hxs
5:I compose a new song and drink a cup of wine
9:In the bower of last year when weather is as fine.
13:When will you come back like the sun on the decline?
17:Deeply I sigh for the fallen flowers in vain.
21:Vaguely I seem to know$ the swallows come again.
25:oLoitering on the garden path, I alone remain.

[root@localhost ~]# grep -n -E '[a-zA-Z]' /work6/hxs
5:I compose a new song and drink a cup of wine
9:In the bower of last year when weather is as fine.
13:When will you come back like the sun on the decline?
17:Deeply I sigh for the fallen flowers in vain.
21:Vaguely I seem to know$ the swallows come again.
25:oLoitering on the garden path, I alone remain.

[root@localhost ~]# grep '^[a-zA-Z]' /work6/hxs
I compose a new song and drink a cup of wine
In the bower of last year when weather is as fine.
When will you come back like the sun on the decline?
Deeply I sigh for the fallen flowers in vain.
Vaguely I seem to know$ the swallows come again.
oLoitering on the garden path, I alone remain.

[root@localhost ~]# grep '^[a-z]' /work6/hxs
oLoitering on the garden path, I alone remain.


文件的上传和下载命令

命令:
rz (# 把windows的文件上传到虚拟机里)
命令:
sz 文件名 (# 把虚拟机的文件下载到windows里)
[root@localhost ~]# sz /work6/777.txt 

# 注意:只能虚拟机和物理机之间上传下载
## 外网下载命令
# 安装wget命令
[root@localhost ~]# yum install -y wget

用法:
wget 下载地址
-O(大写):指定下载的位置和文件的名字

举例:
[root@localhost ~]# wget -O /work6/kobe.jpg https://pics6.baidu.com/feed/8c1001e93901213fb79e6bdb686114d72e2e9597.jpeg?token=3ebb97aebcc188b55841e34248d70e56&s=AE624B83C266A6EC544030280300F010

[root@localhost ~]# ll /work6
total 136
-rw-r--r--. 1 root root      2 Mar 23 19:24 1.txt
-rw-r--r--. 1 root root      2 Mar 23 19:24 2.txt
-rw-r--r--. 1 root root      2 Mar 23 19:24 3.txt
-rw-r--r--. 1 root root     27 Mar 23 19:41 777.txt
-rw-r--r--. 1 root root     16 Mar 23 19:33 bbb.txt
-rw-r--r--. 1 root root     13 Mar 23 19:36 ccc.txt
-rw-r--r--. 1 root root    556 Mar 24 19:07 hxs
-rw-r--r--. 1 root root 106199 Jan  4  1970 kobe.jpg

文件查找命令

type -a 只针对系统内置命令

## 查找命令位置的命令 which
which 只能查找命令

# 举例:
[root@localhost ~]# which ll
alias ll='ls -l --color=auto'
	/usr/bin/ls

字符处理命令-排序

# 命令:
sort

## 语法
sort [选项]... File...

## 选项
-t:指定分隔符
-k:指定按照某一列,进行排序
-n:按照阿拉伯数字排序
-r:reverse 倒叙排序

##举例:
[root@localhost ~]# cat /root/paixu1.txt 
B:A:123
C:F:223
D:n:421
J:m:632
K:l:756
M:g:364

[root@localhost ~]# sort /root/paixu1.txt
B:A:123
C:F:223
D:n:421
J:m:632
K:l:756
M:g:364

## 注意:默认按照每一行的第一个字符进行排序,如果字符相同就往后推,字母按照a-z的顺序排序,排序不修改源文件内容

# -t
举例:

[root@localhost ~]# sort -t ':' /root/paixu1.txt
B:A:123
C:F:223
D:n:421
J:m:632
K:l:756
M:g:364

# -k
[root@localhost ~]# sort -t ':' -k 2 /root/paixu1.txt
B:A:123
C:F:223
M:g:364
K:l:756
J:m:632
D:n:421

# -n
[root@localhost ~]# sort -t ':' -k 3 -n /root/paixu1.txt
B:A:123
C:F:223
M:g:364
D:n:421
J:m:632
K:l:756

# -r
[root@localhost ~]# sort -rnt ':' -k 3 /root/paixu1.txt
K:l:756
J:m:632
D:n:421
M:g:364
C:F:223
B:A:123

-k 和 -t 后面带参数,所以组合命令的时候只能放在后面
即:sort -rnt ':' -k 3 /root/paixu1.txt
   sort -nrt ':' -k 3 /root/paixu1.txt

不可以把-t和-k放在前面

思维导图

posted @   悠悠哉55  阅读(50)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
点击右上角即可分享
微信分享提示