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
Option | Description |
---|---|
-d <delimiter> | Specify the delimiter (default TAB ) |
-f <list> | Select fields (columns), separated by delimiter |
-c <list> | Select character positions |
--complement | Show everything except the selected field/char |
-s | Suppress 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
- Extracting fields by delimiter
cut -d',' -f2 emp.txt
- Extract name and salary
cut -d',' -f2,4 emp.txt
- Extract the first 3 characters of each line
cut -c1-3 emp.txt
- Skip the header and extract “Deparment” and “Location”
cat emp.txt | cut -d',' -f3,5
- Replace output delimiter with
:
instead of,
cut -d',' -f1,2 --output-delimiter=':' emp.txt
- 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
Option | Description |
---|---|
sort -n | Numeric Sort |
sort -r | Reverse (descending) Sort |
sort -k | Specify Sort Key (field/column) |
sort -u | Unique Lines (remove duplicates) |
sort -t | Specify 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
- 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
.
- Sort by Date (4th column) alphabetically
sort -t ',' -k4,4 emp.csv #sort alphabetically by default
- Sort by Customer Name (2nd column) in reverse order
sort -t ',' -k2,2 -r emp.csv
- Remove duplicate lines while sorting by amount
sort -t ',' -k3,3 -u emp.csv
- 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
Option | Description |
---|---|
-c | Prefix lines by the number of occurrences |
-d | Only print duplicate lines |
-u | Only 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
- Show unique hostnames
sort hosts.txt | uniq
- Count occurrences of each hostname
sort hosts.txt | uniq -c
- Show only hostnames that appear more than once
sort hosts.txt | uniq -d
- 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/Pattern | Description |
---|---|
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
- Convert all lowercase letters to uppercase letters
echo "Hello Bro!" | tr 'a-z' 'A-Z'
- Delete all digits from input
echo "abc123" | tr -d '0-9'
- Squeeze multiple spaces into single space
echo "a b c" | tr -s ' '
- Replace commas with tabs
echo "a,b,c" | tr ',' '\t'