文件分割合并排序
文件分割合并排序
文件分割合并排序
Python代码
# output sorted result
sort -o result.out video.txt
# split the fields by ':'
sort -t: -r video.txt
# test whether it has been sorted
sort -c video.txt
# sort by 2nd field
sort -t: +1 video.txt
# sort 3rd field using ascii order
sort -t: +2 video.txt
sort -t: -k3 video.txt
# sort 3rd field using number order
sort -t: +2n video.txt
sort -t: -k3n video.txt
# uniq
sort -u video.txt
# sort 4th field, then sort 1st field
sort -t: -k4 -k1 video.txt
# sort +field_number.characters_in
# sort 2nd filed, begining with 3rd character
sort -t: +1.2 video.txt
# list all unix users
cat /etc/passwd | sort -t: +0 | awk -F":" '{print $1}'
# only not duplicate. here, sort is recommendable
sort video.txt | uniq -u
# only duplicate
sort video.txt | uniq -d
# count dulicate times
sort video.txt | uniq -c
# join, the files must have a common content; the fields must be splited by single tab or space
join [options] input-file1 input-file2
-an n, the number of file. -a1, means joining files based on file 1
-o n.m n, the number of file; m, the number of field. -o 1.3, means display field 3 of file 1
-jn m n, the number of file; m, the number of field.
-t delimiter
# join cross
join names.txt town.txt
# mismatch connections, join all
join -a1 -a2 names.txt town.txt
# base on file1, join all
join -a1 names.txt town.txt
# selective join
join -o 1.1,2.2 names.txt town.txt
# different field join
# extract 3rd field of file 1, 2nd field of file 2, then join them together
join -j1 3 -j2 2 file1 file2
cat pers
P.Jones Office Runner ID897
S.Round UNIX admin ID666
L.Clip Personl Chief ID982
cat pers2
Dept2C ID897 6 years
Dept3S ID666 2 years
Dept5Z ID982 1 year
join -j1 4 -j2 2 pers pers2
ID897 P.Jones Office Runner Dept2C 6 years
ID666 S.Round UNIX admin Dept3S 2 years
ID982 L.Clip Personl Chief Dept5Z 1 year
# cut
cut [options] file1 file2
-c LIST, select only these characters
-f LIST, select only these fields
-d, delimiter
cut -d: -f4 pers
cut -d: -f1,3 pers
cut -d: -f1-3 pers
cut -d: -f1,6 /etc/passwd
# file permision
ls -l | cut -c1-10
# paste
paste -d -s - file1 file2
-d, delimiter
-s, paste one file at a time instead of in parallel
paste -d: pas1 pas2
# list file name, 3 files each row
ls | paste -d" " - - -
# list file name, ls -l|awk 'NF>3{print $8}'
ls | paste -d"" -
# split
split -output_file_size input-filename output-filename
-output_file_size, default 1000 lines
output-filename, default x[aa]->x[zz]
评论暂时关闭