三分薄地,认真耕耘

导航

 

1、sort命令

1.1 作用

以行为单位对文件内容进行排序
也可以根据不同的数据类型来排序

1.2 语法格式


sort [选项] 参数 
cat file | sort 选项 

1.3 常用选项

-f∶ 忽略大小写,会将小写字母都转换为大写字母来进行比较;
-b∶ 忽略每行前面的空格;
-n∶ 按照数字进行排序;
-r∶ 反向排序;
-u∶ 等同于uniq,表示相同的数据仅显示一行;
-t∶ 指定字段分隔符,默认使用 【Tab】键分隔;
-k∶指定排序字段;
-o <输出文件>∶ 将排序后的结果转存至指定文件;

1.4 实际操作

1.4.1 对数字进行排序:

按照第一列数字顺序进行排序,而不是按照数字大小


[root@entos7 shellscript]# cat testfile #原文件
132
23
7
74
84
33
435
6
3
64
34
1
[root@entos7 shellscript]# sort testfile 
# 按照第一列数字进行排序,而不是数字大小
1
132
23
3
33
34
435
6
64
7
74
84

1.4.2 对字母进行排序:

默认是按照首字母进行排序,且小写字母放前面,大小字母放后面


[root@entos7 shellscript]# cat testfile1
aa
abc
Abc
bd
def
connon
banana
erik
Estrack
FLASK
Fast
goal
Gobal
Host
Have
had
[root@entos7 shellscript]# sort testfile1
# 一次依据首列,第二列进行排序,小写字母顺序优先于大写字母
aa
abc
Abc
banana
bd
connon
def
erik
Estrack
Fast
FLASK
goal
Gobal
had
Have
Host

1.4.3 -f参数

-f:忽略大小写,会将小写字母换成大写字母来进行比较


[root@entos7 shellscript]# cat testfile1
aa
abc
Abc
bd
def
connon
banana
erik
Estrack
FLASK
Fast
goal
Gobal
Host
Have
had
[root@entos7 shellscript]# sort -f testfile1
aa
Abc
abc
banana
bd
connon
def
erik
Estrack
Fast
FLASK
goal
Gobal
had
Have
Host

1.4.4 sort -n:

sort -n 按照数字(数值大小)进行排序;sort -r:反向排序


[root@entos7 sort_test]# cat testfile 
132
23
7
74
84
33
435
6
3
64
34
1
[root@entos7 sort_test]# sort -n testfile # 按照数字大小排序
1
3
6
7
23
33
34
64
74
84
132
[root@entos7 sort_test]# sort -nr testfile # 按照数字大小倒序排序
435
132
84
74
64
34
33
23
7
6
3
1
[root@entos7 sort_test]# sort -r testfile # 按照倒序排序
84
74
7
64
6
435
34
33
3
23
132
1

1.4.5 sort -u

sort -u:等同于uniq,表示相同的数据仅显示一行;去除重复


[root@entos7 sort_test]# cat testfile2
aacc
c
aa
dof
dof
lzg
zabbix
comful
elk
zabbix
elk
logrotate
journalctl
elk
abound
journalctl
[root@entos7 sort_test]# sort -u testfile2
aa
aacc
abound
c
comful
dof
elk
journalctl
logrotate
lzg
zabbix

1.4.6 实操

1、按照/etc/passwd内的UID进行从大到小反向排序,并将排序结果输出到123.txt文件中


sort -t ':' -k3 -nr /etc/passwd -o 123.txt
# 两个命令同样效果
cat /etc/passwd | sort -t ':' -k3 -nr -o 123.txt

-t∶ 指定字段分隔符,默认使用 【Tab】键分隔;-k∶指定排序字段;-o <输出文件>∶ 将排序后的结果转存至指定文件


