New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Start learning with our library of video tutorials taught by experts. Get started

Unix for Mac OS X Users
Illustration by

cut: Cutting select text portions


From:

Unix for Mac OS X Users

with Kevin Skoglund

Video: cut: Cutting select text portions

So far we've seen Unix tools that will let you find, translate, and replace. The next useful Unix tool I want to introduce you to is cut. Cut allows you to cut out selected portions of each line of a file. We could probably write a sed command that would do something similar to what cut does, but cut is much simpler and easier to use. The first thing you need to know about cut is that it can cut three things, characters, bytes, or fields, and we are always going to need to pick one of those. We are only going to be looking at characters and fields, and the reason why is that bytes in English is going to be exactly equivalent to characters, because every character takes up exactly 1 byte.
Expand all | Collapse all
  1. 3m 57s
    1. Introduction
      1m 14s
    2. Using the exercise files
      2m 43s
  2. 32m 2s
    1. What is Unix?
      7m 27s
    2. The terminal application
      4m 23s
    3. Logging in and using the command prompt
      5m 19s
    4. Command structure
      5m 22s
    5. Kernel and shells
      5m 25s
    6. Unix manual pages
      4m 6s
  3. 15m 58s
    1. The working directory
      2m 49s
    2. Listing files and directories
      3m 59s
    3. Moving around the filesystem
      4m 58s
    4. Filesystem organization
      4m 12s
  4. 1h 4m
    1. Naming files
      5m 41s
    2. Creating files
      2m 19s
    3. Unix text editors
      6m 39s
    4. Reading files
      5m 35s
    5. Reading portions of files
      3m 27s
    6. Creating directories
      2m 40s
    7. Moving and renaming files and directories
      8m 32s
    8. Copying files and directories
      3m 7s
    9. Deleting files and directories
      3m 38s
    10. Finder aliases in Unix
      4m 10s
    11. Hard links
      5m 30s
    12. Symbolic links
      6m 36s
    13. Searching for files and directories
      6m 32s
  5. 34m 58s
    1. Who am I?
      4m 3s
    2. Unix groups
      1m 52s
    3. File and directory ownership
      6m 41s
    4. File and directory permissions
      4m 27s
    5. Setting permissions using alpha notation
      6m 49s
    6. Setting permissions using octal notation
      3m 49s
    7. The root user
      1m 57s
    8. sudo and sudoers
      5m 20s
  6. 52m 34s
    1. Command basics
      4m 4s
    2. The PATH variable
      4m 13s
    3. System information commands
      3m 40s
    4. Disk information commands
      6m 8s
    5. Viewing processes
      5m 0s
    6. Monitoring processes
      3m 36s
    7. Stopping processes
      3m 19s
    8. Text file helpers
      6m 50s
    9. Utility programs
      7m 28s
    10. Using the command history
      8m 16s
  7. 20m 39s
    1. Standard input and standard output
      1m 24s
    2. Directing output to a file
      4m 13s
    3. Appending to a file
      2m 44s
    4. Directing input from a file
      5m 28s
    5. Piping output to input
      4m 40s
    6. Suppressing output
      2m 10s
  8. 41m 28s
    1. Profile, login, and resource files
      9m 11s
    2. Setting command aliases
      6m 59s
    3. Setting and exporting environment variables
      4m 54s
    4. Setting the PATH variable
      6m 10s
    5. Configuring history with variables
      6m 17s
    6. Customizing the command prompt
      6m 5s
    7. Logout file
      1m 52s
  9. 1h 25m
    1. grep: Searching for matching expressions
      5m 21s
    2. grep: Multiple files, other input
      4m 28s
    3. grep: Coloring matched text
      2m 57s
    4. Introduction to regular expressions
      3m 22s
    5. Regular expressions: Basic syntax
      3m 19s
    6. Using regular expressions with grep
      5m 20s
    7. tr: Translating characters
      8m 17s
    8. tr: Deleting and squeezing characters
      5m 30s
    9. sed: Stream editor
      7m 45s
    10. sed: Regular expressions and back-references
      7m 8s
    11. cut: Cutting select text portions
      7m 42s
    12. diff: Comparing files
      4m 35s
    13. diff: Alternative formats
      4m 30s
    14. xargs: Passing argument lists to commands
      7m 25s
    15. xargs: Usage examples
      7m 59s
  10. 42m 25s
    1. Finder integration
      4m 45s
    2. Clipboard integration
      5m 5s
    3. Screen capture
      3m 42s
    4. Shut down, reboot, and sleep
      3m 34s
    5. Text to speech
      2m 36s
    6. Spotlight integration: Searching metadata
      3m 41s
    7. Spotlight integration: Metadata attributes
      4m 24s
    8. Using AppleScript
      5m 23s
    9. System configurations: Viewing and setting
      5m 51s
    10. System configurations: Examples
      3m 24s
  11. 1m 26s
    1. Conclusion
      1m 26s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
