Stoppt die Vorratsdatenspeicherung! Jetzt klicken &handeln! Willst du auch an der Aktion teilnehmen? Hier findest du alle relevanten Infos und Materialien:
Jump to menu and information about this site.

Thursday·25·November·2010

Useful but Unknown Unix Tools: cut //at 15:55 //by abe

from the the-first-cut-is-the-deepest dept.

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.)

Comments

Re: Useful but Unknown Unix Tools: cut

Posted by: Buck
Website: 
Time: Thu, 25 Nov 2010 03:46

the one i always see is

uname -n | cut -d. -f1

then there's

getent {passwd,group} name | cut -d: -f...

but all the cool kids probably do like

ruby -retc -e 'puts Etc.getpwnam(...).dir'

nowadays

Reply

Re: Useful but Unknown Unix Tools: cut

Posted by: Iñigo
Website: http://poisonbit.wordpress.com
Time: Thu, 25 Nov 2010 11:05

@abe: cut -c3-8 <<< Hurzlipuh

@ Buck: uname -n | cut -d. -f1 can be write as: hostname -s

getent {passwd,group} name | cut -d: -f1 can be write without non builtins: IFS=: read user _ <<< "$( getent {passwd,group} name )"; echo "$user"

ruby -retc -e 'puts Etc.getpwnam(...).dir' not so cool, but Perl kids does the same since 20 years ago, in backward compatible (an more than one) way :) perl -E '$dir = (getpwnam ...)[7]; say $dir;'

Greetings, and note that all these "unix tools" related posts in planet Debian, are very appreciated. Iñigo

Reply

Re: Useful but Unknown Unix Tools: cut

Posted by: Ac
Website: 
Time: Thu, 25 Nov 2010 23:35

coreutils is just a goldmine. Randomly browsing the man pages just now i found join, which would have been useful many times to me before, if I just had known about it.

Reply

Re: Useful but Unknown Unix Tools: cut

Posted by: Jörg Sommer
Website: mailto:joerg@alea.gnuu.de
Time: Sun, 28 Nov 2010 14:03

The field selector with the delimiter is really helpful:

apt-get -qq -y --print-uris dist-upgrade |cut -f2 -d\' |wget -i

Reply

Your Comment

Spam Protection: To post a comment, you'll have to answer the following question: What is 42 minus 19?

Name:
URL or E-Mail: [http://... or mailto:you@example.com] (optional)
Title: (optional)
Spam Protection Answer:
Comment:

Tag Cloud

2CV, aha, Apache, APT, aptitude, ASUS, Automobiles, autossh, Berlin, bijou, Blogging, Blosxom, Blosxom Plugin, Browser, BSD, CDU, Chemnitz, Citroën, CLI, CLT, Conkeror, CSS, CX, deb, Debian, Doofe Parteien, E-Mail, eBay, EeePC, Emacs, Epiphany, Etch, ETH Zürich, Events, Experimental, Firefox, Fläsch, FreeBSD, Freitagstexter, FVWM, Galeon, Gecko, git, GitHub, GNOME, GNU, GNU Coreutils, GNU Screen, Google, GPL, grep, grml, gzip, Hackerfunk, Hacks, Hardware, Heise, HTML, identi.ca, IRC, irssi, Jabber, JavaShit, Kazehakase, Lenny, Liferea, Linux, LinuxTag, LUGS, Lynx, maol, Meme, Microsoft, Mozilla, Music, mutt, Myon, München, nemo, Nokia, nuggets, Open Source, Opera, packaging, Pentium I, Perl, Planet Debian, Planet Symlink, Quiz, Rant, ratpoison, Religion, RIP, Sarcasm, Sarge, Schweiz, screen, Shell, Sid, Spam, Squeeze, SSH, Stoeckchen, Stöckchen, SuSE, Symlink, Symlink-Artikel, Tagging, Talk, taz, Text Mode, ThinkPad, Ubuntu, USA, USB, UUUCO, UUUT, VCFe, Ventilator, Vintage, Wahlen, Wheezy, Wikipedia, Windows, WML, Woody, WTF, X, Xen, zsh, Zürich, ÖPNV

Calendar

 2010 
Months
Nov
 November 
Mo Tu We Th Fr Sa Su
25
         

Tattletale Statistics

Blog postings by posting time
Blog posting times this month



Search


Advanced Search


Categories


Recent Postings

0 most recent of 0 postings total shown.


Recent Comments

Hackergotchi of Axel Beckert

About...

This is the blog or weblog of Axel Stefan Beckert (aka abe or XTaran) who thought, he would never start blogging... (He also once thought, that there is no reason to switch to this new ugly Netscape thing because Mosaïc works fine. That was about 1996.) Well, times change...

He was born 1975 at Villingen-Schwenningen, made his Abitur at Schwäbisch Hall, studied Computer Science with minor Biology at University of Saarland at Saarbrücken (Germany) and now lives in Zürich (Switzerland), working at the Network Security Group (NSG) of the Central IT Services (Informatikdienste) at ETH Zurich.

Links to internal pages are orange, links to related pages are blue, links to external resources are green and links to Wikipedia articles, Internet Movie Database (IMDb) entries or similar resources are bordeaux. Times are CET respective CEST (which means GMT +0100 respective +0200).


RSS Feeds


Identity Archipelago


Picture Gallery


Button Futility

Valid XHTML Valid CSS
Valid RSS Any Browser
This content is licensed under a Creative Commons License (SA 3.0 DE). Some rights reserved. Hacker Emblem
Get Mozilla Firefox! Powered by Linux!
Typed with GNU Emacs Listed at Tux Mobil
XFN Friendly Button Maker

Blogroll

People I know personally


Other blogs I like or read


Independent News


Interesting Planets


Web comics I like and read

Stalled Web comics I liked


Blogging Software

Blosxom Plugins I use

Bedside Reading

Just read

  • Bastian Sick: Der Dativ ist dem Genitiv sein Tod (Teile 1-3)
  • Neil Gaiman and Terry Pratchett: Good Omens (borrowed from Ermel)

Currently Reading

  • Douglas R. Hofstadter: Gödel, Escher, Bach
  • Neil Gaiman: Keine Panik (borrowed from Ermel)

Yet to read

  • Neil Stephenson: Cryptonomicon (borrowed from Ermel)

Always a good snack

  • Wolfgang Stoffels: Lokomotivbau und Dampftechnik (borrowed from Ermel)
  • Beverly Cole: Trains — The Early Years (getty images)

Postponed