Sorting and columns
เรียงบรรทัด
sortเรียงบรรทัดตามลำดับตัวอักษร (หรือ, ด้วย-n, เป็นตัวเลข):
sort names.txt
sort -rกลับลำดับ;sort -nสำหรับรายการของตัวเลข
ลบค่าซ้ำ
uniqลบ คู่ซ้ำที่ติดกัน เพราะมันมองแค่เพื่อนข้าง คุณมักต้องsortก่อน:
sort names.txt | uniq
- เพิ่ม
uniq -cเพื่อ นับ ว่าแต่ละบรรทัดปรากฏกี่ครั้งด้วย
sort, then uniq
uniq only sees neighbours — sort first, so the duplicates sit together.
เลือกคอลัมน์
- ไฟล์หลายฉบับมีคอลัมน์คั่นด้วยตัวอักษร — เจุระ, ลามด้า, หรือแท็บ
cutดึงคอลัมน์ที่ต้องการออกมา-dกำหนด delimiter (ตัวคั่น),-fคือ field (ช่องข้อมูล):
cut -d : -f 1 people.txt
- สิ่งนี้จะดึง สนามแรก ของแต่ละ
name:ageบรรทัด — ชื่อ
ข้อผิดพลาดที่พบบ่อย
sortเป็นแบบตัวอักษร;sort -nเรียงตัวเลขถูกต้องcutต้องใช้ delimiter ที่ถูกต้องกับ-d
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Get a tidy, de-duplicated list of names. Pipe sort เข้าใน uniq: sort names.txt | uniq.
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
people.txt has name:age on each line. Pull out just the names with cut -d : -f 1 people.txt.
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
How many times does each name appear in names.txt? Show each name once, with its จำนวนนับ in front.
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
sizes.txt holds three file sizes. Sort them as numbers, largest first. (Plain sort -r puts 9 first: it compares text.)