[root@entos7 sort_test]# sort -t ':' -k3 -nr /etc/passwd -o 123.txt
[root@entos7 sort_test]# head -n 20 123.txt
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
yangjie:x:1000:1000:yangjie:/home/yangjie:/bin/bash
ods:x:999:999:softhsm private keys owner:/var/lib/softhsm:/sbin/nologin
polkitd:x:998:997:User for polkitd:/:/sbin/nologin
colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
unbound:x:996:993:Unbound DNS resolver:/etc/unbound:/sbin/nologin
gluster:x:995:992:GlusterFS daemons:/run/gluster:/sbin/nologin
libstoragemgmt:x:994:991:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
saslauth:x:993:76:Saslauthd user:/run/saslauthd:/sbin/nologin
chrony:x:992:987::/var/lib/chrony:/sbin/nologin
geoclue:x:991:985:User for geoclue:/var/lib/geoclue:/sbin/nologin
cockpit-ws:x:990:984:User for cockpit-ws:/:/sbin/nologin
sssd:x:989:983:User for sssd:/:/sbin/nologin
dirsrv:x:988:982:user for 389-ds-base:/usr/share/dirsrv:/sbin/nologin
setroubleshoot:x:987:981::/var/lib/setroubleshoot:/sbin/nologin
saned:x:986:980:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
gnome-initial-setup:x:985:979::/run/gnome-initial-setup/:/sbin/nologin
pcp:x:984:978:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin
kdcproxy:x:983:977:IPA KDC Proxy User:/:/sbin/nologin
ipaapi:x:982:976:IPA Framework User:/:/sbin/nologin

2、对/var/log目录下的文件空间大小进行排序,并将结果输出到文件


[root@entos7 sort_test]# du -a /var/log/ | sort -nr -k1 -o 456.txt
[root@entos7 sort_test]# cat 456.txt
145488	/var/log/
102400	/var/log/logfile_test/log-file-20250402
102400	/var/log/logfile_test
10656	/var/log/messages-20250515
10316	/var/log/messages-20250504
8756	/var/log/sa
7580	/var/log/audit/audit.log
7580	/var/log/audit
3996	/var/log/anaconda
2616	/var/log/anaconda/journal.log
740	/var/log/anaconda/packaging.log
460	/var/log/sa/sa30
460	/var/log/sa/sa29
460	/var/log/sa/sa07
460	/var/log/sa/sa04
460	/var/log/sa/sa02
460	/var/log/sa/sa01
444	/var/log/sa/sa05
428	/var/log/sa/sa26
400	/var/log/anaconda/syslog
364	/var/log/sa/sar30
364	/var/log/sa/sar29
364	/var/log/sa/sar07
364	/var/log/sa/sar04

2、uniq命令

2.1 概述

uniq命令用于检查及并合并文本文件中连续重复出现的行列,一般与sort命令联合使用

2.2 语法格式

uniq [选项] 参数

2.3 选项

-c: 进行计数(剔除文件中重复出现的行)
-d: 仅显示连续的重复行
-u: 仅显示出现一次的行

2.4 实际操作

2.4.1 合并文件中连续重复内容


[root@entos7 shellscript]# cat testfile1
11
11  #连续重复出现的行会被合并
22
22
3334
22
44
566
43
321 #连续重复出现的行会被合并
321
44
334 
221
321 #虽然此行重复,但是非连续出现,因此不会被合并
[root@entos7 shellscript]# uniq testfile1
11
22
3334
22
44
566
43
321
44
334
221
321

2.4.2 uniq结合sort -n使用,根据数值排序并合并重复行

sort -n testfile1 | uniqsort -nu testfile1 一样


[root@entos7 shellscript]# cat testfile1
11
11
22
22
3334
22
44
566
43
321
321
44
334
221
321
[root@entos7 shellscript]# sort -n testfile1 | uniq
# 排序后所有重复项都连续,因此都去重并合并为一行
11
22
43
44
221
321
334
566
3334

2.4.3 uniq -c 对连续出现的重复行合并,然后计数

[root@entos7 shellscript]# cat testfile1
11
11
22
22
3334
22
44
566
43
321
321
44
334
221
321

[root@entos7 shellscript]# sort -n testfile1 | uniq -c
# 排序后再去重统计,所有重复行被合并为一行并计数
      2 11
      3 22
      1 43
      2 44
      1 221
      3 321
      1 334
      1 566
      1 3334
[root@entos7 shellscript]# uniq -c testfile1
# 未排序就去重统计,只有连续出现的重复行会被合并为一行然后计数
      2 11
      2 22
      1 3334
      1 22
      1 44
      1 566
      1 43
      2 321
      1 44
      1 334
      1 221
      1 321

2.4.4 uniq -c的应用

统计登录失败次数大于3次的IP地址,可以用于检测是否有人爆破密码,可加入定时任务,失败登录大于3次,则进行报警并加入黑名单


