iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

cut / tr

cut and tr: column extraction and character translation. The micro-tools that quietly do most of your text munging.

Linux — cut + tr

EXAMPLE
# ===== cut: extract columns / fields =====
# By byte:
cut -b 1-3 file               # bytes 1-3 of each line
cut -b 1,4,7 file              # specific bytes

# By character (same as -b for ASCII):
cut -c 1-10 file

# By field (with delimiter):
cut -d: -f1 /etc/passwd       # first field of colon-separated
cut -d, -f1,3 data.csv         # 1st and 3rd
cut -d, -f2-4 data.csv         # range
cut -d, -f3- data.csv          # 3rd to end

# Output delimiter:
cut -d: -f1,7 --output-delimiter=' ' /etc/passwd

# ===== tr: translate / squeeze / delete characters =====
# Replace:
echo 'hello' | tr 'a-z' 'A-Z'                  # HELLO
echo 'foo:bar:baz' | tr ':' ','                # foo,bar,baz

# Delete:
echo 'a 1 b 2 c 3' | tr -d '0-9'              # 'a  b  c '
echo 'hello\r\nworld' | tr -d '\r'           # remove carriage returns

# Squeeze repeating:
echo 'a    b   c' | tr -s ' '                  # 'a b c'

# Complement (translate everything except the set):
echo 'abc 123' | tr -dc 'a-zA-Z'               # 'abc'

# ===== Common combinations =====
# Extract usernames from /etc/passwd:
cut -d: -f1 /etc/passwd | sort

# Convert CSV to TSV:
cut -d, -f1,3 data.csv | tr ',' '\t'

# Strip \r\n line endings (Windows -> Unix):
tr -d '\r' < windows.txt > unix.txt

# Count unique extensions in current dir:
ls | grep '\.' | rev | cut -d. -f1 | rev | sort | uniq -c

# Sum a column:
cut -d, -f3 data.csv | tr -d '$,' | awk '{s+=$1} END {print s}'

# Top 5 IPs in an access log:
cut -d' ' -f1 access.log | sort | uniq -c | sort -rn | head -5

# ===== Cut limitations =====
# - One delimiter per call (use awk for multi-char or regex delimiters)
# - No quoted-field handling -> use 'csvkit' / 'mlr' / 'awk' for proper CSV

# Use mlr (miller) for proper CSV:
mlr --csv cut -f name,email data.csv
mlr --csv filter '$age > 30' data.csv

# ===== Patterns to internalise =====
# - cut -d X -f N for delimited fields
# - tr for character-level translate / delete / squeeze
# - tr -dc for keeping only certain characters
# - mlr / csvkit / awk when CSV has quoting

# ===== Pitfalls =====
# - cut with multi-char delimiter (use awk)
# - tr range on Unicode (works on bytes only; UTF-8 multibyte chars break)
# - Forgetting -d (default delimiter is TAB, not space)
# - Quoted CSV fields containing the delimiter -> cut splits incorrectly

Why it matters

cut + tr are tiny but indispensable: column slicing, character translation, deletion, squeezing. Reach for awk when fields need logic; reach for miller / csvkit when CSV has quoting. Most "I need to clean this text" tasks are a one-liner of cut and tr.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
cut -d, -f1,3 data.csv
tr 'a-z' 'A-Z' < file
Try it Yourself »

Discussion

Loading…