Day 4 [part :- 3] :- ๐Ÿ” Mastering Text Processing in Ubuntu: grep, find, awk, and sed ๐Ÿš€

Day 4 [part :- 3] :- ๐Ÿ” Mastering Text Processing in Ubuntu: grep, find, awk, and sed ๐Ÿš€

ยท

2 min read

The grep, find, awk, and sed commands are powerful tools in the Ubuntu command line interface (CLI) used for searching, processing, and manipulating text. Here's an explanation of each:

grep

grep (Global Regular Expression Print) is used to search for specific patterns within files. It supports regular expressions, making it a versatile tool for pattern matching.

Basic Usage:

grep 'pattern' filename

#for multiple
grep "pattern" filename1 filename2

#for printing with line number
grep -n "pattern" filename

Example:

grep 'hello' file.txt

This command searches for the word "hello" in file.txt.

find

find is used to search for files and directories within a directory hierarchy. It can search by name, type, size, modification time, and more.

Basic Usage:

find [path] [expression]

Example:

find /home/user -name "*.txt"

This command searches for all .txt files in the /home/user directory.

awk

awk is a powerful programming language for pattern scanning and processing. It is used for manipulating data and generating reports.

Basic Usage:

awk 'pattern {action}' filename

Example:

awk '{print $1}' file.txt

#for multiple

awk '{print $1,$2,...}' file.txt

This command prints the first column of each line in file.txt.

sed

sed (Stream Editor) is used for parsing and transforming text. It can perform basic text transformations on an input stream (a file or input from a pipeline).

Basic Usage:

sed 's/pattern/replacement/g' filename
#g means global and you can use also number , like how many time you want to change it

Example:

sed 's/hello/world/' file.txt

This command replaces the first occurrence of "hello" with "world" in each line of file.txt.

These commands are essential for text processing and file management in the Ubuntu CLI, providing robust functionality for a variety of tasks.

Thank you for reading!

ยฉ 2024 Anand Raval. All rights reserved.

ย