Bash
cut & paste
cut and paste commands for column-based text processing.
29 commands
Windows
MacOS
Linux
#text-processing
#columns
cut by Character
Extract characters 1 through 5
cut -c1-5 file.txt
Extract first character of each line
cut -c1 file.txt
Extract from character 5 to end
cut -c5- file.txt
Extract first 10 characters
cut -c-10 file.txt
Extract characters at positions 1, 3, 5
cut -c1,3,5 file.txt
cut by Field
Extract first field from CSV
cut -d ',' -f1 data.csv
Extract fields 1 and 3
cut -d ',' -f1,3 data.csv
Extract username and shell
cut -d ':' -f1,7 /etc/passwd
Extract second tab-delimited field
cut -d '\t' -f2 data.tsv
Extract from field 2 to end
cut -d ' ' -f2- file.txt
Extract fields 1 through 3
cut -d ',' -f1-3 data.csv
paste Operations
Merge files side by side with tabs
paste file1.txt file2.txt
Merge files with comma delimiter
paste -d ',' file1.txt file2.txt
Join all lines into a single line
paste -s file.txt
Join lines with comma separator
paste -s -d ',' file.txt
Arrange into two columns
paste - - < file.txt
Arrange into three columns
paste - - - < file.txt
Delimiter Options
Use pipe as delimiter
cut -d '|' -f2 data.txt
Use semicolon as delimiter
cut -d ';' -f1,3 data.txt
Merge with explicit tab delimiter
paste -d '\t' file1.txt file2.txt
Merge with colon delimiter
paste -d ':' file1.txt file2.txt
Common Patterns
Count unique values in field 2
cut -d ',' -f2 data.csv | sort | uniq -c
Truncate ps output to 80 chars
ps aux | cut -c1-80
Get first directory in PATH
echo $PATH | cut -d ':' -f1
Extract file sizes from ls output
ls -l | cut -d ' ' -f5
Combine columns from two CSVs
paste -d ',' <(cut -d ',' -f1 a.csv) <(cut -d ',' -f2 b.csv)
Quick Commands
Extract first field from a CSV file
cut -d ',' -f1 data.csv
Join all lines into one comma-separated line
paste -s -d ',' file.txt
Get first directory from PATH variable
echo $PATH | cut -d ':' -f1