linux分割内容单引号,linux变量、cut_sort_wc_uniq_tee_tr_split 命令使用方法

本文详细介绍了Linux系统中环境变量的查看、设置、删除以及全局变量的使用,包括env、set命令,变量命名规则,特殊字符处理,变量累加等。此外,还讲解了PS1和PS2变量,以及cut、sort、wc、uniq、tee、tr、split等实用命令的操作和用法,展示了如何在Linux环境中进行文件管理和数据处理。
摘要由CSDN通过智能技术生成

查看系统变量:

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

404

2018

5

[root@localhost ~]#sort 1.txt

2018

404

5

a

aa_1

abbop

abc

#test

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

[root@localhost ~]#cat 1.txt

abc

aa_1

a

abbop

#test

404

2018

5

[root@localhost ~]#sort -n 1.txt

a

aa_1

abbop

abc

#test

5

404

2018

-r 参数:倒序排序

[root@localhost ~]#sort -n 1.txt

a

aa_1

abbop

abc

#test

5

404

2018

[root@localhost ~]#sort -nr 1.txt

2018

404

5

#test

abc

abbop

aa_1

a

wc统计命令:

-l 参数:统计行数

[root@localhost ~]#cat test.txt

abc

aa_1

a

abbop

#test

404

2018

5

[root@localhost ~]#wc -l test.txt

9 test.txt

-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 总用量

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值