linux变量、cut_sort_wc_uniq_tee_tr_split 命令使用方法
admin
2023-03-07 21:03:57
0

查看系统变量:
1.env命令

[root@localhost ~]# env

2.set命令

[root@localhost ~]# set

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

自定义变量:

1.定义变量:

[root@localhost ~]# a=test
[root@localhost ~]# echo $a
test

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

[root@localhost ~]# a=1
[root@localhost ~]# echo $a
1
[root@localhost ~]# a_1=2
[root@localhost ~]# echo $a_1
2
[root@localhost ~]# a1=3
[root@localhost ~]# echo $a1
3
[root@localhost ~]# 1a=4
-bash: 1a=4: 未找到命令

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

[root@localhost ~]# a=abc
[root@localhost ~]# echo $a
abc
[root@localhost ~]# a='a b c'
[root@localhost ~]# echo $a
a b c
[root@localhost ~]# a=a b c
-bash: b: 未找到命令

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

[root@localhost ~]# a=1
[root@localhost ~]# b=2
[root@localhost ~]# echo $a$b
12
[root@localhost ~]# c='$a$b'
[root@localhost ~]# echo $c
$a$b
[root@localhost ~]# c="$a$b"
[root@localhost ~]# echo $c
12

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

[root@localhost ~]# a=test
[root@localhost ~]# echo $a
test
[root@localhost ~]# bash
[root@localhost ~]# echo $a

[root@localhost ~]# exit
exit
[root@localhost ~]# export a=test
[root@localhost ~]# bash
[root@localhost ~]# echo $a
test

6.删除变量:unset 变量名

[root@localhost ~]# a=1
[root@localhost ~]# echo $a
1
[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.txt
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]#cat 1.txt |cut -d ":" -f 1
root
[root@localhost ~]#cat 1.txt |cut -d ":" -f 1,2
root:x
[root@localhost ~]#cat 1.txt |cut -d ":" -f 1-5
root:x:0:0:root
[root@localhost ~]#cat 1.txt |cut -c 4
t

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

[root@localhost ~]#
[root@localhost ~]#cat 1.txt 
abc
aa_1
a
abbop
#test

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

[root@localhost ~]#cat 1.txt 
abc
aa_1
a
abbop
#test

-r 参数:倒序排序

[root@localhost ~]#sort -n 1.txt 
a
aa_1
abbop
abc
#test

wc统计命令:

-l 参数:统计行数

[root@localhost ~]#cat test.txt 
abc
aa_1
a
abbop
#test

-m 参数:统计字符数

[root@localhost ~]#cat a.txt 
2019
0917
[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 world
test
test
1
1
2019
[root@localhost ~]#uniq a.txt 
2019 
hello world
test
1
2019   #该行未被去重

结合sort排序命令使用:

[root@localhost ~]#cat a.txt 
2019
hello world
test
test
1
1
2019
[root@localhost ~]#sort a.txt | uniq
1
2019
hello world
test

-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.txt
10M xaa
10M xab
6.1M xac

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

[root@localhost ~]#split -b 10M log.txt testlog.
[root@localhost ~]#ls
log.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 总用量

相关内容

热门资讯

原创 手... 以前购买的手机想要用3天不充电,基本上是不可能的,但这两年手机电池技术有大突破,非重度使用的情况下,...
咪咕数媒亮相第34届全国图书交... 潮新闻客户端 记者 王景平 7月24日,第34届全国图书交易博览会盛大举办。作为数字文化领域的核心央...
王兴兴和他的机甲机器人登上《时... 当地时间7月23日,宇树科技创始人兼首席执行官王兴兴与该公司突破性的载人机甲机器人GD01共同登上美...
APEC论坛聚焦亚太人工智能普... 这是7月23日在成都街头拍摄的会议主题装置。当日,2026年亚太经合组织(APEC)数字和人工智能部...
菲尔兹奖得主王虹4岁不慎烫伤,... 中国数学家王虹、邓煜获得菲尔兹奖,这是中国籍数学家首次获得这一奖项。王虹1991年出生在广西桂林平乐...
美媒:特朗普下令暂停空袭伊朗 美国“Axios”新闻网7月25日援引知情人士报道,特朗普24日指示美军不得对伊朗发动新的打击,打破...
法国野火“创历史纪录”:政坛吵... 综合法媒BFM新闻台、《费加罗报》报道,当地时间7月25日中午,内政部长努内兹(Laurent Nu...
51.79亿,携程真的疼了吗? 2026年7月25日,市场监管总局对携程反垄断案作出行政处罚:罚没款合计51.79亿元。但这个数字被...
一艘越南籍渔船在南沙遇险,29... 新华社三沙7月25日电(记者赵颖全)记者从三沙海上搜救中心获悉,25日傍晚,一艘越南籍渔船在南沙永暑...
成都如何耕好新型工业化“试验田... 7月24日,APEC数字周城市侧配套活动——制造强国建设专家研讨暨新型工业化推进交流会举行,院士专家...