grep -i 'failed password' /var/log/secure | awk '{print $11}' |sort -nr|uniq -c
     60 172.31.0.11
     51 10.255.30.54
      1 10.255.20.23
     44 10.200.4.1
     69 10.200.0.5
      9 10.200.0.1
      1 10.15.200.9
      3 10.15.200.1
      2 10.15.100.8
      2 10.15.100.5
      2 10.15.100.48
      1 10.15.100.21


2.4.5 uniq -d 仅显示连续的重复行

[root@entos7 shellscript]# cat testfile1
11
11
22
22
3334
22
44
566
43
321
321
44
334
221
321
[root@entos7 shellscript]# uniq -dc testfile1
      2 11
      2 22
      2 321
[root@entos7 shellscript]# sort -n testfile1|uniq -dc
      2 11
      3 22
      2 44
      3 321

2.4.6 uniq -u仅显示未连续出现的行


[root@entos7 shellscript]# cat testfile1
11
11
22
22
3334
22
44
566
43
321
321
44
334
221
321
[root@entos7 shellscript]# uniq -u testfile1
# 仅显示未连续重复出现的行
3334
22
44
566
43
44
334
221
321
[root@entos7 shellscript]# sort -n testfile1 | uniq -u
# 排序后去重仅显示文件中唯一的行
43
221
334
566
3334

3、tr命令

3.1 作用

Linux tr 命令用于转换或删除文件中的字符。

tr 指令从标准输入设备读取数据,经过字符串转译后,将结果输出到标准输出设备。

3.2 语法格式


tr [-cdst][--help][--version][第一字符集][第二字符集]  
tr [OPTION]…SET1[SET2] 


3.3 常用选项

  • -c, --complement:反选设定字符。也就是符合 SET1 的部份不做处理,不符合的剩余部份才进行转换
  • -d, --delete:删除指令字符
  • -s, --squeeze-repeats:缩减连续重复的字符成指定的单个字符
  • -t, --truncate-set1:削减 SET1 指定范围,使之与 SET2 设定长度相等
  • --help:显示程序用法信息
  • --version:显示程序本身的版本信息

3.4 字符集合的范围:

  • \NNN 八进制值的字符 NNN (1 to 3 为八进制值的字符)
  • \ 反斜杠
  • \a Ctrl-G 铃声
  • \b Ctrl-H 退格符
  • \f Ctrl-L 走行换页
  • \n Ctrl-J 新行
  • \r Ctrl-M 回车
  • \t Ctrl-I tab键
  • \v Ctrl-X 水平制表符
  • CHAR1-CHAR2 :字符范围从 CHAR1 到 CHAR2 的指定,范围的指定以 ASCII 码的次序为基础,只能由小到大,不能由大到小。
  • [CHAR*] :这是 SET2 专用的设定,功能是重复指定的字符到与 SET1 相同长度为止
  • [CHAR*REPEAT] :这也是 SET2 专用的设定,功能是重复指定的字符到设定的 REPEAT 次数为止(REPEAT 的数字采 8 进位制计算,以 0 为开始)
  • [:alnum:] :所有字母字符与数字
  • [:alpha:] :所有字母字符
  • [:blank:] :所有水平空格
  • [:cntrl:] :所有控制字符
  • [:digit:] :所有数字
  • [:graph:] :所有可打印的字符(不包含空格符)
  • [:lower:] :所有小写字母
  • [:print:] :所有可打印的字符(包含空格符)
  • [:punct:] :所有标点字符
  • [:space:] :所有水平与垂直空格符
  • [:upper:] :所有大写字母
  • [:xdigit:] :所有 16 进位制的数字
  • [=CHAR=] :所有符合指定的字符(等号里的 CHAR,代表你可自订的字符)

3.5 实际操作

3.5.1 tr 替换字符


[root@entos7 tr_test]# echo '123abcdcba321' |tr 'a-z' 'A-Z'
123ABCDCBA321 # 将set1中的小写字母都替换成了大写字母
[root@entos7 tr_test]# echo '123abcdcba321' |tr 'cb' 'BC'
123aCBdBCa321  # 将set1中的cb字符,替换成了大写,同时将'cb'字符串替换成了BC
[root@entos7 tr_test]# echo '123abcdcba321' |tr 'bc' 'BC'
123aBCdCBa321 # 将set1中的bc字符,替换成了大写,同时将'bc'字符串替换成了CB