Unix for Mac OS X Users
6h 35m Beginner Apr 29, 2011

Viewers: in countries Watching now:

Unix for Mac OS X Users unlocks the powerful capabilities of Unix that underlie Mac OS X, teaching how to use command-line syntax to perform common tasks such as file management, data entry, and text manipulation. The course teaches Unix from the ground up, starting with the basics of the command line and graduating to powerful, advanced tools like grep, sed, and xargs. The course shows how to enter commands in Terminal to create, move, copy, and delete files and folders; change file ownership and permissions; view and stop command and application processes; find and edit data within files; and use command-line shortcuts to speed up workflow. Exercise files accompany the course.

Topics include:
  • Moving around the file system
  • Creating and reading files
  • Copying, moving, renaming, and deleting files and directories
  • Creating hard links and symbolic links
  • Understanding user identity, file ownership, and sudo
  • Setting file permissions with alpha and octal notation
  • Changing the PATH variable
  • Using the command history
  • Directing input and output
  • Configuring the Unix working environment
  • Searching and replacing using grep and regular expressions
  • Manipulating text with tr, sed, and cut
  • Integrating with the Finder, Spotlight, and AppleScript
Subjects:
Developer Web
Software:
Mac OS X Unix
Author:
Kevin Skoglund

cut: Cutting select text portions

So far we've seen Unix tools that will let you find, translate, and replace. The next useful Unix tool I want to introduce you to is cut. Cut allows you to cut out selected portions of each line of a file. We could probably write a sed command that would do something similar to what cut does, but cut is much simpler and easier to use. The first thing you need to know about cut is that it can cut three things, characters, bytes, or fields, and we are always going to need to pick one of those. We are only going to be looking at characters and fields, and the reason why is that bytes in English is going to be exactly equivalent to characters, because every character takes up exactly 1 byte.

And really, bytes are there mostly for when you are thinking about raw data, not actual characters, but raw data, and you want to grab a certain amount of bytes out of it. We're working with text files here, so we're really thinking about characters, and most times that's what you will be doing. So characters and fields is where we're going to focus. So let's start with characters. Notice that I am inside my user directory, inside unix_files, and inside there I have this file that I created earlier called dir_content.txt. If we take a look at that file you will see that it's just a directory listing.

It looks very much like the output from ls-la, because that's exactly what I did, ls-la earlier, and I directed that output into a file. So this is not my current director listing. It's a snapshot of what it was previously. So here's the scenario. Imagine that we have a file like this and we say, you know what, I like the data here, but I really wish I could just grab a selected portion of it. For example, I really wish that I could grab these permissions out of here, just that line and that part of this line, all the way down, essentially grabbing a column.

Just wish I could grab that column of these permissions out of it. That's the purpose that cut serves. So we say cut and then we need to specify an option. Always have to specify an option. Either -c for characters, which is what we'll be doing, or -b for bytes, or -f for fields, which we will see in a moment. And then we tell it what characters we want to cut. Well, out of each line we'd like to cut characters 2-10. That's what represents these characters, starting with character 2, going until we get to character 10. And then we need to say the file that we want to do that from, and there it is. It's that easy.

Now, notice that it also grabbed this up here, total 144. That was the top line here. It went ahead and grabbed that as well. So be aware of that. You actually, I believe, can suppress the output of this from your directory listing if you needed to, but for our purposes, we just really want to illustrate the way that cut works. Now, let's say we wanted to cut something else out of this. We can cut more than one thing. We can grab more data. So in addition from cutting the permissions, let's say that we also wanted to keep the file size. So we wanted the permissions, followed by the file size, and then after that we will grab the file name as well.

So what we'll be left with is permissions, size, name. Everything else will end up being removed. So what we need to do is we look at the data here and you can see that this is the last column, so what we want to do is we want to find out how many characters is this? How many do I need to skip over essentially till I get to more data that I want? So I am going to use Command+C to copy that. Let's just do echo and we'll put that in quotes and pipe it into Word Count. Word Count tells me it's 21 characters. So now I know I need to skip over 21 characters, so 2-10, and then we'll do a comma, followed by skipping 21 characters starting with 10, so that would be 31.

