Linux Text Manipulation Commands
Linux Text Manipulation Commands
In Linux, text manipulation is a very important skill. Many times, you will work with files and data that are stored as plain text. Linux provides powerful commands that help you search, edit, cut, format, and transform text quickly and easily. These commands are simple yet very powerful, and learning them will make working with Linux much easier and faster.
Some of the commonly used Linux text manipulation commands:
grep Command
The grep command is used to search for specific patterns or words inside files or output. It prints the lines that match the given pattern.
Example:
grep "hello" filename.txt
This command will search for the word “hello” inside the file filename.txt and display all matching lines.
sed Command
The sed command stands for Stream Editor. It is used to find and replace text, insert lines, or delete lines from a file without opening it manually.
Example:
sed 's/old/new/' filename.txt
This command will replace the first occurrence of the word “old” with “new” in each line of filename.txt.
awk Command
The awk command is a powerful programming language and tool for text processing. It is mainly used to extract specific columns and perform actions on them.
Example:
awk '{print $1}' filename.txt
This command will print the first word (column) of each line from filename.txt.
cut Command
The cut command is used to extract specific sections of text from a file or output, based on characters, bytes, or fields.
Example:
cut -d ',' -f 2 filename.txt
This command will display the second field of each line from filename.txt, assuming the fields are separated by commas.
sort Command
The sort command is used to arrange the lines of text in alphabetical or numerical order.
Example:
sort filename.txt
This command will sort the lines in filename.txt in alphabetical order.
uniq Command
The uniq command filters out repeated lines that are adjacent. It is often used after the sort command.
Example:
sort filename.txt | uniq
This command will sort the file and then remove duplicate lines.
tr Command
The tr command is used to translate or delete characters from the input.
Example:
tr 'a-z' 'A-Z' < filename.txt
This command will convert all lowercase letters to uppercase in filename.txt.
wc Command
The wc command stands for Word Count. It is used to count lines, words, and characters in a file.
Example:
wc filename.txt
This command will display the number of lines, words, and characters in filename.txt.
head Command
The head command is used to display the first few lines of a file.
Example:
head -n 5 filename.txt
This command will show the first 5 lines of filename.txt.
tail Command
The tail command is used to display the last few lines of a file.
Example:
tail -n 5 filename.txt
This command will show the last 5 lines of filename.txt.