From the computer of: qmchenry
(339 recipes)
Created: Oct 06, 2003
Many data files have data fields delimited by a single character like a tab or colon. To extract the full name field out of /etc/passwd, the fifth colon-delimited field, use:
The cut command allows a great deal of flexibility in cutting data. In this case, the -d : directs cut to use a colon character as the delimiter. The -f 5 parameter directs cut to extract only the fifth field. The field parameter makes cut extrememly flexible. Other examples are -f 2-5 to extract fields 2 through 5, -f 1,3,7 to extract the first, third, and seventh fields.
To extract a fixed set of columns, for example the column numbers 44 through 49 from a long directory listing (ls -l), use the following command:
On many UNIX systems, these columns represent the modification date. Like the -f parameter, the -c parameter can accept alternative values such as -c 5,7,6,8 will present those character positions in that order.
One of the trickier column extraction involves the presence of a variable amount of whitespace between fields. To extract the process id (second) field from a process listing (ps -ef), cut will not work. Another powerful text manipulator in UNIX is awk which understands that several spaces should be counted as a single whitespace. To extract the pid from a ps -ef, use:
Awk is an incredibly powerful tool, and this is a trivial but useful application of it.
Subscribe to the Tech-Recipes Newsletter
You can get tips like this delivered in your email every week!
We will never, ever sell your email address or spam you.
2 Recipe comments: View comments
Select an arbitrary column of text in UNIX by bwhitehead
Re: Select an arbitrary column of text in UNIX by lloydsj
Related recipes:
Create/modify a UNIX file with an arbitrary timestamp
Convert Unix lines breaks to Macintosh line breaks
Remove ^M characters at end of lines in vi
Delete Solaris files older than a certain date and time
Solve your UNIX identity crisis
How to empty or clear the contents of an existing UNIX file
Find help with man pages
Unix/Linux: Easily find or search files or directories
Convert Macintosh line breaks to Unix line breaks
Convert DOS line breaks to Unix line breaks
Sponsored links
Login
Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.Search
Tech Recipes
· Home
· Mac OS X
· Solaris
· Windows
· Programming
· UNIX
· Cisco
· MySQL
· Google
· Instant messaging
· Add recipes
· Leave feedback
· Recommend us
· RSS Syndication
Hot recipes
Make XP look like Windows Vista / Longhorn for free!
Hacks to Beat Rapidshare Download Limits and Waiting Time
XP: Small, Free Way to Use and Mount Images (ISO files) Without Burning Them
Vista: Should I Install 64-bit or 32-bit Version? (x64 vs x86)
How Do I Use or Open Bin, Cue, or ISO Files?
MySpace Hack: View Pictures and Comments on a Private Profile
MySpace: Hack to Dowload Any Song on Myspace
How to uninstall IE8
Who's Online
There are currently, 1763 guest(s) and 1 member(s) that are online.
You are Anonymous user. You can register for free by clicking here
Information
Wish to advertise with us?
All logos and trademarks in this site are property of their respective owner. The comments and forum posts are property of their posters, all the rest © 2003-2007 by QD Ideas, LLC.
Users of this site are legally bound by the Terms and conditions and Disclaimer. Do not use this web site if you do not agree with these policies.
The members, admins, and authors of this website respect your privacy.
Page Generation: 0.86 Seconds
Fri Aug 29 4:26:01 2008