Now, I would also like to leave a space between them, so I am actually going to keep the space in there before it. So that would be going to 30 and then I'll go up to 35. That will give me the space plus the four characters here that represent the size. So there we go, now I have the size. Let's say I also want to get the name, and it's here. I want to leave a space here so I'll keep this space in. So I want to know how wide is that? Copy it. echo it into Word Count. That's 14 characters.

So now I go back up here and I add 14 onto this and I come up with 49 to the end. If you want to go all the way to the end, you can just put a dash with nothing after it. You can do the same thing at the beginning too if you knew you wanted to get everything at the beginning. So now, look at that. I have sort of my own custom ls listing. We can take this same thing, let's copy it, and we can actually do that. ll, pipe through this cut statement, and look at that. I can see exactly the data I want. I can leave everything else out of there.

You could take, for example, your history file and let's grep that for everything that has fruit in it and then let's take that and let's pipe that into cut, and I am going to cut everything 24 characters to the end. So I have got just then the command itself. I left off everything that was at the beginning of it. Or we could take ps aux, your listing of all your processes, and let's cut 11-15 and 72-end. Now, I have just got the process ID and what's actually running? Everything else that's inside ps aux. pipe it so we don't see everything.

Now, all this stuff in the middle, that's all gone. I can really condense it down to just what I want to see, just the process ID and what is running. I leave out all those other stats. So that's how cut works. Cut also has this -f option that is really nice. I have a file here that I created earlier called us_presidents.tsv, tab-separated values. So it's just an information about U.S. Presidents that is tab separated values. You could use any other tab delimited file that you wanted to for this.

The tabs are important, and the reason why I say that is because if we use cut -f, by default what it's going to do is use those tabs to figure out where the columns are. So if I say I want fields 2, 6 out of us_presidents.tsv, look at that. I get just the column that has the President's name in it and then I get just the state that they're from. Notice that it also kept the tabs as delimiters between these. So it's great! You can have something that's like tab- separated values from a spreadsheet and you can just grab the columns out of it that you want.

We have something here which we can then save as another tab-separated file. So we can save this as presidents_states.tsv. And now we have this file saved that we can then continue to work with. We can now join that with other things, we can use it in sed scripts, whatever we want to do. We've just zeroed in on the data that we want. Now, I want to show you that we have another example of a file. I have us_presidents.csv and that's comma-separated values. We worked with that earlier as well.

It's the same thing, but instead of tabs we have commas between the data. If we tried the same thing that we did before but on the csv file, let me just clear this so you can see, using csv this time, asking for columns 2 and 6, it doesn't work. It gives me everything back, because it was expecting the tab to be the delimiter. If you want to change the delimiter, then you have to use the -d option. So -d and then tell it what delimiter is. So if it's a comma, now it does it.

Notice that it still kept the comma as the delimiter in the output as well, but the main thing is that this -d followed by what it should be looking for. By default it's tab. There are a couple of other options that it offers. There is a -s option and that would do nothing to lines that don't have delimiters. It would just let them through on their own. That's all there is to using cut. It's a fairly simple tool to use, but it's really quite powerful, especially when used in conjunction with a lot of the other Unix commands and techniques that we've learned.

Find answers to the most frequently asked questions about Unix for Mac OS X Users.


Expand all | Collapse all
please wait ...
Q: The exercise files for the following movies appear to be broken:
07_02_files
07_03_files
07_04_files
07_05_files
08_03_files

Is there something wrong with them?
These exercises include one or more "dot files", whose file names start with a period. These files are normally hidden from view by the Finder.  So that they would show up in the Finder, the period has been removed from the file names. Additionally, "_example" has been added at the end of the file name to make it clear that the file will not work as-is. To make the dot files usable, either:

1) Open the file in a text editor to view its contents. Note that it may not be possible to double-click the file to open it because there is no file extension (such as .txt).
2) Resave the file under a new name (usually by choosing File > Save As), adding a "." to the beginning of the file name and removing "_example" from the end.

OR

1) Copy and rename the file from the Unix command line using the techniques discussed in this course. Rename the file by adding a "." to the start and removing "_example" from the end. Include the "-i" option to prevent overwriting an existing file unexpectedly.
Example:  cp -i ~/Desktop/Exercise\ Files/Chapter_07/07_02_files/bashrc_example ~/.bashrc
 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Unix for Mac OS X Users.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.