AI Hints
Home›Linux Tutorial›Patterns and Practice
Linux · Lesson 60 of 120

Practice: Text Challenge

Tr, sed and friends.

⏱ 2 min read▶ Video at 1:17:50🎓 Free
Watch this lesson in the Linux courseWatch on YouTube ↗Full Linux course

One last challenge for the text tools. This one uses the team file, a small CSV. Read the tasks, and try each one before you watch the answer.

Practice: Text Challenge
From the video: Practice: Text Challenge
  • One last text challenge
  • A small CSV file
  • Try each task first

The tasks

Task one. Print only the names, without the header line. Task two. Show the file with every role in capital letters. Task three. Count how many people live in London. Pause, and try all three.

Task 1names only, no header
Task 2roles in CAPITALS
Task 3how many in London?
Toolstail, cut, tr, sed, grep

Some answers

Tail dash N plus two starts from line two, which skips the header, and cut takes the first field. For task two, awk with a comma separator, and the toupper function, capitalises the second field. And grep dash C London counts two people.

Terminal
tail -n +2 team.csv | cut -d, -f1
sam
alex
jo
priya
awk -F, 'BEGIN{OFS=","} {$2=toupper($2); print}' team.csv
name,ROLE,city
sam,DEVELOPER,London
alex,DESIGNER,Paris
jo,DEVELOPER,Berlin
priya,MANAGER,London
grep -c London team.csv
2

There is always another way

Don't worry if your answer looked different. On Linux, there are usually several ways to do the same thing. If your command gave the right result, it's a right answer. Over time, you'll find the tools you like best.

keyvalue
Many waysto do the same job
Right resultis a right answer
Your favouritesyou'll find them
Practicemakes it natural

Summary

That's the end of the text tools section. You can search, count, slice, and change text like a pro. Next, a new section. Users and permissions.

Key takeaways
  • grep, cut, awk
  • sort, uniq, tr, sed
  • Pipelines
Next up: Users and Groups — who can do what