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

來源:互聯網
上載者:User

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  搜尋模式串

*, ?都可運用到模式串和檔案名稱

Regex例子:

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為分隔字元

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.