linux变量、cut_sort_wc_uniq_tee_tr_split 命令使用方法
查看系统变量:
1.env命令
[root@localhost ~]# env
2.set命令
[root@localhost ~]# set
*set可以显示用户自定义的变量
自定义变量:
1.定义变量:
[root@localhost ~]# a=test[root@localhost ~]# echo $atest
2.变量命名规则:可包含大小写字母、数字、下划线(不能以数字开头)
[root@localhost ~]# a=1[root@localhost ~]# echo $a1[root@localhost ~]# a_1=2[root@localhost ~]# echo $a_12[root@localhost ~]# a1=3[root@localhost ~]# echo $a13[root@localhost ~]# 1a=4-bash: 1a=4: 未找到命令
3.变量值含特殊字符($ / \ # 空格等等)时需添加单引号:
[root@localhost ~]# a=abc[root@localhost ~]# echo $aabc[root@localhost ~]# a='a b c'[root@localhost ~]# echo $aa b c[root@localhost ~]# a=a b c-bash: b: 未找到命令
4.变量的累加:当变量值中包含变量名时需使用双引号才能读取变量的值,使用单引号变量名会被识别为字符串
[root@localhost ~]# a=1[root@localhost ~]# b=2[root@localhost ~]# echo $a$b12[root@localhost ~]# c='$a$b'[root@localhost ~]# echo $c$a$b[root@localhost ~]# c="$a$b"[root@localhost ~]# echo $c12
5.全局变量:在当前的shell中(终端)定义的变量只在当前的shell中(终端)生效,使用全局变量可以使变量在当前shell的子shell中也生效,但在子shell中定义的全局变量不会再父shell中生效(全局变量只能在当前shell和当前shell的子shell中生效)
定义全局变量命令:export
[root@localhost ~]# a=test[root@localhost ~]# echo $atest[root@localhost ~]# bash[root@localhost ~]# echo $a[root@localhost ~]# exitexit[root@localhost ~]# export a=test[root@localhost ~]# bash[root@localhost ~]# echo $atest
6.删除变量:unset 变量名
[root@localhost ~]# a=1[root@localhost ~]# echo $a1[root@localhost ~]# unset a
环境变量:
1.变量配置文件:
a.系统层面:/etc/profile、/etc/bashrc
b.用户层面:~/.bash_profile、\~/.bashrc、\~/.bash_history、\~/.bash_logout
*系统层面的配置文件通常在登录时加载,用户层面的配置文件只对单个用户生效
2.PS1变量:表示每行命令行最前端的内容([root@localhost ~]#)
[root@localhost ~]# echo $PS1[\u@\h \W]\$
*u代表用户,h代表hostname,W代表当前所在目录
将大写W改为小写w后显示绝对路径:
[root@localhost ~]#cd /etc/sysconfig/[root@localhost sysconfig]#PS1='[\u@\h \w]\$'[root@localhost /etc/sysconfig]#
3.PS2变量:(在另一种模式中使用,比如登录mysql后)
[root@localhost ~]#echo $PS2>
cut分割命令:
-d 参数:指定分割符号,-f 参数:指定段数,-c 参数:指定第几个字符
[root@localhost ~]#cat 1.txtroot:x:0:0:root:/root:/bin/bash[root@localhost ~]#cat 1.txt |cut -d ":" -f 1root[root@localhost ~]#cat 1.txt |cut -d ":" -f 1,2root:x[root@localhost ~]#cat 1.txt |cut -d ":" -f 1-5root:x:0:0:root[root@localhost ~]#cat 1.txt |cut -c 4t
sort排序命令:将每行的内容以ASCII码从小到大排序
[root@localhost ~]#[root@localhost ~]#cat 1.txt abcaa_1aabbop#test
-n 参数:以数字从小到大排序(字母和特殊符号开头的行会被默认为0)
[root@localhost ~]#cat 1.txt abcaa_1aabbop#test
-r 参数:倒序排序
[root@localhost ~]#sort -n 1.txt aaa_1abbopabc#test
wc统计命令:
-l 参数:统计行数
[root@localhost ~]#cat test.txt abcaa_1aabbop#test
-m 参数:统计字符数
[root@localhost ~]#cat a.txt 20190917[root@localhost ~]#wc -m a.txt 10 a.txt
*cat查看文件内容显示只有8个字符,但wc -m显示10个字符是因为还有隐藏换行符 "$"
[root@localhost ~]#cat -A a.txt 2019$0917$
-w 参数:统计词数(以空格分隔)
[root@localhost ~]#cat a.txt 2019 0917 hello,world test[root@localhost ~]#wc -w a.txt 4 a.txt
uniq去重命令:
uniq只能在相邻的行之间去重,所以uniq一般配合sort使用,先排序再去重:
[root@localhost ~]#cat a.txt 2019 hello worldtesttest112019[root@localhost ~]#uniq a.txt 2019 hello worldtest12019 #该行未被去重
结合sort排序命令使用:
[root@localhost ~]#cat a.txt 2019hello worldtesttest112019[root@localhost ~]#sort a.txt | uniq12019hello worldtest
-c 参数:统计去重次数
[root@localhost ~]#sort a.txt | uniq -c 2 1 2 2019 1 hello world 2 test
tee重定向命令:(与 > 重定向类似,区别在于tee命令在重定向时会打印重定向的内容)
[root@localhost ~]#sort a.txt | uniq -c 2 1 2 2019 1 hello world 2 test[root@localhost ~]#sort a.txt |uniq -c |tee b.txt 2 1 2 2019 1 hello world 2 test[root@localhost ~]#cat b.txt 2 1 2 2019 1 hello world 2 test
-a 参数:追加重定向
[root@localhost ~]#cat b.txt 2 1 2 2019 1 hello world 2 test[root@localhost ~]#sort a.txt |uniq -c |tee -a b.txt 2 1 2 2019 1 hello world 2 test[root@localhost ~]#cat b.txt 2 1 2 2019 1 hello world 2 test 2 1 2 2019 1 hello world 2 test
tr替换命令:(可以替换单个、多个及所以字符)
[root@localhost ~]#echo "hello world"hello world[root@localhost ~]#echo "hello world" |tr 'h' 'H'Hello world[root@localhost ~]#echo "hello world" |tr '[hw]' '[HW]'Hello World[root@localhost ~]#echo "hello world" |tr '[a-z]' '[A-Z]'HELLO WORLD[root@localhost ~]#echo "hello world" |tr '[a-z]' '0'00000 00000
split切割命令:(通常用于切割大日志文件)
-b参数:指定切割大小(不指定单位的情况下默认是字节)
[root@localhost ~]#find /etc/ -type f -exec cat {} > log.txt \;[root@localhost ~]#ls -lh 总用量 27M-rw-r--r--. 1 root root 27M 9月 17 22:44 log.txt[root@localhost ~]#split -b 10M log.txt [root@localhost ~]#du -sh *27M log.txt10M xaa10M xab6.1M xac
切割的同时可以指定文件前缀:
[root@localhost ~]#split -b 10M log.txt testlog.[root@localhost ~]#lslog.txt testlog.aa testlog.ab testlog.ac
-l 参数:按行数切割
[root@localhost ~]#split -l 60000 log.txt [root@localhost ~]#wc -l * 170640 log.txt 60000 xaa 60000 xab 50640 xac 341280 总用量