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 >>
).
AWK Command | SED Command | Use Case |
---|---|---|
awk '{print}' file.txt |
sed '' file.txt |
Print entire file content. |
awk '{print $2}' file.txt |
sed -n 's/.*\(pattern\).*/\1/p' file.txt |
Print a specific column (extract a pattern). |
awk '/error/ {print}' file.txt |
sed -n '/error/p' file.txt |
Print lines matching "error" . |
awk '{print NR, $0}' file.txt |
sed '=' file.txt |
Print line numbers along with content. |
awk 'NF > 5' file.txt |
sed -n '/\(\w\+\s\+\)\{5,\}/p' file.txt |
Print lines with more than 5 words. |
awk '{sum += $2} END {print sum}' file.txt |
`sed -n ‘s/.* [0β9]\+[0-9]\+$/\1/p’ file.txt | paste -sd+ |
awk 'NR==3 {print}' file.txt |
sed -n '3p' file.txt |
Print only the third line. |
awk 'NR==3, NR==5 {print}' file.txt |
sed -n '3,5p' file.txt |
Print lines from 3 to 5. |
awk '$2 > 100' file.txt |
sed -n '/ [1-9][0-9][0-9] /p' file.txt |
Print lines where column 2 is greater than 100. |
awk '!seen[$1]++' file.txt |
sed -n '/^\(.*\)$/!d' file.txt |
Print unique values from the first column. |
awk 'NR % 2 == 0' file.txt |
sed -n 'n;p' file.txt |
Print only even-numbered lines. |
awk '{print tolower($0)}' file.txt |
sed 's/.*/\L&/' file.txt |
Convert text to lowercase. |
awk '{print toupper($0)}' file.txt |
sed 's/.*/\U&/' file.txt |
Convert text to uppercase. |
awk '{$1=""; print}' file.txt |
sed 's/^[^ ]* //' file.txt |
Remove the first column. |
awk '{$NF=""; print}' file.txt |
sed 's/ [^ ]*$//' file.txt |
Remove the last column. |
awk 'sub(/error/, "warning")' file.txt |
sed 's/error/warning/g' file.txt |
Replace "error" with "warning" . |
awk '{print $1, $3}' file.txt |
sed 's/\(\S\+\) \S\+/\1/' file.txt |
Print only columns 1 and 3. |
awk 'NR>5 {print}' file.txt |
sed '1,5d' file.txt |
Delete the first 5 lines. |
awk 'NR==10 {exit}' file.txt |
sed '10q' file.txt |
Stop processing after line 10. |
awk '{printf "%-10s %-10s\n", $1, $2}' file.txt |
sed -E 's/(.*) (.*)/\1 \2/' file.txt |
Format output into a table. |
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.
Linux Text Processing with awk
and sed
β Cheat Sheet with Examples
awk
and sed
are powerful command-line tools used for text processing in Linux. This guide provides detailed examples of both commands for filtering, replacing, and manipulating text.
π sed
(Stream Editor) β Text Substitution & Manipulation
sed
is used for searching, replacing, deleting, and modifying text in a stream or file.
1οΈβ£ Replace a Word in a File
πΉ Example: Replace "error"
with "warning"
in log.txt
.
2οΈβ£ Replace Word Only in Specific Line
πΉ Example: Replace "error"
with "warning"
only on line 3.
3οΈβ£ Delete a Specific Line
πΉ Deletes line 5 from file.txt
.
4οΈβ£ Delete Lines Containing a Specific Word
πΉ Deletes all lines containing "error"
.
5οΈβ£ Print Only Matching Lines
πΉ Prints only the lines that contain “error”.
6οΈβ£ Replace Text in a File and Save Output
πΉ Modifies the file in place by replacing "old"
with "new"
.
7οΈβ£ Remove Empty Lines
πΉ Deletes all empty lines.
8οΈβ£ Insert Text at the Beginning of Each Line
πΉ Adds "Prefix: "
at the beginning of each line.
9οΈβ£ Insert a Line Before a Match
πΉ Inserts "New Line Here"
before lines matching "pattern"
.
π Append a Line After a Match
πΉ Appends "New Line Here"
after lines matching "pattern"
.
π awk
(Pattern Scanning and Processing Language)
awk
is used for filtering, formatting, and processing text in structured data.
1οΈβ£ Print the Entire File
πΉ Prints all lines in file.txt
.
2οΈβ£ Print a Specific Column
πΉ Prints the second column.
3οΈβ£ Print Multiple Columns
πΉ Prints first and third columns.
4οΈβ£ Print Lines Matching a Pattern
πΉ Prints only lines containing "error"
.
5οΈβ£ Print Line Numbers
πΉ Prints each line with a line number.
6οΈβ£ Print Lines with More than 5 Words
πΉ Prints lines with more than 5 fields (words).
7οΈβ£ Sum a Column of Numbers
πΉ Calculates the sum of the second column.
8οΈβ£ Find the Maximum Value in a Column
πΉ Finds the largest number in the second column.
9οΈβ£ Find Unique Values in a Column
πΉ Prints only unique values from the first column.
π Format Output Like a Table
πΉ Aligns the first two columns into a table format.
π Combining awk
and sed
You can combine both tools for powerful text processing.
πΉ Remove Empty Lines and Print Specific Columns
πΉ Removes empty lines and prints column 1 and 3.
πΉ Replace a Word and Print Specific Lines
πΉ Replaces "error"
with "warning"
and prints only matching lines.