brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: cut can drop a single column and keep all the others

Linux
~/tips/tip-linux-cut-complement

cut -d, -f2 --complement people.csv

Remove a single field from a CSV or log without listing every field you want to keep.

With cut you normally list the fields you want. When a file has twenty columns and only one of them is in the way, that list gets long and easy to get wrong.

GNU cut can turn the selection around:

cut -d, -f2 --complement people.csv

This prints every column except the second. So a,b,c,d becomes a,c,d, and the commas between the remaining fields stay where they belong. You can give a range or a list too, like -f2,5-7, and all of those disappear at once.

A use I like: stripping a column with personal data, such as an email address, from an export before you share it:

cut -d';' -f3 --complement export.csv > export-clean.csv

It works with character positions as well. cut -c1-20 --complement drops the first twenty characters of every line, which is a quick way to get rid of a fixed width timestamp at the start of a log.

The usual cut limits still apply. It splits on every single delimiter, so a CSV with quoted fields that contain commas will be cut in the wrong place. For files like that, a real CSV tool is safer. --complement is also a GNU option: BusyBox and the BSD cut on macOS do not know it.

Further reading: GNU coreutils: cut invocation

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link