Linux小推车Linux基础

Linux Day9: cut/sort/uniq/wc/tr/

2018-10-10  本文已影响30人  泥人吴

cut

-d: 指定字段分隔符,默认空格
-f: 指定要显示的字段
> + -f 1,3:显示1,3字段
> + -f 1-3:显示1-3字段
$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
...

# 如果剪切第一行:
$ cut -d : -f1 /etc/passwd
root
daemon
bin
sys
sync
...

文本排序:sort

 不影响源文件排序,只影响显示排序
 -n:数值排序
 -r :降序
 -t: 字段分隔符
 -k以那个字段为关键字进行排序
 排序后相同的行只显示一次
$ sort -t: -k3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
jmzeng:x:1000:1000:,,,:/home/jmzeng:/bin/bash
systemd-timesync:x:100:102:systemd Time Synchronization,,,:/run/systemd:/bin/false
spguo:x:1001:1003:,,,:/home/spguo:/bin/bash
fzhao:x:1002:1004:,,,:/home/fzhao:/bin/bash
zgxu:x:1003:1005:,,,:/home/zgxu:/bin/bash
myshen:x:1004:1006:,,,:/home/myshen:/bin/bash

uniq:report or omit repeated lines

-d:只显示重复的行
-c: 显示文件中行重复的次数。

文本统计:wc (word count)

$ wc /etc/fstab
  9  54 541 /etc/fstab
$ wc -l /etc/fstab
9 /etc/fstab
$ wc -w /etc/fstab
54 /etc/fstab

字符处理命令:

tr:转换/删除字符

tr [OPTION]... SET1 [SET2]

$ tr 'ab' 'AB'
abc
ABc
able
ABle
acd
Acd

`tr 'a-z' 'A-Z' < /etc/passwd```将/etc/passwd文件中的a-z换成A-Z
-d:删除出现在字符集中的所有字符

$ tr -d 'ab'
helloab
hello
helloba
hello
are
re

友情阅读推荐:

生信技能树公益视频合辑:学习顺序是linux,r,软件安装,geo,小技巧,ngs组学!
请猛戳下面链接
B站链接:https://m.bilibili.com/space/338686099

YouTube链接:https://m.youtube.com/channel/UC67sImqK7V8tSWHMG8azIVA/playlists

生信工程师入门最佳指南:https://mp.weixin.qq.com/s/vaX4ttaLIa19MefD86WfUA

学徒培养:https://mp.weixin.qq.com/s/3jw3_PgZXYd7FomxEMxFmw

上一篇下一篇

猜你喜欢

热点阅读