特殊符号、管道符命令: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]hu-test 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]#

原文地址:http://blog.51cto.com/shuzonglu/2060451

时间: 2024-08-03 08:50:23

特殊符号、管道符命令:cut、sort、uniq、wc、tee、tr、split等命令的相关文章

Linux管线命令 - cut,grep,sort,uniq,wc,tee,tr,col,join,paste,expand,split,xargs

在每个管线后面接的第一个数据必定是『命令』喔!而且这个命令必须要能够接受 standard input 的数据才行,这样的命令才可以是为『管线命令』,例如 less, more, head, tail 等都是可以接受 standard input 的管线命令啦.至于例如 ls, cp, mv 等就不是管线命令了!因为 ls, cp, mv 并不会接受来自 stdin 的数据. 也就是说,管线命令主要有两个比较需要注意的地方: 管线命令仅会处理 standard output,对于 standar

0219自学Linux_bash特性+命令学习(cut,sort,uniq,wc,tr,histroy,alias)+通配符glob

09 GPL,BSD,Apache三个开源协定的大体联系及其区别 1.自由软件,版权描述:但是照样是有版权的 2.开源协定,版权描述 www.kernel.org内核版本的版本号查看网址,也是官网 查看最新kernel的最新版本,www.kernel.org习惯了解 列出linux发行版和linux内核的关系 Lniux发行版,GUN:GUN/Linux. 源代码:必须要编译才可以运行,所以发行版是已经将源代码已经编译完成的东西,组合在一起,就形成了发行版,主流的三大发行版:Fedora:它为r

linux cat,tac,more,less,head,tail,cut,sort,uniq,wc,tr命令的使用

cat:连接并显示,比如: [[email protected] ~]# cat /tmp/sort.test  111 324 567 324 890 890 567 abc 加上选项-n会显示行号: [[email protected] ~]# cat -n /tmp/sort.test      1111      2324      3567      4324      5890      6890      7567      8abc 加上-E选项会显示行尾符$:(linux中行尾

linux常用命令-文本处理cut,sort,uniq,wc,tr

cut:截取文本特定字段 NAME       cut - remove sections from each line of files -d, --delimiter=DELIM(指定字段分隔符,默认是空格) use DELIM instead of TAB for field delimiter -f, --fields=LIST(指定要显示的字段) select  only  these  fields;  also print any line that contains no del

cut,sort,uniq,wc

cut cut参数详解 选项 注释 -b 以字节为单位进行分割. -c 以字符为单位进行分割. -d 自定义分隔符,默认为制表符. -f 与-d一起使用,指定显示哪个区域. -n 取消分割多字节字符.仅和 -b 标志一起使用. 注解: cut使用三种定位方式:b(bytes),c(characters),d(fields) cut只擅长处理"以一个字符间隔"的文本内容. sort sort将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将

65.shell特殊符号与和cut,sort,wc,uniq,tee,tr,split命令

liunx的特殊符号 代表字母或者数字 多个 ? 任意一个字符"#" 注释\ 脱义字符| 管道符 1.* 代表任意个任意字符或者数字 [[email protected] /]# ls *.txt1.txt[[email protected] /]# 2.?任意一个字符 [[email protected] /]# ls ?.txt1.txt[[email protected] /]# 3.注释 [[email protected] /]# #11111[[email protecte

Linux CentOS 7 shell中的特殊字符及与管道相关的命令(cut,sort,wc,uniq,tee,tr,split)

一. shell特殊符号cut命令 1.特殊符号 * :任意个任意字符 ? :任意单个字符# :注释\ :转义字符 | :管道符 2.几个和管道相关的命令 (1) cut cut 把文件分段 cat /etc/passwd cut -d: -f 3  /etc/passwd    cut -d: -f 3,6,5  /etc/passwd cut -d: -f 3-6  /etc/passwd cut -c 10 /etc/passwd   取第十个字符 cut -c 5-10 /etc/pas

文本文件命令(wc,cut,sort,uniq)及常用参数

wc 字数统计 wc [OPTION]... [FILE]... -l, --lines 显示行数 -w, --words 显示单词数 -c, --bytes 显示字节数 -L, --max-line-length 打印最长行的长度. eg: cut 文件提取命令 官方解释:remove sections from each line of files 从文件中每一行选取部分 根据官方解释可以知道cut是以每一行为处理对象的 用法:cut OPTION... [FILE]... 常用参数: (其

文本处理工具 wc cut sort uniq

<1> wc 统计字符 行 单词 wc -l wc -w wc -c wc可以可以放在文件前面 也可以放在文件后面,如上图. <2> cut 切 顾名思义就是切割文件用的 作用:是切割一任意行文件的列.... 但是默认按照空格来分割的个格列的 当然可以使用 cut -d 来指定分隔符 如 cut -d: cat -d"22",但是其默认参数必须要跟上-f 指定输出第几列,不指定就会报错 常用的也就-d -f 但是还有一个是以前没用用到过的,就是指定输出分隔符 -

cut,sort,wc,uniq,tee,tr,split,并且,和,或者

cut 把一个文件分段 cut -d:(指定分割符) -f(第几段) 3,4,5 /etc/passwd cut -c(截取第几个字符) 1-10 /etc/passwd sort 用来排序 sort -t:(指定分割符) -k3(第几段) -n(纯数字排序) /etc/passwd sort -t:(指定分割符) -k3,5(区间段用,号,不能用-) -n(纯数字排序) -r(反序排序) -u(去重复) /etc/passwd wc -l(文档有多少行数) 1.txt 2.txt wc -w(