一、sort
Usage: sort [OPTION]... [FILE]...
-o 輸出檔案
-d 按字典順序排序
-n 按資料大小輸出
-r 按逆序輸出排序結果
-k 指定分類是域上的數字分類
-t 域分隔符,用非空格或tab分隔域
sort -k3 -n -r -t: /etc/passwd
sort -d /etc/passwd
二、WC
Usage: wc [OPTION]... [FILE]...
-c 字元數量~
-l 行數~
-w 統計單詞數量~
wc /etc/passwd
36 65 1739/etc/passwd #36行,65個單詞(空格區分),1739個字元
三、diff
diff
Usage: diff [OPTION]... FILES
Compare files line by line.
-q 顯示有無差異,不顯示詳細的資訊~
-c 顯示全部内文,并标出不同之處~
-b 不檢查空格字元的不同~
-B 不檢查空白行
-r 比較子目錄中的檔案~
diff /etc/passwd./passwd.bak
2d1
< bin:x:1:1:bin:/bin:/sbin/nologin
四、grep
Usage: grep [OPTION]... PATTERN [FILE] ...
Search for PATTERN in each FILE or standard input.
-c 隻列印比對的行編号數
-i 比對文本時忽略大小寫
-n 在每行前顯示其行編号
-v 逆向輸出,列印不比對的行
-f file 要比對的字元在檔案清單中
cat /etc/passwd |grep -n root
1:root:x:0:0:root:/root:/bin/bash
12:operator:x:11:0:operator:/root:/sbin/nologin
#grep '[Tt]his' file.txt
#grep '^[^#]' file.txt
比對任意字元
grep 'r..t' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
五、sed
sed
Usage: sed [OPTION]... {script-only-if-no-other-script}[input-file]...
S 替代操作
i 插入指令
a 附加指令
d 删除全部比對行
D 删除首次比對的行
#sed -n '1,4p' /etc/passwd列印1~4行,-n --quiet以免先列印出passwd的全部内容
#sed '/80/D' file.txt
#sed 's/var/usr/g' file.txt 替換file.txt中全部var為usr
#sed '50,$s/help/man/g' file.txt 從50~最後行替換help為man
sed '/done/d' xj_user_p.log 删除done
六、awk
Usage: awk [POSIX or GNU style options] -f progfile [--] file...
Usage: awk [POSIX or GNU style options] [--] 'program' file...
gawk '{ sum += $1 }; END { print sum }' file
gawk -F: '{ print $1 }' /etc/passwd
NF 目前記錄中的字段數。NR 目前記錄數。
awk -F: '{print NR,$1,$NF}' ./passwd.bak
awk -F: 'NR==5{print NR,$0}' ./passwd.bak 列印出5,15,25...行
5 sync:x:5:0:sync:/sbin:/bin/sync
15 nobody:x:99:99:Nobody:/:/sbin/nologin
25 apache:x:48:48:Apache:/var/www:/sbin/nologin
七、uniq
如果要在檔案中查找重複的行,uniq指令會很有用,該指令一般格式為:uniq in_fileout_file
該格式中,uniq把in_file複制到out_file,處理過程中,去掉其中的重複行,uniq對重複行的定義是完全比對的連續行。如果不指定out_file,結果就寫入标準輸出。 -d選項:它告訴uniq把檔案中的重複行寫入out_file,不管它們在檔案中連續出現多少次,這樣的連續行隻寫一次 -c選項:帶-c選項後,每行最前面顯示該行在輸入檔案中出現的次數。
$ more test.txt
aaa
ccc
ccc
ccc
ddd
bbb
eee
123
$ uniq test.txt
aaa
ccc
ddd
bbb
eee
123
$ uniq -d test.txt
ccc
$ uniq -c test.txt
1 aaa
3 ccc
1 ddd
1 bbb
1 eee
1 123
$
綜合示例:
...
資料處理:
找出上海股票漲幅最大的股票?
sort -n -r -k4 t.txt | sed -n '1p'
漲幅>3的股票?
awk '{if ($1>3) print $0} ' t.txt
漲幅在在4~15之間的股票
awk'{if($4>0&&$4<15){print$0}}' t.txt
轉自:http://hi.baidu.com/��������/blog/item/caf21ba89c54ada1ca130c4a.html
http://hi.baidu.com/edeed/blog/item/b1be513db16db5ed3c6d9727.html