3.5.2 tr -c 保留set1中与set2不匹配的字符,其它删除

echo '123abcdcba321' |tr -c '0-9' 'T'
123TTTTTTT321T[root@entos7 tr_test] 
# 将输入中除数字之外的所有字符都替换成T,包括'\n'


3.5.3 tr -d 删除所有set1中的字符


[root@entos7 tr_test]# echo '123abcdcba321' |tr -d [:digit:]
abcdcba
[root@entos7 tr_test]# echo '123abcdcba321' |tr -d [0-9]
abcdcba

3.5.4 tr -s 将重复出现的字符压缩成一个字符。

[root@entos7 tr_test]# echo 'soooooooooooooo haaaaaaaaaaaaaaaaaaaaaaaapppppppppppppppy' | tr -s 'oap'
so hapy
#将set1中的字符全部压缩为1个

[root@entos7 tr_test]# echo 'soooooooooooooo haaaaaaaaaaaaaaaaaaaaaaaapppppppppppppppy' | tr -s 'oap' 'o'
so hoy
# 将set2中的字符全部压缩为1个

删除空行以及压缩空行


[root@entos7 tr_test]# cat testfile #原文件内容
1111


2222
[root@entos7 tr_test]# cat testfile | tr -s '\n' #压缩'多个\n'为一个
1111
2222
[root@entos7 tr_test]# grep -v '^$' testfile 
1111
2222
[root@entos7 tr_test]# uniq -u testfile #压缩连续重复出现的多行为一行
1111
2222
[root@entos7 tr_test]# cat -s testfile #转换多行空行为一行
1111

2222

转换字符示例

# 将"11\n\n\n\n22"转换为11:22
[root@entos7 ~]# echo -e "11\n\n\n\n22"
11



22
[root@entos7 ~]# echo -e "11\n\n\n\n22"|tr -s '\n' ':'|cut -c-5
11:22


3.5.5 tr应用

3.5.5.1 对PATH变量进行逐行输出


[root@entos7 ~]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@entos7 ~]# echo $PATH | tr ':' '\n'
/usr/lib64/qt-3.3/bin
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/root/bin

3.5.5.2 使用tr对数组进行排序

# 使用tr对数组重新排序
[root@entos7 ~]# arr=(10 20 50 40 60 30)
[root@entos7 ~]# echo ${arr[@]}
10 20 50 40 60 30
[root@entos7 ~]# echo ${arr[@]} | tr ' ' '/n'
10/20/50/40/60/30
[root@entos7 ~]# echo ${arr[@]} | tr ' ' '\n'
10
20
50
40
60
30
[root@entos7 ~]# echo ${arr[@]} | tr ' ' '\n' | sort -n|tr '\n' ' '
10 20 30 40 50 60

4、cut命令

4.1 作用

显示行中的指定部分,删除文件中指定字段
cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段写至标准输出。
如果不指定 File 参数,cut 命令将读取标准输入。必须指定 -b、-c 或 -f 标志之一。

4.2 格式

cut [选项] 参数

4.3 常用选项

-f∶ 通过指定哪一个字段进行提取。cut命令使用"TAB"作为默认的字段分隔符。
-d∶ “TAB"是默认的分隔符,使用此选项可以更改为其他的分隔符。
-complement∶ 此选项用于排除所指定的字段。
-output-delimiter∶ 更改输出内容的分隔符。
-b :以字节为单位进行分割。这些字节位置将忽略多字节字符边界,除非也指定了 -n 标志

4.4 实例操作

4.1 cut -d

cut默认以tab作为分隔符,使用此选项可以更改为其他的分隔符;-f 指定对哪一个字段进行提取


[root@entos7 cut_test]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@entos7 cut_test]# echo $PATH|cut -d ':' -f3
/usr/local/bin
[root@entos7 cut_test]# echo $PATH|cut -d ':' -f3-4
/usr/local/bin:/usr/sbin
[root@entos7 cut_test]# cut -d ':' -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp

4.2 --complement: 此选项


[root@entos7 cut_test]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@entos7 cut_test]# echo $PATH | cut -d ':' --complement -f 2
/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

