加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux变量、cut_sort_wc_uniq_tee_tr_split 命令使用方法

发布时间:2020-12-14 00:36:35 所属栏目:Linux 来源:网络整理
导读:查看系统变量: 1.env命令 [[email?protected] ~]# env 2.set命令 [[email?protected] ~]# set * set可以显示用户自定义的变量 自定义变量: 1.定义变量: [[email?protected] ~]# a=test[[email?protected] ~]# echo $atest 2.变量命名规则:可包含大小写字
查看系统变量:
1.env命令

[[email?protected] ~]# env

2.set命令

[[email?protected] ~]# set

*set可以显示用户自定义的变量

自定义变量:

1.定义变量:

[[email?protected] ~]# a=test
[[email?protected] ~]# echo $a
test

2.变量命名规则:可包含大小写字母、数字、下划线(不能以数字开头)

[[email?protected] ~]# a=1
[[email?protected] ~]# echo $a
1
[[email?protected] ~]# a_1=2
[[email?protected] ~]# echo $a_1
2
[[email?protected] ~]# a1=3
[[email?protected] ~]# echo $a1
3
[[email?protected] ~]# 1a=4
-bash: 1a=4: 未找到命令

3.变量值含特殊字符($ / # 空格等等)时需添加单引号:

[[email?protected] ~]# a=abc
[[email?protected] ~]# echo $a
abc
[[email?protected] ~]# a=‘a b c‘
[[email?protected] ~]# echo $a
a b c
[[email?protected] ~]# a=a b c
-bash: b: 未找到命令

4.变量的累加:当变量值中包含变量名时需使用双引号才能读取变量的值,使用单引号变量名会被识别为字符串

[[email?protected] ~]# a=1
[[email?protected] ~]# b=2
[[email?protected] ~]# echo $a$b
12
[[email?protected] ~]# c=‘$a$b‘
[[email?protected] ~]# echo $c
$a$b
[[email?protected] ~]# c="$a$b"
[[email?protected] ~]# echo $c
12

5.全局变量:在当前的shell中(终端)定义的变量只在当前的shell中(终端)生效,使用全局变量可以使变量在当前shell的子shell中也生效,但在子shell中定义的全局变量不会再父shell中生效(全局变量只能在当前shell和当前shell的子shell中生效
定义全局变量命令:export

[[email?protected] ~]# a=test
[[email?protected] ~]# echo $a
test
[[email?protected] ~]# bash
[[email?protected] ~]# echo $a

[[email?protected] ~]# exit
exit
[[email?protected] ~]# export a=test
[[email?protected] ~]# bash
[[email?protected] ~]# echo $a
test

6.删除变量:unset 变量名

[[email?protected] ~]# a=1
[[email?protected] ~]# echo $a
1
[[email?protected] ~]# unset a

环境变量:

1.变量配置文件:

a.系统层面:/etc/profile、/etc/bashrc
b.用户层面:~/.bash_profile、~/.bashrc、~/.bash_history、~/.bash_logout
*系统层面的配置文件通常在登录时加载,用户层面的配置文件只对单个用户生效

2.PS1变量:表示每行命令行最前端的内容([[email?protected] ~]#)

[[email?protected] ~]# echo $PS1
[[email?protected]h W]$

*u代表用户,h代表hostname,W代表当前所在目录
将大写W改为小写w后显示绝对路径:

[[email?protected] ~]#cd /etc/sysconfig/
[[email?protected] sysconfig]#PS1=‘[[email?protected]h w]$‘
[[email?protected] /etc/sysconfig]#

3.PS2变量:(在另一种模式中使用,比如登录mysql后)

[[email?protected] ~]#echo $PS2
>

cut分割命令:

-d 参数:指定分割符号,-f 参数:指定段数,-c 参数:指定第几个字符

[[email?protected] ~]#cat 1.txt
root:x:0:0:root:/root:/bin/bash
[[email?protected] ~]#cat 1.txt |cut -d ":" -f 1
root
[[email?protected] ~]#cat 1.txt |cut -d ":" -f 1,2
root:x
[[email?protected] ~]#cat 1.txt |cut -d ":" -f 1-5
root:x:0:0:root
[[email?protected] ~]#cat 1.txt |cut -c 4
t

sort排序命令:将每行的内容以ASCII码从小到大排序

[[email?protected] ~]#
[[email?protected] ~]#cat 1.txt 
abc
aa_1
a
abbop
#test
<zxcv
404
2018
5
[[email?protected] ~]#sort 1.txt 
2018
404
5
a
aa_1
abbop
abc
#test
<zxcv

-n 参数:以数字从小到大排序(字母和特殊符号开头的行会被默认为0)

[[email?protected] ~]#cat 1.txt 
abc
aa_1
a
abbop
#test
<zxcv
404
2018
5
[[email?protected] ~]#sort -n 1.txt 
a
aa_1
abbop
abc
#test
<zxcv
5
404
2018

-r 参数:倒序排序

[[email?protected] ~]#sort -n 1.txt 
a
aa_1
abbop
abc
#test
<zxcv
5
404
2018
[[email?protected] ~]#sort -nr 1.txt 
2018
404
5
<zxcv
#test
abc
abbop
aa_1
a

wc统计命令:

-l 参数:统计行数

[[email?protected] ~]#cat test.txt 
abc
aa_1
a
abbop
#test
<zxcv
404
2018
5
[[email?protected] ~]#wc -l test.txt 
9 test.txt

-m 参数:统计字符数

[[email?protected] ~]#cat a.txt 
2019
0917
[[email?protected] ~]#wc -m a.txt 
10 a.txt

*cat查看文件内容显示只有8个字符,但wc -m显示10个字符是因为还有隐藏换行符 "$"

[[email?protected] ~]#cat -A a.txt 
2019$
0917$

-w 参数:统计词数(以空格分隔)

[[email?protected] ~]#cat a.txt 
2019 
0917 hello,world test
[[email?protected] ~]#wc -w a.txt 
4 a.txt

uniq去重命令:

uniq只能在相邻的行之间去重,所以uniq一般配合sort使用,先排序再去重:

[[email?protected] ~]#cat a.txt 
2019 
hello world
test
test
1
1
2019
[[email?protected] ~]#uniq a.txt 
2019 
hello world
test
1
2019   #该行未被去重

结合sort排序命令使用:

[[email?protected] ~]#cat a.txt 
2019
hello world
test
test
1
1
2019
[[email?protected] ~]#sort a.txt | uniq
1
2019
hello world
test

-c 参数:统计去重次数

[[email?protected] ~]#sort a.txt | uniq -c
      2 1
      2 2019
      1 hello world
      2 test

tee重定向命令:(与 > 重定向类似,区别在于tee命令在重定向时会打印重定向的内容)

[[email?protected] ~]#sort a.txt | uniq -c 
      2 1
      2 2019
      1 hello world
      2 test
[[email?protected] ~]#sort a.txt |uniq -c |tee b.txt
      2 1
      2 2019
      1 hello world
      2 test
[[email?protected] ~]#cat b.txt 
      2 1
      2 2019
      1 hello world
      2 test

-a 参数:追加重定向

[[email?protected] ~]#cat b.txt 
      2 1
      2 2019
      1 hello world
      2 test
[[email?protected] ~]#sort a.txt |uniq -c |tee -a b.txt 
      2 1
      2 2019
      1 hello world
      2 test
[[email?protected] ~]#cat b.txt 
      2 1
      2 2019
      1 hello world
      2 test
      2 1
      2 2019
      1 hello world
      2 test

tr替换命令:(可以替换单个、多个及所以字符)

[[email?protected] ~]#echo "hello world"
hello world
[[email?protected] ~]#echo "hello world" |tr ‘h‘ ‘H‘
Hello world
[[email?protected] ~]#echo "hello world" |tr ‘[hw]‘ ‘[HW]‘
Hello World
[[email?protected] ~]#echo "hello world" |tr ‘[a-z]‘ ‘[A-Z]‘
HELLO WORLD
[[email?protected] ~]#echo "hello world" |tr ‘[a-z]‘ ‘0‘
00000 00000

split切割命令:(通常用于切割大日志文件)

-b参数:指定切割大小(不指定单位的情况下默认是字节)

[[email?protected] ~]#find /etc/ -type f -exec cat {} > log.txt ;
[[email?protected] ~]#ls -lh 
总用量 27M
-rw-r--r--. 1 root root 27M 9月  17 22:44 log.txt
[[email?protected] ~]#split -b 10M log.txt 
[[email?protected] ~]#du -sh *
27M log.txt
10M xaa
10M xab
6.1M xac

切割的同时可以指定文件前缀:

[[email?protected] ~]#split -b 10M log.txt testlog.
[[email?protected] ~]#ls
log.txt  testlog.aa  testlog.ab  testlog.ac

-l 参数:按行数切割

[[email?protected] ~]#split -l 60000 log.txt 
[[email?protected] ~]#wc -l *
  170640 log.txt
   60000 xaa
   60000 xab
   50640 xac
  341280 总用量

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读