Chapter 22: Bash Remove Section (cut)
What does cut actually do? (super simple first)
cut = remove sections from each line of files (That’s literally what the man page says!)
It cuts out (extracts) only the parts you want from each line and prints them. You tell it:
- Cut by characters (-c)
- Cut by bytes (-b)
- Cut by fields/columns (-f) using a delimiter (-d)
Think of it as a column selector or text slicer – very fast, very lightweight, perfect for logs, CSV, passwd file, who output, etc.
It does NOT edit the file – it shows the result on screen (or you redirect to new file).
1. Create test files right now (copy-paste these)
First – simple text file:
|
0 1 2 3 4 5 6 7 8 9 10 |
cat > users.txt << 'EOF' armanshaikh:x:1000:1000:Arman Hyderabad:/home/arman:/bin/bash rahul:x:1001:1001:Rahul Delhi:/home/rahul:/bin/zsh priya:x:1002:1002:Priya Bangalore:/home/priya:/bin/bash EOF |
Second – comma separated (CSV-like):
|
0 1 2 3 4 5 6 7 8 9 10 11 |
cat > employees.csv << 'EOF' 101,Arman,Hyderabad,Developer,85000 102,Rahul,Delhi,Manager,120000 103,Priya,Bangalore,Tester,65000 104,Suresh,Hyderabad,Developer,90000 EOF |
Third – spaces/tabs example:
|
0 1 2 3 4 5 6 7 8 9 10 |
cat > scores.txt << 'EOF' Math 85 Arman Science 92 Rahul English 78 Priya EOF |
2. Three main ways to cut (these are the only modes!)
| Mode | Option | When to use | Example command |
|---|---|---|---|
| By character position | -c | Fixed-width text, no delimiters | cut -c 1-5 file.txt |
| By byte position | -b | Same as -c in ASCII, different in UTF-8 | cut -b 1-10 file.txt |
| By field/column | -f + -d | Delimited files (CSV, : , tab, space) | cut -d ‘,’ -f 2 employees.csv |
3. Most common usage – cut fields (columns) – 90% of real use
Default delimiter = TAB
But almost always we change it with -d
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# Extract usernames from /etc/passwd (delimiter = :) cut -d ':' -f 1 /etc/passwd # Extract names from our users.txt cut -d ':' -f 5 users.txt # Output: # Arman Hyderabad # Rahul Delhi # Priya Bangalore # Extract 2nd column (name) from CSV cut -d ',' -f 2 employees.csv # Arman # Rahul # Priya # Suresh # Multiple columns (1 and 5 – ID + salary) cut -d ',' -f 1,5 employees.csv # 101,85000 # 102,120000 # ... # Range of columns (2 to 4) cut -d ',' -f 2-4 employees.csv # Arman,Hyderabad,Developer # Rahul,Delhi,Manager # ... # All except one column (from 2 to end) cut -d ',' -f 2- employees.csv |
4. Change output delimiter (very useful!)
|
0 1 2 3 4 5 6 7 8 9 10 |
# Make output use | instead of , cut -d ',' -f 1,3 --output-delimiter=' | ' employees.csv # 101 | Hyderabad # 102 | Delhi # ... |
5. Cut by character position (-c)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# First 5 characters of each line cut -c 1-5 users.txt # arman # rahul # priya # Characters 15-25 cut -c 15-25 users.txt # Specific positions (1,3,5) cut -c 1,3,5 scores.txt |
Note: -c counts Unicode characters (good for emojis, non-ASCII)
6. Cut by bytes (-b) – rare unless dealing with binary-ish data
|
0 1 2 3 4 5 6 7 |
echo "Hyderabad 2026" | cut -b 1-9 # Hyderabad |
(Usually same as -c for English text)
7. Real-world examples you will use every day
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# Get current logged-in users (only usernames) who | cut -d ' ' -f 1 | sort | uniq # Extract IP from ifconfig / ip addr (old style) ifconfig | grep "inet " | cut -d ':' -f 2 | cut -d ' ' -f 1 # Get home directories from passwd cut -d ':' -f 6 /etc/passwd # Top memory processes – PID + %MEM ps aux --sort=-%mem | head -10 | cut -c 10-15,25-30 # Clean CSV – keep only name and city cut -d ',' -f 2,3 employees.csv > names_cities.csv # From date output – only month and day date | cut -d ' ' -f 2,3 # Feb 25 |
8. Quick cheat-sheet table
| Goal | Command example | Notes |
|---|---|---|
| First column (tab sep) | cut -f 1 file.txt | Default delimiter = tab |
| Second column (comma sep) | cut -d ‘,’ -f 2 file.csv | Most common |
| Columns 1 + 3 + 5 | cut -d ‘,’ -f 1,3,5 file.csv | List with commas |
| From column 2 to end | cut -d ‘,’ -f 2- file.csv | Drop first column |
| Characters 1 to 10 | cut -c 1-10 file.txt | Fixed width |
| Change output separator | cut -d ‘,’ –output-delimiter=’;’ -f 1,2 file.csv | Nice for new format |
| From pipe (no file) | echo "a,b,c" |
cut -d ',' -f 2 |
| Only first field + unique | `cut -d ‘:’ -f 1 /etc/passwd | sort |
9. Pro tips from daily use
- Always specify -d when not using tabs (most files are comma/space/colon)
- Use –output-delimiter when you want pretty output
- cut is very fast – great in pipelines (grep | cut | sort | uniq)
- If fields have delimiter inside quotes → cut fails (use awk or csvkit instead)
- Test on one line first: head -1 file.csv | cut -d ‘,’ -f 1-3
Now open your terminal and try these 3:
|
0 1 2 3 4 5 6 7 8 |
cut -d ',' -f 2,3 employees.csv cut -d ':' -f 1,5 users.txt who | cut -d ' ' -f 1 |
Tell me what you see! Or ask:
- “How to cut only usernames from who command but skip empty lines?”
- “How to cut IP addresses from ip addr output?”
- “cut vs awk – when to use which?”
We’ll build exact commands together! 😄
