Thursday·25·November·2010
Useful but Unknown Unix Tools: cut //at 15:55 //by abe
Just recently someone asked on #debian.de how to extract the 3rd to 8th character out of some string. That reminded me that the GNU Coreutils tool “cut” is also quite unknown.
The solution to his problem is:
$ echo Hurzlipuh | cut -c3-8 rzlipu
My favourite usage of “cut” is to restrict the output of programs which can’t do that themselves to 80 columns (common terminal width):
$ dpkg -l | grep -i macro | cut -c-80 ii docbook-to-man 1:2.0.0-28 ii latex-cjk-common 4.8.2+git20090105-5 ii m4 1.4.14-3 ii mp4h 1.3.1-5 rc swfdec-gnome 2.28.0-1
cut can cut out columns based on characters (-c), bytes (-b), fields (-f) delimited by tab (or the delimiter given by -d).
After many comments: Yes, “dpkg -l” is capable of restricting and even
fitting its output to the number of available columns if $COLUMNS is
passed as environment variable. (And $COLUMNS is set by default in the
shell, but not exported as environment variable as it may
change on terminal resize or WINCH signal.)
Tagged as: #debian.de, cut, GNU Coreutils, UUUT
4 comments // show without comments // write a comment