🏫 The School›🧹 Shell scripting›✂️ धडा 04 — Text tools: ऑफिस
🖼️ See the drawing + lab 🏠 Course home 🌿 Branch on GitHub ✏️ View source
🖼️ आकृती आणि labThe drawing + lab पूर्ण पानावर उघडा ↗Open full page ↗

✂️ धडा 04 — Text tools: ऑफिस

📍 तुम्ही इथे आहात: 12 पैकी धडा 04 · पुढे: lesson-05-variables-quoting


📦 या ब्रँचमध्ये काय आहे

धडे 01–04. text file बद्दलच्या कोणत्याही प्रश्नाचे उत्तर देणारे छोटे tools, आणि reports लिहिणारे दोन.

🧒 5 वर्षांच्या मुलाला समजावल्यासारखे

नोंदवही एक CSV आहे: प्रत्येक विद्यार्थ्यासाठी एक ओळ, columns मध्ये स्वल्पविराम. ऑफिसमध्ये तज्ज्ञ आहेत: grep ओळी शोधतो, cut columns निवडतो, sort क्रम लावतो, uniq -c पुनरावृत्ती मोजतो, sed पुन्हा लिहितो, awk columns आणि गणित करतो, wc मोजतो, head/tail डोकावून पाहतात. त्यांच्यापैकी कोणीही दुसऱ्याला ओळखत नाही; धडा 03 मधला pipe त्यांची ओळख करून देतो. बहुतेक प्रश्नांना तीन तज्ज्ञ पुरतात.

🗺️ आकृती

flowchart LR
  F["📋 register.csv"] --> G["grep ',3A,'"] --> C["cut -d, -f3"] --> S["sort | uniq -c"] --> A["🧮 awk sums, formats"] --> R["📄 the report"]

❓ काय

Tool काय करतो लक्षात ठेवा
grep ओळी शोधतो -i case · -v उलटे · -c मोजा · -n ओळ क्रमांक · -E regex · -r आतल्या folders सकट
cut columns निवडतो -d, delimiter · -f2 field
sort / uniq क्रम / duplicates काढणे uniq ला sorted input लागतो · uniq -c मोजतो · sort -t, -k3 field नुसार
sed ओळी पुन्हा लिहितो s/old/new/ · -i जागेवरच (macOS: -i '') · -n '2,5p'
awk columns, बेरजा, गट -F, · $1 पहिले field · NR ओळ क्रमांक · END {}
wc, head, tail, tr, column, xargs मोजणे, डोकावणे, अक्षरे बदलणे, table करणे, लागू करणे tail -n +2 header वगळतो · tr -s ' ' दाबून एक करतो

🤔 का

log किंवा CSV बद्दलचा प्रश्न एका ओळीत सुटायला हवा, program लिहून नाही. जेव्हा सुमारे तीनपेक्षा जास्त tools किंवा nested condition लागते, तेव्हा Python घ्या (trade-offs पान पाहा) — पण असे किती क्वचित होते हे पाहून तुम्हाला आश्चर्य वाटेल.

🔧 कसे (या repo मध्ये)

cmd_report: awk -F, '{ n[$2]++; if ($3=="A+"||$3=="A") top[$2]++ } END { for (c in n) printf … }' — वर्गानुसार गट, मोजणी, format. cmd_grade: awk -F, -v OFS=, -v n="$name" -v g="$grade" '$1==n {$3=g} {print}' — एका row वरचा एक column बदला, सगळे छापा.

🧪 करून पाहा

tail -n +2 data/register.csv | cut -d, -f2 | sort | uniq -c            # students per class
grep -c ',3A,' data/register.csv                                       # how many in 3A
awk -F, 'NR>1 && $3=="A" {print $1}' data/register.csv                 # who has an A
sed -n '2,3p' data/register.csv | sed 's/,/ | /g'                      # two rows, prettier
bin/school.sh report
sort -t, -k3 data/register.csv | column -t -s,                          # by grade

✅ तपासा — तुम्हाला काय दिसायला हवे

3 3A आणि 2 3B; 3; Aishwarya आणि Meera; class 3A: 3 students, 2 with A or A+ (67%) आणि 3B ची ओळ.

