You are previewing Expert Shell Scripting.

Expert Shell Scripting

Cover of Expert Shell Scripting by Ron Peters Published by Apress
  1. Title Page
  2. Contents at a Glance
  3. Contents
  4. About the Author
  5. About the Technical Reviewer
  6. Acknowledgments
  7. Introduction
    1. How This Book Came About
    2. Who Should Read This Book
    3. Issues and Ideas
  8. PART 1: Basic Scripting Techniques
    1. CHAPTER 1: Shell Script Debugging
      1. Shell Trace Options
      2. Simple Output Statements
      3. Controlling Output with Debug Levels
      4. Simplifying Error Checking with a Function
      5. Manual Stepping
    2. CHAPTER 2: Standard Function Library
      1. The Library File
      2. Some Useful Functions
      3. Using Your Library
    3. CHAPTER 3: Date and Time Manipulation
      1. Date in Days
      2. Evaluating for the Current Day and Time
    4. CHAPTER 4: Comparisons and Tests
      1. The Basics of Comparisons
    5. CHAPTER 5: Accepting Command-Line Options, Switches, and Parameters
    6. CHAPTER 6: Testing Variables and Assigning Defaults
      1. Setting Defaults
      2. Variable Substitution
    7. CHAPTER 7: Indirect Reference Variables
      1. Log File Monitoring with Indirect Variables
      2. The Main Monitor Loop
    8. CHAPTER 8: Shell Process Tree
      1. Process Tree Implemented Using Arrays
      2. Process Tree Implemented Using Indirect Variables
      3. Bourne Shell Implementation of a Process Tree
    9. CHAPTER 9: Data Redirection
      1. Avoiding Confusion
      2. Common Redirection
      3. Access to User-Specified File Handles
      4. Descriptor Access from the Shell
    10. CHAPTER 10: Piping Input to read
      1. Line-by-Line Option 1
      2. Line-by-Line Option 2
      3. Line-by-Line Option 3
      4. Line-by-Line Option 4
      5. Pipe to read Directly
      6. Process Input Word-by-Word
  9. PART 2: System Interaction and Advanced Techniques
    1. CHAPTER 11: Math from the Shell
      1. expr
      2. Internal Shell Math
      3. bc
      4. dc
    2. CHAPTER 12: cron
      1. crontab Entries
      2. Environment Problems
      3. Output Redirection
    3. CHAPTER 13: Self-Linked Scripts
    4. CHAPTER 14: Throttling Parallel Processes
      1. Parallel Processing with ksh
      2. Parallel Processing with bash
    5. CHAPTER 15: Command-Line Editing and History
      1. Setting Up vi Editing
      2. Command and File Completion
    6. CHAPTER 16: Scripting from the Command Line
      1. A Few Examples
    7. CHAPTER 17: Automating User Input with expect
      1. A Shell Script to Customize Parameters for an expect Script
      2. An expect Script to Automate telnet
    8. CHAPTER 18: User Input Timeout
      1. Manual Timeout Method
      2. Timeout Using stty
      3. General Timeout Utility
    9. CHAPTER 19: Instant Keyboard Response
    10. CHAPTER 20: Directory Copying
      1. Using cp
      2. Using tar
      3. Using find
      4. Using rsync
    11. CHAPTER 21: A Brief Tour of the X Display Environment
      1. The Display
      2. X Traffic Through ssh
      3. X Applications Through a Third-Party System
      4. User-Profile Entry
      5. Root-Profile Entry
      6. Throw a Temporary Root Window
    12. CHAPTER 22: X Navigation Window
      1. Navigation Window Usage
      2. Navigation Setup
      3. Navigation Window
    13. CHAPTER 23: Command-Line E-mail Attachments
      1. uuencode
      2. MIME Encoding
    14. CHAPTER 24: Text-Processing One-Liners
      1. Displaying Specific Fields
      2. Specifying the Field Separator
      3. Simple Pattern-Matching
      4. Matching Fields Against Several Values
      5. Determining the Number of Fields
      6. Determining the Last Field
      7. Determining the Second-to-Last Field
      8. Passing Variables to awk
      9. Using a Variable Passed to awk in a Condition
      10. Displaying a Range of Fields (Main Method)
      11. Displaying a Range of Fields (Alternate Method)
      12. Determining the Length of a String Using awk
      13. Determining the Length of a String Using expr
      14. Displaying a Substring with awk
      15. Displaying a Substring with expr
      16. Conducting Simple Search and Replace with sed
      17. Disregarding Blank and Commented Lines from a File
      18. Conducting Dual Search and Replace with sed
      19. Filtering Lines with sed
      20. Searching for Multiple Strings with egrep
      21. A Clean Method of Searching the Process Table
      22. Summing Columns Using awk
      23. Generating Random Numbers Using awk
      24. Generating Random Numbers from the Shell
      25. Displaying Character-Based Fields with sed
      26. Escaping Special Characters
      27. Returning Trailing Lines from a Pattern Match Using grep
      28. Returning Preceding Lines to a Pattern Match Using grep
    15. CHAPTER 25: Editing Files in Place
      1. Simple Search and Replace with ed
      2. Search and Replace Using ed, Dissected
      3. Examples of ed Commands
      4. Escaping Special Characters in a File
    16. CHAPTER 26: Evaluating Variables in a Flat File
    17. CHAPTER 27: Read Piped Input
    18. CHAPTER 28: Free-Format Output Using cat
    19. CHAPTER 29: Automating Interactive Processes
  10. PART 3: Useful Scripts
    1. CHAPTER 30: Automating E-mail with procmail
      1. The .procmailrc File
      2. Usage Examples
      3. The Code
    2. CHAPTER 31: Process-Management Monitor
    3. CHAPTER 32: Managing File Counts
      1. File-Count Monitor
      2. Testing File-Count Methods
    4. CHAPTER 33: Processes Running from inittab
    5. CHAPTER 34: Automatic RCS
    6. CHAPTER 35: Colorful /proc Reporting
    7. CHAPTER 36: Password-Aging Notification
      1. Script Initialization
      2. Processing Begins
      3. Determine Password Age
    8. CHAPTER 37: A Pseudo–shadow File
    9. CHAPTER 38: Linux Gold-System Build
    10. CHAPTER 39: System Snapshots
      1. Snapshot Script
      2. Snapshot Promotion
      3. Creating the Latest Snapshot
      4. Final Thoughts
    11. CHAPTER 40: Removing Large Files and Log Rolling
    12. CHAPTER 41: Core Finder
    13. CHAPTER 42: Network Adapter Failover
      1. Check the Network
      2. Switch the Interfaces
    14. APPENDIX A: Test Switches
    15. APPENDIX B: Special Parameters
    16. APPENDIX C: Other Shell-Scripting Resources
      1. Manual Pages
      2. Books
      3. Shell Resources
      4. Online Resources
  11. INDEX
O'Reilly logo

CHAPTER 24Text-Processing One-Liners

Even though this book is about using the shell's command language, I use a fair number of calls to other utilities for text processing. Sed, awk, and grep are the primary UNIX text-processing utilities, although I have used others. This chapter gives you a collection of short and useful one-liners that illustrate quite a few methods for gathering specific information from various textual sources.

Very often when writing a script, you need to know source data locations before you start pruning the data for further processing. For instance, you can find the load average of a running Linux system from the first line of the output of the top utility, the output of the uptime command, the output of the w command, ...

The best content for your career. Discover unlimited learning on demand for around $1/day.