您的位置:首页 > 运维架构 > Shell

Linux shell命令(2) - cat/cut/paste/sed/tr/grep/sort/uniq

2007-11-04 23:01 726 查看
who

alluserscurrentlyloggedintothesystem
cat

examinethecontentsofafile
wc-l

wc-w

wc-c

getacountofthetotalnumberoflines,words,andcharactersofinformationcontainedinafile

cut

cut-ccharsfile

$who
rootconsoleFeb2408:54
stevetty02Feb2412:55
georgetty08Feb2409:15
dawntty10Feb2415:55
$who|cut-c1-8Extractthefirst8characters
root
steve
george
dawn
$


$who|cut-c1-8,18-
rootFeb2408:54
steveFeb2412:55
georgeFeb2409:15
dawnFeb2415:55
$

-dand-foptions

以-d指定分界符将参数分成多个field,-f指定哪几个field

不指定分界符,则以tab为分界符

$cat/etc/passwd
root:*:0:0:TheSuperUser:/:/usr/bin/ksh
cron:*:1:1:CronDaemonforperiodictasks:/:
bin:*:3:3:Theownerofsystemfiles:/:
uucp:*:5:5::/usr/spool/uucp:/usr/lib/uucp/uucico
asg:*:6:6:TheOwnerofAssignableDevices:/:
steve:*:203:100::/users/steve:/usr/bin/ksh
other:*:4:4:Neededbysecureprogram:/:
$cut-d:-f1/etc/passwdExtractfield1
root
cron
bin
uucp
asg
steve
other
$cut-d:-f1,6/etc/passwdExtractfields1and6
root:/
cron:/
bin:/
uucp:/usr/spool/uucp
asg:/
steve:/users/steve
other:/
$


grep

greppatternfiles搜索模式串

*,?都可运用到模式串和文件名

正则表达式例子:

grep'[A-Z]'listLinesfromlistcontainingacapitalletter

grep'[0-9]'dataLinesfromdatacontaininganumber
grep'[A-Z]...[0-9]'listLinesfromlistcontainingfive-characterpatternsthatstartwithacapitalletterandendwithadigit

grep'\.pic$'filelistLinesfromfilelistthatendin.pic
grep–i忽略匹配大小写

grep-v找出不匹配模式串的行

grep-l仅列出包含模式串的文件

grep-n额外给出匹配的行号

sort

排序

sort-u去除重复项

sort-r逆向排序

sort-o重定向到文件sortnames-osorted_names等价于sortnames-o>sorted_names

sort-n算术排序,而非字符排序

sort+1n跳过第1个field,然后算术排序

sort-t指定分界符来分隔field,通常sort+1n这类都默认以space或tab为分隔符
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