Working with Files and Text

Basics

Part 0 : A word of caution

Using globs at the command line is very powerful…

And with great power comes great responsibility.

Part 1 : Plain Text Files

Plain text files

Looking at file contents

There are several commands for looking at the content of files.

WARNING

If you enter any of the commands without a filename, they will go into “stdin mode”

$ cat

You have to press Ctr-C

Demo - file contents

demos/01-fine_contents.sh

The find command

find takes a directory named as an argument followed by options

Demo - find

demos/02-find.sh

Part 2 : Sending Output to a File

File redirection

Demo - File Redirection

demos/03-file_redirection.sh

Part 3 : Text Editors

Text Editors

So far we have just been looking at and creating plain text files. What if we want to edit them?

Demo - Text Editors

demos/04-text_editors.sh

Part 4 : Inserting Output into the Command Line

Command Substitution

Demo - Command Substitution

demos/05-command_substitution.sh

One last thing…

Manual pages

$ man grep

Practice Problems

  1. The sandbox directory contains a directory named simulations/, which contains a directory name backups.201601/. Go into this directory
    1. What is the 5th number in the second column of the file named data-01.txt?
    2. What is the second to last number in the second column of the file named data-02.txt?
    3. Which file in this directory has the most lines?
    4. Create a file named data-XX.txt that contains the lines from all the data files in this directory
    5. How many lines does the file you just created contain?
    6. Create a file named data-last_lines.txt that contains just the last line from all the data files in this directory
  2. Go into the simulations/ directory of the sandbox.
    1. The directory contains several files that are named run-XXX.out. These files contain the output of some program that was used to processed data. Some of the runs “failed”, i.e. there was an error during the processing. When this happened, the processor printed the word “ERROR”, which is contained in the file. How many of the run-XXX.out files contain error messages?
    2. Create a list of the files that have error messages in them and how many error messages they have. Save this list to a file named errors.txt.
    3. The data processor also printed a warning if only a few data points were found during processing. In this case, the processor printed “WARNING” to the file. Create a list of all the files that contain warnings and/or errors named warnings_and_errors.txt