4.3 --output-delimiter:更改输出内容的分隔符


[root@entos7 cut_test]# echo $PATH | cut -d ':' -f 1-4 --output-delimiter=' '
/usr/lib64/qt-3.3/bin /usr/local/sbin /usr/local/bin /usr/sbin

4.4 cut命令三个定位方法

  • 字节(bytes),用选项-b (不常用)
  • 字符(characters),用选项-c
  • 域(fields),用选项-f

参数说明:必须指定分割后的输出单位,即参数b、c、f必须选一

[root@entos7 cut_test]# who #cut默认的分隔符为TAB
root     pts/0        2025-05-25 11:41 (192.168.16.3)
[root@entos7 cut_test]# who |cut -f 1
root     pts/0        2025-05-25 11:41 (192.168.16.3)
[root@entos7 cut_test]# who |cut -d ' ' -f 1 # 更改分隔符为空格,输出
root
[root@entos7 cut_test]# who |cut -b 4 
t
[root@entos7 cut_test]# who |cut -b 4,5,6
t
# 当输出为标准字符(ACSII)时,-b、-c参数输出一致
[root@entos7 cut_test]# who |cut -b 4-10
t     p
[root@entos7 cut_test]# who |cut -c 4-10
t     p


4.4.1 -b和-c参数区别

-c 会以字符为单位,输出正常;而-b只会以字节为单位(Byte)来计算,如果遇到汉字等超过1个字节的字符,就会输出乱码

-n:取消分割多字节字符。仅仅和-b一起使用。如果字符的最后一个字节落在-b的list参数指示的范围之内,该字符将被写出;否则,该字符被排除

-nb等同于-c

[root@datanode01 ~]# cat cut_test.txt
成都
上海
北京
雄衡
兰州
武汉
长沙
昆明
南京
合肥
苏州
[root@datanode01 ~]# cut -b 1 cut_test.txt










[root@datanode01 ~]# cut -n -b 1 cut_test.txt
成
上
北
雄
兰
武
长
昆
南
合
苏
[root@datanode01 ~]# cut -c 1 cut_test.txt
成
上
北
雄
兰
武
长
昆
南
合
苏

5、截取字符的四种方法

方法一:expr substr $字符串 起始位置 截取长度 (起始位置从1开始)

方法二:echo ${字符串:起始位置:截取长度} (起始位置从0开始)

方法三:echo $字符串 | cut -b 起始位置—结束位置 (起始位置从1开始)

方法四:echo $字符串 | cut -c 起始位置—结束位置 (起始位置从1开始)


[root@entos7 substr_test]# echo ${N:1:4} #起始位置从0开始
2b4c
[root@entos7 substr_test]# echo $N |cut -b 1-4 #起始位置从1开始
a2b4
[root@entos7 substr_test]# echo $N |cut -c 1-4 #起始位置从1开始
a2b4
[root@entos7 substr_test]# expr substr $N 1 4 #起始位置从1开始
a2b4

6、split命令

6.1 功能说明

split是linux中的一个使用工具,用于将大文件分割成较小的文件。这在处理较大的文件时特别有用,特别是需要将大文件分成多个部分进行并行处理或者传输时。split默认将大文件分割为每个1000行的小文件

6.2 语法说明


SYNOPSIS
       split [OPTION]... [INPUT [PREFIX]]

option: 这是split命令的选项,用于控制如何分割文件
INPUT:要分割的文件。如果省略,则从标准输入读取
PERFIX:输出文件的前缀,如果省略,则默认为x。

6.3 选项说明


-数字 ,设置要分割的行数
-b, --bytes=SIZE,将文件分割为SIZE大小的文件块。SIZE可以是以下单位之一:K,G,M,T,P,E
-l, --lines=NUMBER,将文件分割为每个文件包含NUMBER行的文件。
-a, --suffix-length=N,使用N个字符后缀来生成输出文件,默认为2个字符
-d,  --numric-suffixes,使用数字后缀而不是字母后缀,这在使用-a选项时特别有用
-t, 设置间隔符
--verbose:在输出文件时,显示进度
--help:显示帮助信息
--vresion:显示版本信息

注意: split不会删除原始文件,只是创建新的分裂文件

6.4 实践操作

6.4.1 split默认分割


