cut - Extract Section From Each Line

The cut command is used to extract specific fields or character positions from each line of input. It is especially useful for processing text files, logs, or command output where you need to isolate columns or substrings.

Essential Options

OptionDescription
-d <delimiter>Specify the delimiter (default TAB)
-f <list>Select fields (columns), separated by delimiter
-c <list>Select character positions
--complementShow everything except the selected field/char
-sSuppress lines without the delimiter
--output-delimiter <string>Use a custom output delimiter (GNU cut)

Examples

Use this sample file: employee.txt

ID,Name,Department,Salary,Location
1,Alice,Engineering,85000,New York
2,Bob,Sales,65000,San Francisco
3,Charlie,HR,60000,Chicago
4,David,Engineering,90000,Boston
5,Eva,Marketing,70000,Seattle
  1. Extracting fields by delimiter
cut -d',' -f2 emp.txt
  1. Extract name and salary
cut -d',' -f2,4 emp.txt
  1. Extract the first 3 characters of each line
cut -c1-3 emp.txt
  1. Skip the header and extract “Deparment” and “Location”
cat emp.txt | cut -d',' -f3,5
  1. Replace output delimiter with : instead of ,
cut -d',' -f1,2 --output-delimiter=':' emp.txt
  1. Display everything except the ID
cut -d ',' -f1 --complement emp.txt

sort - Sort Lines Of Text Files

The sort command arranges lines in text files in alphabetical, numerical or custom column-based order. It’s a powerful tool for organizing data, finding duplicates, and preparing files for further processing.

Essential Options

OptionDescription
sort -nNumeric Sort
sort -rReverse (descending) Sort
sort -kSpecify Sort Key (field/column)
sort -uUnique Lines (remove duplicates)
sort -tSpecify Delimiter For Fields (default:space)

Examples

Use this sample file: emp.csv

OrderID,Customer,Amount,Date
1003,Alice,250,2025-07-01
1001,Bob,100,2025-06-29
1005,Charlie,300,2025-07-03
1002,Eva,150,2025-06-30
1004,David,100,2025-07-02
  1. Sort by Amount (3rd Column) numerically
sort -t ',' -k3,3 -n emp.csv

We need to separate the fields in order to access the 3rd column,

  • Use -t "," to specifiy delimiter which separates the lines with ,(comma) as delimiter.
  • Then we specify -k3,3 which selects the 3rd column (because the fields are separated)
  • and finally, we can sort it numerically by using -n.
  1. Sort by Date (4th column) alphabetically
sort -t ',' -k4,4 emp.csv #sort alphabetically by default
  1. Sort by Customer Name (2nd column) in reverse order
sort -t ',' -k2,2 -r emp.csv
  1. Remove duplicate lines while sorting by amount
sort -t ',' -k3,3 -u emp.csv
  1. Sort by OrderID (1st column) numerically
sort -t ',' -k1,1 -n emp.csv

uniq - Report Or Filter Repeated Lines

The uniq command is used to remove duplicate adjacent lines from a file or output. It is most effective when used after sort, as only consecutive duplicate lines are filtered.

Essential Options

OptionDescription
-cPrefix lines by the number of occurrences
-dOnly print duplicate lines
-uOnly print unique lines

Examples

Use this sample file: hosts.txt

server1.example.com
server2.example.com
server1.example.com
server3.example.com
server2.example.com
server2.example.com
server4.example.com
server5.example.com
server5.example.com
server5.example.com
  1. Show unique hostnames
sort hosts.txt | uniq
  1. Count occurrences of each hostname
sort hosts.txt | uniq -c
  1. Show only hostnames that appear more than once
sort hosts.txt | uniq -d
  1. Show only hostnames that appear exactly once
sort hosts.txt | uniq -u

tr - Translate or Delete Characters

The tr command is used to translate (replace), squeeze, or delete characters from standard input. It’s great for quick, in-place text transformations.

Essential Options

Option/PatternDescription
tr 'a' 'A'Replace all a with A
tr 'a-z' 'A-Z'Convert lowercase into uppercase
tr -d '0-9'Delete all the digits
tr -s ' 'Squeeze repeated spaces into one
tr -cd 'A-Za-z\n'Delete all except letter and newlines

Examples

  1. Convert all lowercase letters to uppercase letters
echo "Hello Bro!" | tr 'a-z' 'A-Z'
  1. Delete all digits from input
echo "abc123" | tr -d '0-9'
  1. Squeeze multiple spaces into single space
echo "a     b       c" | tr -s ' '
  1. Replace commas with tabs
echo "a,b,c" | tr ',' '\t'