## Extract columns and character ranges from each line

# First field of a colon-separated file
cut -d: -f1 /etc/passwd

# Several fields
cut -d: -f1,3,7 /etc/passwd

# A range of fields
cut -d: -f1-3 /etc/passwd

# From field 3 to the end
cut -d: -f3- /etc/passwd

# Everything up to field 3
cut -d: -f-3 /etc/passwd

# Tab is the default delimiter
cut -f2 data.tsv

# Comma-separated file
cut -d, -f1,2 users.csv

# Space as the delimiter (only a single space, not runs of spaces)
cut -d' ' -f1 access.log

# Everything except one field
cut -d: --complement -f2 /etc/passwd

# Keep only lines that actually contain the delimiter
cut -d: -s -f2 mixed.txt

# Change the output delimiter
cut -d: -f1,7 --output-delimiter=' -> ' /etc/passwd

# Output a tab instead of the input delimiter
cut -d, -f1,3 --output-delimiter=$'\t' data.csv

# First 10 characters of every line
cut -c1-10 /var/log/syslog

# A single character position
cut -c1 flags.txt

# From character 5 to the end
cut -c5- ids.txt

# The last part of a fixed-width line
cut -c41-60 report.txt

# Several character ranges at once
cut -c1-4,10-12 fixed-width.txt

# Bytes instead of characters, which differs for UTF-8
cut -b1-10 utf8.txt

# Just the date from a syslog line
cut -c1-15 /var/log/syslog

# Usernames and shells, to spot accounts that can log in
cut -d: -f1,7 /etc/passwd | grep -v nologin

# Every user's home directory
cut -d: -f6 /etc/passwd

# Group names
cut -d: -f1 /etc/group

# Requested paths from an access log
cut -d'"' -f2 /var/log/nginx/access.log

# Client IP from an access log
cut -d' ' -f1 /var/log/nginx/access.log

# Count requests per client IP
cut -d' ' -f1 /var/log/nginx/access.log | sort | uniq -c | sort -rn | head

# Read from a pipe
ps aux | cut -c1-80

# Take a field out of command output
df -h | cut -d' ' -f1

# Host part of an email address
cut -d@ -f2 emails.txt

# Strip the extension from file names
ls *.log | cut -d. -f1

# Read the field list from a variable
fields=1,3; cut -d: -f"$fields" /etc/passwd

# When fields are separated by runs of spaces, awk is the better tool
awk '{print $1, $3}' report.txt
