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 '[A-Z]' list

Lines from list containing a capital letter

grep '[0-9]' data Lines from data containing a number
grep '[A-Z]...[0-9]' list

Lines from list containing five-character patterns that start with a capital letter and end with a digit

grep '\.pic$' filelist Lines from filelist that end in .pic

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为分隔符

posted @ 2007-11-04 23:01  中土  阅读(2208)  评论(0编辑  收藏  举报
©2005-2008 Suprasoft Inc., All right reserved.