1. 程式人生 > >【unix學習】檔案處理2—檔案sort、cut命令

【unix學習】檔案處理2—檔案sort、cut命令

檔案sort、cut命令

檔案排序-sort

sort

Ordering a set of items according to some criteria

options

b Ignore leading blanks
f Consider lowercases and uppercase letters to be equivalent
t field separator
r Sort in reverse order
k Specify a field as the sort key
n Compare according to string numerical value
Notice the LANG!

  1. 對某一列排序
    預設是以空格劃分
sort -k index file 

index 為檔案中的列
把該列當作字元來比較
2. 把該列當作數字來比較
-n

sort -nk -4 file 

3.逆序
-r

sort -rnk -4 file 

4.欄位分隔符
指定分隔符 -t

[s14516@gdufs]$sort -t: -nk4 /tmp/student_record

對於排序,要注意檔案的語言
echo $LANG

cut 定位符

1.預設分隔符為定位符

cut -f1 a.txt

f1 為分隔後的第一個位置的值
2. 指定欄位分隔符

cut -d" " -f1 a.txt
[[email protected]]$cut -d":" -f1 /tmp/donors

3.擷取指定位置的字元

cut -c11-19 f a.txt