🏁 तुम्ही आत्ताच काय सिद्ध केले

कोणत्याही text file बद्दलचे मोजणी, गाळणी आणि गटवारीचे प्रश्न तुम्ही एका ओळीत सोडवू शकता, आणि शाळेचा report लिहिणारा awk वाचू शकता.

⚠️ नेहमीच्या चुका

🏭 प्रत्यक्ष वापरात हे का महत्त्वाचे: "या log मध्ये मिनिटाला किती 5xx?" याचे उत्तर पहाटे 3 वाजता grep | cut | sort | uniq -c असते. हे सहा tools माहीत असणे म्हणजे उत्तर आणि ticket यांच्यातला फरक.

⏭️ पुढे

धडा 05 — variables आणि quoting: name tags, आणि shell मधली सर्वात प्रसिद्ध आपत्ती टाळणारा एक नियम.

✂️ Lesson 04 — The text tools: the office

📍 You are here: Lesson 04 of 12 · Next: lesson-05-variables-quoting


📦 What's in this branch

Lessons 01–04. The small tools that answer any question about a text file, and the two that write reports.

🧒 Explain like I'm 5

The register is a CSV: one line per student, commas between columns. The office has specialists: grep finds lines, cut picks columns, sort orders, uniq -c counts repeats, sed rewrites, awk does columns and arithmetic, wc counts, head/tail peek. None of them knows the others; the pipe from lesson 03 introduces them. Most questions are three specialists long.

🗺️ Diagram

flowchart LR
  F["📋 register.csv"] --> G["grep ',3A,'"] --> C["cut -d, -f3"] --> S["sort | uniq -c"] --> A["🧮 awk sums, formats"] --> R["📄 the report"]

❓ What

Tool Does Remember
grep find lines -i case · -v invert · -c count · -n line numbers · -E regex · -r recurse
cut pick columns -d, delimiter · -f2 field
sort / uniq order / dedupe uniq needs sorted input · uniq -c counts · sort -t, -k3 by field
sed rewrite lines s/old/new/ · -i in place (macOS: -i '') · -n '2,5p'
awk columns, sums, groups -F, · $1 first field · NR line number · END {}
wc, head, tail, tr, column, xargs count, peek, translate, tabulate, apply tail -n +2 skips the header · tr -s ' ' squeeze

🤔 Why

A question about a log or a CSV should take one line, not a program. When it takes more than about three tools or a nested condition, reach for Python (see the trade-offs page) — but you will be surprised how rarely that is.

🔧 How (in this repo)

cmd_report: awk -F, '{ n[$2]++; if ($3=="A+"||$3=="A") top[$2]++ } END { for (c in n) printf … }' — group by class, count, format. cmd_grade: awk -F, -v OFS=, -v n="$name" -v g="$grade" '$1==n {$3=g} {print}' — change one column on one row, print everything.

🧪 Try it

tail -n +2 data/register.csv | cut -d, -f2 | sort | uniq -c            # students per class
grep -c ',3A,' data/register.csv                                       # how many in 3A
awk -F, 'NR>1 && $3=="A" {print $1}' data/register.csv                 # who has an A
sed -n '2,3p' data/register.csv | sed 's/,/ | /g'                      # two rows, prettier
bin/school.sh report
sort -t, -k3 data/register.csv | column -t -s,                          # by grade

✅ Verify — what you should see

3 3A and 2 3B; 3; Aishwarya and Meera; class 3A: 3 students, 2 with A or A+ (67%) and the 3B line.

🏁 What you just proved

You can answer counting, filtering and grouping questions about any text file in one line, and read the awk that writes the school's report.

⚠️ Common mistakes

🏭 Why this matters in production: "how many 5xx per minute in this log?" is grep | cut | sort | uniq -c at 3 a.m. Knowing these six tools is the difference between an answer and a ticket.

⏭️ Next

Lesson 05 — variables & quoting: name tags, and the one rule that prevents the most famous shell disaster.

← Previouspipes redirectionNext →variables quoting

This page is the lesson's README from the lesson-04-text-tools branch, shown here so the whole School stays on one site. Code files open on GitHub at the same branch.