Sorting and columns · Mengurutkan dan kolom
Putting lines in order
sortarranges lines alphabetically (or, with-n, as numbers):
sort -rreverses the order;sort -nis for lists of numbers.
Mengurutkan baris
sortmengatur baris secara alfabetis (atau, dengan-n, sebagai angka):
sort names.txt
sort -rmembalik urutan;sort -ndigunakan untuk daftar angka.
Removing duplicates
uniqremoves neighbouring repeated lines. Because it only looks at neighbours, you usuallysortfirst:
- Add
uniq -cto also count how many times each line appeared.
Menghapus duplikat
uniqmenghapus duplikat bertetangga. Karena hanya memeriksa tetangga, Anda biasanya perlusortterlebih dahulu:
sort names.txt | uniq
- Tambahkan
uniq -cuntuk juga menghitung berapa kali setiap baris muncul.
sort, then uniq · sort, lalu uniq
uniq only sees neighbours — sort first, so the duplicates sit together. · uniq hanya melihat tetangga — sort pertama, sehingga duplikatnya duduk bersama.
Picking out columns
- Many files have columns separated by a character — a comma, a colon, a tab.
cutpulls out the columns you want.-dsets the delimiter,-fthe field:
- That takes the first field of each
name:ageline — the names.
Mengambil kolom tertentu
- Banyak file memiliki kolom yang dipisahkan oleh karakter — koma, titik dua, tab.
cutmenarik kolom yang Anda inginkan.-dmenetapkan pemisah delimiter,-fmenentukan field (kolom):
cut -d : -f 1 people.txt
- Itu mengambil kolom pertama dari setiap baris
name:age— yaitu nama-nama.
Common mistakes
sortis alphabetical;sort -nsorts numbers correctly.cutneeds the right delimiter with-d.
Kesalahan umum
sortbersifat alfabetis;sort -nmengurutkan angka dengan benar.cutmemerlukan delimiter yang tepat dengan-d.
Get a tidy, de-duplicated list of names. Pipe sort into uniq: sort names.txt | uniq. · Dapatkan daftar nama yang rapi dan tidak berganda. Pipe sort ke uniq: sort names.txt | uniq.
people.txt has name:age on each line. Pull out just the names with cut -d : -f 1 people.txt. · people.txt memiliki name:age di setiap baris. Tarik keluar hanya nama-namanya dengan cut -d : -f 1 people.txt.