Searching and Filtering Data with Linux Utilities
In this step, you will learn how to search and filter data using powerful Linux utilities such as grep, find, and awk.
Let's start by creating a sample text file called data.txt with some content:
cat > data.txt << EOF
John,25,male
Jane,30,female
Bob,35,male
Alice,28,female
EOF
Now, let's search for the word "male" in the data.txt file using the grep command:
grep "male" data.txt
Example output:
John,25,male
Bob,35,male
To search for lines that contain the word "female", we can use:
grep "female" data.txt
Example output:
Jane,30,female
Alice,28,female
You can also use the find command to search for files. For example, to find all files in the current directory and its subdirectories, you can use:
find .
Example output:
.
./data.txt
./newdir
./newdir/copyfile.txt
./file1.txt
./file2.txt
./subdirectory
./subdirectory/file3.txt
./subdirectory/file4.txt
To search for a specific file, you can use the -name option:
find . -name "data.txt"
Example output:
./data.txt
Finally, let's use the awk command to extract specific fields from the data.txt file. For example, to print the second field (age) for each line:
awk -F',' '{print $2}' data.txt
Example output:
25
30
35
28
You have now learned how to use grep, find, and awk to search and filter data in Linux.