Real-world Applications of Linux Pipelines
In this final step, you will explore some real-world applications of Linux pipelines by analyzing log files, processing data files, and solving common system administration tasks.
Analyzing Log Files
System administrators often need to extract useful information from log files. Let's use pipelines to analyze the server_log.txt file:
- Count occurrences of each log level (INFO, WARNING, ERROR):
cd ~/project
cat server_log.txt | grep -o "INFO\|WARNING\|ERROR" | sort | uniq -c
Output:
4 INFO
3 ERROR
2 WARNING
- Extract all timestamps and log levels:
cat server_log.txt | grep -o "\[[0-9-]* [0-9:]*\] [A-Z]*" | head -5
Output:
[2023-05-10 08:45:22] INFO
[2023-05-10 09:12:35] ERROR
[2023-05-10 09:14:01] INFO
[2023-05-10 09:14:10] INFO
[2023-05-10 09:30:45] WARNING
Processing CSV Data
Let's use pipelines to analyze the sales.csv file:
- Extract and count unique products sold:
cat sales.csv | tail -n +2 | cut -d',' -f2 | sort | uniq -c
The tail -n +2 command skips the header line of the CSV file.
Output:
2 Keyboard
2 Laptop
2 Monitor
2 Mouse
1 Printer
- Calculate the total number of units sold:
cat sales.csv | tail -n +2 | cut -d',' -f3 | paste -sd+ | bc
Output:
113
This pipeline extracts the third column (Units), combines all values with "+" signs, and then uses the bc calculator to compute the sum.
System Monitoring Tasks
Linux pipelines are also useful for system monitoring and administration tasks:
- List the top 5 processes consuming the most memory:
ps aux | sort -k 4 -r | head -6
This command lists processes sorted by the 4th column (memory usage) in reverse order and shows the top 6 lines (including the header).
- Find all files larger than 10MB and sort them by size:
cd ..
find . -type f -size +10M -exec ls -lh {} \; | sort -k 5 -h
This command will show our large test files sorted by size. The output should look similar to:
-rw-r--r-- 1 labex labex 12M May 10 12:00 ./large_file3.dat
-rw-r--r-- 1 labex labex 15M May 10 12:00 ./large_file2.dat
-rw-r--r-- 1 labex labex 20M May 10 12:00 ./large_file1.dat
This example demonstrates finding and sorting files by size. The files were created during setup specifically to show how file size filtering works in Linux.
These examples demonstrate how Linux pipelines can be used to solve real-world problems efficiently. By combining simple commands, you can create powerful data processing workflows without writing complex scripts or programs.