linux echo配合管道符,特殊符号、管道符命令:cut、sort、uniq、wc、tee、tr、split等命令(示例代码)...

特殊符号

*任意个任意字符

?任意一个字符

#注释字符

\脱义字符

|管道符

$变量前缀, !$组合,正则里面表示行尾

;多条命令写到一行,用分号分隔

~用户家目录,后面正则表达式中表示匹配符

&放到命令后面,会把命令丢到后台

> >> 2> 2>> &>:输出重定向、追加重定向、错误输出重定向、错误追加重定向、不区分重定向;

【】方括号表示指定字符中的一个,【0-9】,【a-zA-Z】,【abc】;

||和&& 判断语句,用于命令之间;(|| 或的意思 ls 1.txt || ls 2.txt 如果执行ls 1.txt成功 就不会执行ls 2.txt)

(&& 是and的意思 ls 1.txt && ls 2.txt 表示 如果ls 1.txt 执行成功 才会执行 ls 2.txt)

实验1:||符号;

当ls 1.txt这个条件生效,那么就忽略ls 2.txt这个命令;

当ls 3.txt 这个条件不生效,那么执行后面ls 2.txt这条命令;

[[email protected] test]# ls

1.txt 2.txt a.txt

[[email protected] test]# ls 1.txt || ls 2.txt

1.txt

[[email protected] test]# ls 3.txt || ls 2.txt

ls: 无法访问3.txt: 没有那个文件或目录

2.txt

[[email protected] test]#

实验2:&&and命令;

当第一条命令错误,全部命令失效;

当第一条命令成功,再来执行第二条命令;

[[email protected] test]# ls 3.txt && ls 2.txt

ls: 无法访问3.txt: 没有那个文件或目录

[[email protected] test]# ls 1.txt && ls 2.txt

1.txt

2.txt

[[email protected] test]#

管道符命令

cut命令:

作用:截取文件部分显示,-d分隔符 -f指定段数 -c指定第几个字符;

显示passwd文档的前两段,截取:之前的1段;

cat /etc/passwd |head -2 |cut -d ":" -f 1

[[email protected] ~]# cat /etc/passwd |head -2

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

[[email protected] ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1

root

bin

[[email protected] ~]#

显示passwd文档前两段,截取:之前的1到2段显示出来;

cat /etc/passwd |head -2 |cut -d ":" -f 1,2

[[email protected] test]# cat /etc/passwd |head -2 |cut -d ":" -f 1,2

root:x

bin:x

[[email protected] test]#

显示passwd文档 前两段 截取:之前的 1-3段

cat /etc/passwd |head -2 |cut -d ":" -f 1-3

[[email protected] test]# cat /etc/passwd |head -2 |cut -d ":" -f 1-3

root:x:0

bin:x:1

[[email protected] test]#

sort命令:

作用:排序显示;

-n:以数字排序 ;

-r:反序;

-t: 分隔符;

-kn1/-kn1,n2:指定范围;

将psswd 按ACISS编码排序从a-z顺序;

sort /etc/passwd

[[email protected] ~]# sort /etc/passwd

adm:x:3:4:adm:/var/adm:/sbin/nologin

bin:x:1:1:bin:/bin:/sbin/nologin

chrony:x:998:996::/var/lib/chrony:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

dbus:x:81:81:System message bus:/:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

halt:x:7:0:halt:/sbin:/sbin/halt

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

nobody:x:99:99:Nobody:/:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

polkitd:x:999:997:User for polkitd:/:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

root:x:0:0:root:/root:/bin/bash

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

sync:x:5:0:sync:/sbin:/bin/sync

systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin

user005:x:1008:1005::/home/user005:/bin/bash

user007:x:1007:1005::/home/user007:/bin/bash

user01:x:1000:1000::/home/user01:/bin/bash

user02:x:1001:1001::/home/user02:/bin/bash

[[email protected] ~]#

wc命令

作用:统计文件行数、字数;

-l: 统计文件行数;

-m:统计字符数(换行符也会计算在内)

-w:统计词 (空白作为分隔)

统计/etc/passwd 有多少行

wc -l /etc/passwd

[[email protected] ~]# wc -l /etc/passwd

23 /etc/passwd

[[email protected] ~]#

uniq命令

作用:去重复(去重复内容只限于相邻段),配合sort排序然后再去重复,-c 统计行数;

实验1:不排序去重复;

[[email protected] abc]# cat 2.txt

abc

222

abc

111

111

[[email protected] abc]# uniq 2.txt

abc

222

abc

111

[[email protected] abc]#

实验2:排序去重复;

[[email protected] abc]# cat 2.txt

abc

222

abc

111

111

[[email protected] abc]# sort 2.txt | uniq

111

222

abc

[[email protected] abc]#

tee命令

作用:相当于>命令,也就是输出重定向,但是可以显示;

参数-a:追加;

比如:cat 2.txt >a.txt 是不显示将2.txt文件输出到a.txt;

[[email protected] abc]# cat 2.txt > a.txt

[[email protected] abc]# cat a.txt

abc

222

abc

111

111

[[email protected] abc]#

实验1:使用tee命令,输出到a.txt;

cat 2.txt | tee a.txt

[[email protected] abc]# cat 2.txt | tee a.txt

abc

222

abc

111

111

[[email protected] abc]# cat a.txt

abc

222

abc

111

111

[[email protected] abc]#

实验2:追加到a.txt文件中;

[[email protected] abc]# cat a.txt

abc

222

abc

111

111

[[email protected] abc]# cat 2.txt | tee -a a.txt

abc

222

abc

111

111

[[email protected] abc]# cat a.txt

abc

222

abc

111

111

abc

222

abc

111

111

[[email protected] abc]#

tr命令

作用:替换命令;

实验:将输出的字符串abclinux中的abc替换为123并输出;

[[email protected] test]# echo "abclinux"

abclinux

[[email protected] test]# echo "abclinux" | tr ‘abc‘ ‘123‘

123linux

[[email protected] test]#

split命令

作用:切割命令,将一个大的文件分割成多个文件;-b大小(默认单位字节),-l 行数;

实验1:按大小来分割文件;

split -b 100k a.txt

[[email protected] test]# ls

a.txt xaa xab

[[email protected] test]# wc -l a.txt

6359 a.txt

[[email protected] test]# rm -rf x*

[[email protected] test]# ls

a.txt

[[email protected] test]# split -b 100k a.txt

[[email protected] test]# ls

a.txt xaa xab xac

[[email protected] test]# du -sh *

244K a.txt

100K xaa

100K xab

44K xac

[[email protected] test]#

实验2:按行数来分割文件;

split -l 1000 a.txt

[[email protected] test]# ls

a.txt

[[email protected] test]# wc -l a.txt

6359 a.txt

[[email protected] test]# split -l 1000 a.txt

[[email protected] test]# ls

a.txt xaa xab xac xad xae xaf xag

[[email protected] test]# wc -l *

6359 a.txt

1000 xaa

1000 xab

1000 xac

1000 xad

1000 xae

1000 xaf

359 xag

12718 总用量

[[email protected] test]#

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值