Skip to main content

Command Palette

Search for a command to run...

Day-4 text processing cmnds In Linux

Updated
β€’4 min read
Day-4  text processing cmnds In Linux
M

Hi, I'm Mounika Pogakula, a passionate DevOps enthusiast transitioning into tech with a strong foundation in Linux, Networking, and Cloud fundamentals. I hold a B.Sc. in Computers and a Networking Essentials certification. I’m diving deep into tools like Git, Docker, Jenkins, Kubernetes, and AWS, while sharing my real-world learning journey, hands-on practice, and beginner-friendly insights here on Hashnode. πŸ“š I'm especially focused on simplifying complex topics, documenting my progress, and building high-impact projects that help meβ€”and othersβ€”grow in the DevOps space. Let’s learn, build, and grow together πŸ’»βœ¨

πŸ“„ What is Text Processing?

Text processing means working with text data to read, change, organize, or analyze it.

  • It involves manipulating the actual content of text files or streams.

  • You can extract parts of the text, replace words, count data, sort lines, or format output.

  • It’s like editing or analyzing text to get useful information.

Example:

  • Extracting usernames from a file.

  • Changing all "error" words to "warning".

  • Counting how many words are in a documents

Linux Text Processing Commands

  • sed β€” Stream editor to find & replace text, filter lines

  • awk β€” Powerful column/field extraction and processing tool

  • cut β€” Extract specific columns or character ranges

  • sort β€” Sort lines alphabetically or numerically

  • uniq β€” Remove duplicate adjacent lines

  • tr β€” Translate or delete characters (e.g., case conversion)

  • wc β€” Count lines, words, characters

βœ… 1. awk β€” Extract and process columns

awk is a powerful text-processing tool that works on column-based data.

Syntax:

awk '{print $1, $3}' filename

Example:
File: data.txt

Alice 24 Engineer
Bob 30 Doctor
Cara 28 Teacher

Command:

awk '{print $1, $3}' data.txt

Output:

Alice Engineer
Bob Doctor
Cara Teacher

πŸ“Œ Explanation: Prints 1st and 3rd columns (Name and Job).


βœ… 2. sed β€” Replace or edit text

. sed β€” Stream editor for text substitution and filtering

sed stands for stream editor, which is used to search a word in the file and replace it with the word required to be in the output

Note: it will only modify the output, but there will be no change in the original file.

sed is a stream editor used to find and replace text or edit in-place.

Syntax:

sed 's/old/new/' filename

Example:

echo "I like cats" | sed 's/cats/dogs/'

Output:

I like dogs

πŸ“Œ Explanation: Replaces the word "cats" with "dogs".

To replace in a file permanently:

sed -i 's/cats/dogs/' file.txt

Syntax:

sed 's/old/new/g' filename

Example:
File: file.txt

Hello world
Hello everyone

Command:

sed 's/Hello/Hi/g' file.txt

Output:

Hi world
Hi everyone
  • πŸ“Œ Explanation:
    Replaces all occurrences of "Hello" with "Hi" in each line.

4. cut β€” Extract specific columns or characters

Syntax:

cut -d'delimiter' -f field_numbers filename

Example:
File: /etc/passwd (sample line)

root:x:0:0:root:/root:/bin/bash

Command:

cut -d':' -f1 /etc/passwd

Output:

root

πŸ“Œ Explanation:
Extracts and prints the first field (username) from colon-separated lines.


5. sort β€” Sort lines in a file

Syntax:

sort filename

Example:
File: names.txt

Bob
Alice
Cara

Command:

sort names.txt

Output:

Alice
Bob
Cara

πŸ“Œ Explanation:
Sorts lines alphabetically in ascending order.


6. uniq β€” Filter duplicate adjacent lines

Syntax:

uniq filename

Example:
File: sorted.txt

Alice
Alice
Bob
Cara
Cara
Cara

Command:

uniq sorted.txt

Output:

Alice
Bob
Cara

πŸ“Œ Explanation:
Removes duplicate consecutive lines (use sort first for non-adjacent duplicates).


7. tr β€” Translate or delete characters

Syntax:

tr SET1 SET2

Example:

echo 'hello' | tr 'a-z' 'A-Z'

Output:

HELLO

πŸ“Œ Explanation:
Converts all lowercase letters to uppercase.


8. wc β€” Count lines, words, characters

Syntax:

wc -l filename

Example:
File: file.txt

Hello world
This is Linux

Command:

wc -l file.txt

Output:

2 file.txt

πŸ“Œ Explanation:
Counts the number of lines in the file.

🧠 Linux Filter Commands – Quick Reference Table

CommandPurpose / Use CaseExample
sedStream editor to find & replace, delete, or filter linessed 's/old/new/' file.txt
awkPowerful tool for field/column processing and pattern matchingawk '{print $1}' file.txt
cutExtract specific columns or character rangescut -d',' -f1 data.csv
sortSort lines alphabetically or numericallysort names.txt
uniqRemove duplicate adjacent lines`sort names.txt
trTranslate or delete characters (e.g., case change)tr 'a-z' 'A-Z' < file.txt
wcCount lines, words, characters in inputwc -l file.txt