Linux shell命令(2) - cat/cut/paste/sed/tr/grep/sort/uniq
who
- all users currently logged in to the system
cat
- examine the contents of a file
wc -l
wc -w
wc -c
- get a count of the total number of lines, words, and characters of information contained in a file
cut
cut -cchars file
$ who
root console Feb 24 08:54
steve tty02 Feb 24 12:55
george tty08 Feb 24 09:15
dawn tty10 Feb 24 15:55
$ who | cut -c1-8 Extract the first 8 characters
root
steve
george
dawn
$
$ who | cut -c1-8,18-
root Feb 24 08:54
steve Feb 24 12:55
george Feb 24 09:15
dawn Feb 24 15:55
$
-d and -f options
以-d指定分界符将参数分成多个field,-f指定哪几个field
不指定分界符,则以tab为分界符
$ cat /etc/passwd
root:*:0:0:The Super User:/:/usr/bin/ksh
cron:*:1:1:Cron Daemon for periodic tasks:/:
bin:*:3:3:The owner of system files:/:
uucp:*:5:5::/usr/spool/uucp:/usr/lib/uucp/uucico
asg:*:6:6:The Owner of Assignable Devices:/:
steve:*:203:100::/users/steve:/usr/bin/ksh
other:*:4:4:Needed by secure program:/:
$ cut -d: -f1 /etc/passwd Extract field 1
root
cron
bin
uucp
asg
steve
other
$ cut -d: -f1,6 /etc/passwd Extract fields 1 and 6
root:/
cron:/
bin:/
uucp:/usr/spool/uucp
asg:/
steve:/users/steve
other:/
$
grep
grep pattern files 搜索模式串
*, ?都可运用到模式串和文件名
正则表达式例子:
grep –i 忽略匹配大小写
grep -v 找出不匹配模式串的行
grep -l 仅列出包含模式串的文件
grep -n 额外给出匹配的行号
sort
排序
sort -u 去除重复项
sort -r 逆向排序
sort -o 重定向到文件 sort names -o sorted_names 等价于 sort names -o > sorted_names
sort -n 算术排序,而非字符排序
sort +1n 跳过第1个field,然后算术排序
sort -t 指定分界符来分隔field,通常sort +1n这类都默认以space或tab为分隔符