[root@entos7 split_paste_test]# seq 2000 > test_num1.txt
[root@entos7 split_paste_test]# tail -n 10 test_num1.txt
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
[root@entos7 split_paste_test]# split test_num1.txt
[root@entos7 split_paste_test]# ls
# 默认分割成了2个1000行的文件xaa和xbb
hosts  test_num1.txt  test_num.txt  xaa  xab 
[root@entos7 split_paste_test]# head -v xa*
==> xaa <==
1
2
3
4
5
6
7
8
9
10

==> xab <==
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010

6.4.2 将大文件分为指定行数的小文件


[root@entos7 split_paste_test]# split -l 500 test_num1.txt
[root@entos7 split_paste_test]# ls
hosts  test_num1.txt  test_num.txt  xaa  xab  xac  xad
[root@entos7 split_paste_test]# head -v -n 5 xa*
==> xaa <==
1
2
3
4
5

==> xab <==
501
502
503
504
505

==> xac <==
1001
1002
1003
1004
1005

==> xad <==
1501
1502
1503
1504
1505

6.4.3 将大文件按指定文件大小分为小文件


[root@entos7 split_paste_test]# yes "test" | head -c 30M > 30MB_file.txt
[root@entos7 split_paste_test]# ll 30MB_file.txt
-rw-r--r-- 1 root root 31457280 May 25 17:34 30MB_file.txt
[root@entos7 split_paste_test]# split -b 5M -a 3 -d 30MB_file.txt 5M_file
# 将大文件-b按5M大小进行区分,并以5M_file为前缀,-d以数字为后缀,-a后缀为3位
[root@entos7 split_paste_test]# ll *M*_file*
-rw-r--r-- 1 root root 31457280 May 25 17:34 30MB_file.txt
-rw-r--r-- 1 root root  5242880 May 25 17:36 5M_file000
-rw-r--r-- 1 root root  5242880 May 25 17:36 5M_file001
-rw-r--r-- 1 root root  5242880 May 25 17:36 5M_file002
-rw-r--r-- 1 root root  5242880 May 25 17:36 5M_file003
-rw-r--r-- 1 root root  5242880 May 25 17:36 5M_file004
-rw-r--r-- 1 root root  5242880 May 25 17:36 5M_file005

6.5 注意事项:

  1. 自定义输出文件的后缀格式

使用 -a 和 -d 选项可以自定义输出文件的后缀格式。但除了数字后缀,你还可以结合其他字符或字符串来创建更有意义的文件名。

例如,如果你希望将文件分割为 part-001, part-002 等格式,你可以这样做:

split -b 1M -a 3 -d largefile.txt 'part-'

  1. 保留原始文件的行完整性

当使用 -b 选项时,split 命令可能会在行的中间进行分割,这可能会导致某些行被分割到两个或多个不同的文件中。如果你希望尽量保持每行的完整性,可以使用 -C 选项。但是请注意,这可能会导致某些文件的大小超过指定的 SIZE。

  1. 过滤和重定向

split 命令经常与其他命令(如 grep, awk, sed 等)结合使用,以便对分割后的文件进行进一步处理。你也可以使用重定向操作符(> 或 >>)将 split 的输出直接写入其他文件或设备。

  1. 合并分割后的文件

一旦你使用 split 命令将文件分割为多个部分,你可能需要再次将它们合并回原始文件。虽然 split 命令本身不提供合并功能,但你可以使用 cat 命令轻松实现这一点。

例如,如果你将 largefile.txt 分割为 fileaa, fileab, fileac 等文件,你可以使用以下命令将它们合并回原始文件:

cat fileaa* > merged_largefile.txt

注意:上述命令假设所有分割后的文件都以 fileaa 开头,并且你想要将它们全部合并。如果你的文件名模式不同,你需要相应地调整命令。

  1. 处理大文件时的注意事项

当处理非常大的文件时(特别是那些超过系统内存或磁盘空间限制的文件),你需要格外小心。确保你的系统有足够的资源来处理这些文件,并考虑使用其他工具或技术(如流式处理或分布式文件系统)来更有效地处理它们。

最后,尽管 split 命令在处理大文件时非常有用,但它并不是唯一的解决方案。根据你的具体需求和环境,可能有其他更适合你的工具或方法。

7、paste命令

