Linux provides various text manipulation tools and commands for processing and modifying text files efficiently. Here are some of the most commonly used ones:
1. Viewing and Extracting Text
cat file.txt
– Display entire file content.tac file.txt
– Display file content in reverse.less file.txt
– View file one page at a time.head -n 10 file.txt
– Show the first 10 lines.tail -n 10 file.txt
– Show the last 10 lines.cut -d':' -f1 /etc/passwd
– Extract specific fields from a file.
2. Searching and Filtering
grep "pattern" file.txt
– Search for a pattern in a file.grep -i "pattern" file.txt
– Case-insensitive search.grep -r "pattern" /path/to/dir
– Recursive search.awk '{print $1}' file.txt
– Print specific columns from a file.sed -n '/pattern/p' file.txt
– Print lines matching a pattern.
3. Modifying and Replacing Text
sed 's/old/new/g' file.txt
– Replace text in a file.awk '{ gsub("old", "new"); print }' file.txt
– Replace globally usingawk
.tr 'a-z' 'A-Z'
– Convert lowercase to uppercase.tr -d '[:digit:]'
– Remove digits from input.
4. Sorting and Unique Operations
sort file.txt
– Sort lines alphabetically.sort -r file.txt
– Sort in reverse order.uniq file.txt
– Remove duplicate lines.sort file.txt | uniq -c
– Count occurrences of unique lines.
5. Counting and Summarizing
wc -l file.txt
– Count lines in a file.wc -w file.txt
– Count words in a file.wc -c file.txt
– Count characters in a file.
6. Combining and Splitting Files
paste file1.txt file2.txt
– Merge lines from multiple files.join file1.txt file2.txt
– Join two files based on a common field.split -l 100 file.txt part_
– Split a file into smaller chunks.
7. Formatting and Editing
fmt -w 80 file.txt
– Format text to a specific width.nl file.txt
– Number lines in a file.pr -h "Title" file.txt
– Format file for printing.
8. Advanced Processing
awk '{sum+=$1} END {print sum}' file.txt
– Sum values in a column.sed '/pattern/d' file.txt
– Delete lines matching a pattern.perl -pe 's/old/new/g' file.txt
– Perform regex-based replacements.
These commands are powerful when combined using pipes (|
) and redirection (>
and >>
).
Linux provides powerful text processing commands that allow you to manipulate, search, filter, and transform text files efficiently. Here are some of the most commonly used text processing commands along with examples:
1. cat
(Concatenate and Display Files)
Example:
Output: Displays the entire content of file.txt
.
Output: Merges file1.txt
and file2.txt
into merged.txt
.
2. tac
(Reverse File Content)
Example:
Output: Displays the content of file.txt
in reverse order (last line first).
3. less
(View File Page by Page)
Example:
Usage: Navigate using arrow keys or q
to quit.
4. head
(Display First N Lines)
Example:
Output: Shows the first 5 lines of file.txt
.
5. tail
(Display Last N Lines)
Example:
Output: Shows the last 10 lines of file.txt
.
Usage: Continuously monitors a log file in real time.
6. grep
(Search for a Pattern in a File)
Example:
Output: Displays lines containing “error” in file.txt
.
Usage: Case-insensitive search for “warning”.
Usage: Recursively searches for “failed” in /var/log/
directory.
7. awk
(Pattern Scanning and Processing)
Example:
Output: Prints the first column of each line in file.txt
.
Usage: Prints the first and third field of /etc/passwd
, using :
as a delimiter.
8. sed
(Stream Editor for Text Replacement)
Example:
Usage: Replaces all occurrences of “old” with “new” in file.txt
.
Usage: Deletes lines containing “error” in file.txt
.
9. cut
(Extract Specific Columns)
Example:
Output: Extracts the first field (username) from /etc/passwd
.
Usage: Extracts the first five characters from each line.
10. tr
(Translate or Delete Characters)
Example:
Output: Converts lowercase to uppercase: HELLO WORLD
.
Output: Removes numbers: hello
.
11. sort
(Sort Lines in a File)
Example:
Output: Sorts the lines in ascending order.
Usage: Sorts in descending order.
12. uniq
(Remove Duplicate Lines)
Example:
Output: Removes duplicate lines after sorting.
Usage: Counts occurrences of unique lines.
13. wc
(Count Lines, Words, and Characters)
Example:
Output: Counts the number of lines in file.txt
.
Usage: Counts words in file.txt
.
14. paste
(Merge Lines from Multiple Files)
Example:
Output: Combines corresponding lines from file1.txt
and file2.txt
.
15. nl
(Number Lines in a File)
Example:
Output: Numbers each line in file.txt
.
16. pr
(Format File for Printing)
Example:
Output: Adds a header “Title” to file.txt
before printing.
17. split
(Split a File into Smaller Files)
Example:
Usage: Splits file.txt
into multiple smaller files with 50 lines each.
18. tee
(Write Output to File and Terminal)
Example:
Usage: Displays the ls -l
output on the screen and saves it in output.txt
.
19. diff
(Compare Two Files)
Example:
Output: Shows differences between file1.txt
and file2.txt
.
20. comm
(Compare Sorted Files Line by Line)
Example:
Output: Displays common and unique lines from two sorted files.