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 --output-delimiter changes the separator in the columns you pick

Linux
~/tips/tip-cut-output-delimiter

echo 'root:x:0:0:root:/root:/bin/bash' | cut -d: -f1,7 --output-delimiter=' -> '

Prints the fields you select with a new separator instead of the one in the input.

cut is usually the first tool I use to pick columns, and its one annoyance is that the separator comes out unchanged. A colon in, a colon out. There is an option for that.

echo 'root:x:0:0:root:/root:/bin/bash' | cut -d: -f1,7 --output-delimiter=' -> '

The manual says the default is to use the input delimiter for the output. --output-delimiter=STRING uses your string instead, and it can be longer than one character. On coreutils 9.4 this printed:

root -> /bin/bash

Without the option, the same command printed root:/bin/bash.

That makes it a quick way to turn a colon file such as /etc/passwd into something a spreadsheet or another script reads easily. A comma works the same way:

cut -d: -f1,3 --output-delimiter=, /etc/passwd

Two things I’d watch for. Fields come out in the order they appear in the input, not the order you list them. cut -d: -f7,1 printed root:/bin/bash, the same as -f1,7. If you need to reorder columns, use awk.

The other is the short form. The current manual lists -O for this option, but coreutils 9.4 answered invalid option -- 'O'. The long form worked on that version, so use it in scripts that may run on an older system.

One caveat: the new separator is only inserted between the selected fields. If a field itself contains your new separator, the output is ambiguous, so pick a string that does not occur in the data.

Further reading: cut(1) manual, GNU coreutils: cut invocation

More Linux tips notify-send -r "$id" "Backup" "Done" pgrep -af 'sleep 301' smbstatus -L msmtp --serverinfo --tls --tls-certcheck=off --host=smtp.example.com --port=587 smbclient //server/share -U user -Tc backup.tar "*" All Linux tips (283) All topics

Link