✂️ cut – Extract Columns (Fields) from a File
What it does
Extracts specific columns/fields from each line.
Syntax
cut -d "delimiter" -f field_list file
Examples
File: emp.txt
101 Amit 45000 IT
102 Neha 52000 HR
103 Rahul 48000 IT
- Print ID and Name
cut -d " " -f 1,2 emp.txt
- CSV file (comma-separated)
cut -d "," -f 2 data.csv
- By character position
cut -c 1-5 file.txt
cat > stat.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
ctrl+z
cut -b 1,2,3 stat.txt
cut -b -3 stat.txt
cut -c 2,3 stat.txt
cat > b.txt
01234:567;89
abcd:drg;ij
ctrl+z
cut -d ";" -f1 b.txt //-f column name
cut -d ":" -f2 b.txt
who | cut -c 1-2
⚠️ Note: cut works best with single, consistent delimiters.
📎 paste – Combine Files Line by Line
What it does
Merges lines of multiple files horizontally.
Syntax
paste file1 file2
Examples
Files
names.txt salary.txt
Amit 45000
Neha 52000
- Combine side by side
paste names.txt salary.txt
Output
Amit 45000
Neha 52000
- Use comma as delimiter
paste -d "," names.txt salary.txt
- Merge all lines into one
paste -s names.txt
paste a.txt b.txt
paste -d “%” a.txt b.txt
paste -d “1,” a.txt b.txt c.txt
cat stat.txt | paste
🔗 join – Join Two Files Using a Common Field
What it does
Joins two files based on a common key (column)
➡️ Similar to SQL JOIN
Syntax
join file1 file2
Example
File: emp_id.txt
101 Amit
102 Neha
103 Rahul
File: emp_dept.txt
101 IT
102 HR
103 IT
- Join on first column
join emp_id.txt emp_dept.txt
Output
101 Amit IT
102 Neha HR
103 Rahul IT
⚠️ Important:
- Files must be sorted on join field
sort emp_id.txt > a.txt
sort emp_dept.txt > b.txt
join a.txt b.txt
- Join on specific fields
join -1 1 -2 1 file1 file2
📊 Quick Comparison (Very Important for Exams)
| Command | Purpose |
|---|---|
cut | Extract columns |
paste | Combine files horizontally |
join | Combine files using common field |
🧠 Easy Memory Trick
- cut → take out columns ✂️
- paste → stick together files 📎
- join → connect using key 🔗