Linux on call · Chapter 7 of 30
Searching text with grep
By the end of this chapter you can pull exactly the lines you need out of many files at once: by a word, in any case, counted, inverted or matched by a pattern. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Files and text
- 5 tasks
- Linux
- In the all labs plan
The first 2 chapters of Linux on call are free. This one comes with the all labs plan.
Why you need this
Customers say their payments failed this week, and Ravi wants every failed one. The accountant wants to know which logs mention a refund, and every payment of a hundred rupees or more. Kabir wants to know how many lines of the app's log are real events and not its DEBUG heartbeat, and which line of the config sets the port.
Bunty opened the first payment log in less and started reading. There are four logs in two folders, the app log has sixty lines, and the new payment gateway writes Failed where the old one wrote FAILED. Reading by eye, he will miss some, and he cannot count what he skipped.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Save every failed payment in failed.txt
Find every failed payment in all the logs.
1. Go to the chapter folder.
2. Use grep to search for failed in every file under logs, in its subfolders too. Use the option that searches all folders under logs.
3. Some logs say FAILED and some say Failed. Add the option that ignores upper and lower case.
4. Save the output in failed.txt, in the chapter folder.
To pass: failed.txt has every failed payment line from every log under logs, each one once, and nothing else. The file name that grep puts in front of each line is fine. You ran grep in the chapter folder, and it found lines. A grep that finds nothing, or has a typing mistake, does not count.
- Task 2
Save the logs that mention a refund in refund-files.txt
Find which log files mention a refund.
1. Go to the chapter folder.
2. Use grep to search for refund in every file under logs, in its subfolders too.
3. Add the option that prints only the names of the files that match, not the lines.
4. Save the output in refund-files.txt, in the chapter folder.
To pass: refund-files.txt has the path of every log file under logs that mentions refund, one on each line, and nothing else. No lines from inside the files. Each path starts with logs/, as grep prints it from the chapter folder (like logs/payments-mon.log).
- Task 3
Save the count of non-DEBUG lines in not-debug.txt
Count the lines of app/app.log that do not have DEBUG.
1. Use grep with the option that shows the lines that do not match. Search for DEBUG in app/app.log.
2. Add the option that prints only the count of those lines.
3. Check the number on the screen.
4. Save only that number in not-debug.txt, in the chapter folder.
To pass: not-debug.txt has the number of lines in app/app.log without DEBUG, and nothing else. You ran grep in the chapter folder, and it found lines.
- Task 4
Save the port line and its number in port.txt
Find the line in config/app.conf that sets port.
1. Use grep to search for port in config/app.conf. Most lines that come back have port inside a longer word.
2. Add the option that matches port only as a whole word.
3. Add the option that shows the line number in front of each line.
4. Check that only one line comes back. Save it in port.txt, in the chapter folder.
To pass: port.txt has only the line that sets port, with its line number in front, as grep prints it (number, colon, line). No lines where port is part of a longer word. You ran grep in the chapter folder, and it found lines.
- Task 5
Save every payment of 100 rupees or more in big.txt
Find every payment of 100 rupees or more in all the logs.
1. An amount of 100 or more is an amount= with three or more digits.
2. Use grep with -E (extended patterns). Write a pattern for amount= followed by three or more digits. Put it in single quotes.
3. Search every file under logs, in its subfolders too.
4. Save the output in big.txt, in the chapter folder.
To pass: big.txt has every payment line under logs with an amount of 100 or more, each one once, and nothing else. No payment under 100. The file name in front of each line is fine. You ran grep in the chapter folder, and it found lines.
What you will learn
- grep: print the lines that contain a pattern, in one file or a whole folder.
- Its options: -i any case, -r every file below a folder, -l names only, -c count, -v the lines that do not match, -n line numbers, -w whole words.
- Patterns (regular expressions, -E): any digit, the start of a line, "this or that", "one or more".
More in Files and text
Build "Searching text with grep" yourself
Tapri’s ordering app moves onto a Linux server, and the server is now yours to run. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Linux on call are free. This one comes with the all labs plan.