paste是一个非常实用的文本工具,它可以将多个文件的内容按行合并在一起。默认情况下,它会将多个文件按照列进行合并,并使用制表符(\t)作为分隔符,如需现将内容合并成一行,再以行粘贴的方式合并,可以用-s参数

7.1 paste语法格式

paste [option] [file]

7.2 选型说明

  • -d, --delimters=LIST: 使用LIST中的字符作为分隔符,而不是制表符。例如,-d ',' 将使用逗号作为分割符。
  • -s, --serial: 将每个文件的行串行化,而不是并行合并。这会将每个文件的所有行合并为一个长行。

7.3 实践操作


[root@entos7 split_paste_test]# head -v -n 12 test-008 test-009
==> test-008 <==
81
82
83

85
86
87

89
90

==> test-009 <==
91
92
93
94
95
96
97
98
99
100
101
102
[root@entos7 split_paste_test]# paste test-008 test-009
# paste在合并时,会以行数最多的文件为准
81	91
82	92
83	93
	94
85	95
86	96
87	97
	98
89	99
90	100
	101
	102

[root@entos7 split_paste_test]# paste -d '|' test-008 test-009
# 使用|作为分隔符
81|91
82|92
83|93
|94
85|95
86|96
87|97
|98
89|99
90|100
|101
|102
[root@entos7 split_paste_test]# paste -d "abc" test-008 test-009
# paste指定多个字符作为分隔符时,每次合并依序使用多个字符中的一个
81a91
82a92
83a93
a94
85a95
86a96
87a97
a98
89a99
90a100
a101
a102
[root@entos7 split_paste_test]# paste -d 'abc' test-006 test-007 test-008 test-009
# paste指定多个字符作为分隔符时,每次合并依序使用多个字符中的一个
61a71b81c91
62a72b82c92
63a73b83c93
64a74bc94
65a75b85c95
66a76b86c96
67a77b87c97
68a78bc98
69a79b89c99
70a80b90c100
abc101
abc102
[root@entos7 split_paste_test]# paste -s test-00*
# -s 将每个文件转换为行,再行串行化,类似倒置
1	2	3	4	5	6	7	8	9	10
11	12	13	14	15	16	17	18	19	20
21	22	23	24	25	26	27	28	29	30
31	32	33	34	35	36	37	38	39	40
41	42	43	44	45	46	47	48	49	50
51	52	53	54	55	56	57	58	59	60
61	62	63	64	65	66	67	68	69	70
71	72	73	74	75	76	77	78	79	80
81	82	83		85	86	87		89	90
91	92	93	94	95	96	97	98	99	100	101	102

8、eval命令

8.1 概述

eval属于shell内建命令,通过连接参数构造命令

使用空格分割每个参数,构造的命令由shell读取和执行

eval命令会对后面的命令执行两边扫描,第一遍扫描后,如果命令中没有变量的间接引用则执行此命令;如果命令中含有间接的变量引用,则将该变量引用替换为变量的值,然后在执行该命令。

主要是每次执行一个shell命令它会先检察一次,看到有$标志就会把值替换一次,然后再执行一遍。eval不会唤起起另一个shell来执行,而是在本身这个shell内多解析一次,所以替换的结果可以保留下来使用。

8.2 用法

8.2.1 先替换变量在执行命令


[root@entos7 eval_test]# echo "hello world" > eval_test
[root@entos7 eval_test]# cat eval_test
hello world
[root@entos7 eval_test]# mytest='cat eval_test'
[root@entos7 eval_test]# eval $mytest
hello world

8.2.2 获取传递给脚本或函数的最后一个参数

Shell 中使用特殊字符$#可以获取传递给脚本或函数的参数个数,使用$n获取参数,n 为数字,$1表示第一个参数,$2表示第二个参数,所以$$#表示最后一个参数。


[root@entos7 eval_test]# cat test.sh
#!/usr/bin/bash

echo \$$#
eval echo \$$#
[root@entos7 eval_test]# chmod +x test.sh
[root@entos7 eval_test]# ./test.sh fristarg lastarg
$2
lastarg

本文主要参考了以下博客,再次特别感谢

shell命令--split
shell脚本之“sort“、“uniq“、“tr“、“cut“、“split“、“paste“以及“eval“命令详解
Shell脚本-paste工具

posted on 2025-05-25 22:13  平复心态  阅读(91)  评论(0)    收藏  举报