Linux Classes
Linux Classes
Share This With a Friend
LINUX CLASSES - DATA MANIPULATION

Will Pipe Fitting Bring About World Peace?

Throughout this section, we've discussed how to manipulate a file with many different tools. But you can use each of these tools in a more powerful way by combining them into pipelines. Back in "Living in a Shell" you learned how to pump the output from one command to another by redirecting the input or output of those commands. Following are several examples that show how to combine the power of the tools described in this section.

To find files that have not been accessed for over 30 days and print the first five lines of each:

find . -atime +30 -exec head -5 {} \;

To find out if a process named netscape is running:

ps | grep netscape

To print only the second and third lines of a file:

head -3 some.file | tail -2

Note that the usage changes slightly when a command is in the second or subsequent stages of a pipeline. No input file is specified, because the previous stage feeds the command.

At the beginning of this section, I said that it would be no problem to search within a bunch of files, pull out all lines that contain a certain keyword, sort those lines, eliminate duplicates, and then print just the third column of each line. Here's proof that you can do it all on one line:

grep 'stuff' *.data | sort +1 -2 | uniq | cut -f3

Seems almost too easy, doesn't it? Beats the heck out of writing a program several hundred lines long if you want to run it only once! Now let's use the rest of the commands from this section in another pipeline. Start by creating the file odds.ends containing the lines shown here:

Ford Cat 47
IBM Lion 152
Xerox Slug 31
Zenith Bear 26
Intel Cat 133
Hershey Lynx 28
Apple Panda 74

Then execute the following command. (The backslash at the end of a line tells the shell that you are continuing a command on the next line.) Can you figure out what the output will be?

head -5 odds.ends | sed s/Cat/Tigger/g | \
awk /Tigger/'{print "Buy",1,ドル"from",2,ドル"at",3ドル}' | \
tail -1

The correct answer is "Buy Intel from Tigger at 133"--can you prove it?

Previous Lesson: Finding Files
Next Lesson: Linux Shell Scripts

[ RETURN TO INDEX ]



Comments - most recent first
(Please feel free to answer questions posted by others!)

Niamul BAqui (25 Mar 2012, 15:17)
Thank you
Amit D (11 May 2011, 09:21)
Good one
Newbie (19 Nov 2010, 15:37)
Thank you!

I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But don't ask me to send what you already have. :-)

NO SPAM! If you post garbage, it will be deleted, and you will be banned.
*Name:
Email:
Notify me about new comments on this page
Hide my email
*Text:




Copyright © by - Privacy Policy
All rights reserved - Redistribution is allowed only with permission.

Popular Linux Topics

Linux Intro
Linux Files
Linux Commands
Change Password
Copy Files
Linux Shell Basics

Linux Tutorial

Who is Doctor Bob?
What is Linux?
History of Unix
Operating Systems
What's Next?

Linux Basics

Living in a Shell
Root and Other Users
Virtual Consoles
Logoff and Shutdown
Choosing a Shell
The Command Prompt
Wildcards
Command History
Aliases
Redirection
Pipelines
Processes
Stopping a Program
Environment Variables
Help!

Linux Files

The Linux File System
Linux File Names
Linux Directories
Directory Terminology
Navigating the File System
Listing Linux Files
Displaying Linux Files
Copying and Renaming Files
Creating Files and Directories
Deleting Files and Directories
Linux Files - Wildcards
The Nine Deadly Keystrokes
Linux File Permissions
Changing File Permissions

Linux Commands

Important Linux Commands
Changing Your Password
Switching Users
Who is Logged In?
Date and Time
The Echo Command
Spell Checking
Printing Linux Files
Joining Files
Searching for Files
Comparing Files
Task Scheduling
Linking Files

Linux Editors

The Vi Editor
The Emacs Editor
The Pico Editor

Linux Data Manipulation

Slicing & Dicing
Heads or Tails?
Sorting Data
Eliminating Duplicates
Selecting Columns
Selecting Records
Search & Replace
Crunching Data
Finding Files
Pipe Fitting

Linux Shell Programming

Linux Shell Scripts
Executing a Script
Shell Script Variables
Shell Script Logic
Shell Script Looping
Shell Script Debugging

Perl Programming

Perl Basics
Perl Variables
Perl Arguments
Perl Logic
Perl Looping
Perl and Files
Perl Pattern Matching

Linux and Email

Sending Email
Reading Email
Other Mail Commands
Using Pine for Email
The Pine Inbox
Pine Email Basics
Pine Email Folders
Pine for Power Users

Compression and Encoding

Linux File Compression
Archiving With Tar
Compression With Gzip
Compress and Zcat
Zmore and Zless
Zip and Unzip
Encoding and Decoding
Encryption

Linux Does DOS

Accesing DOS Files
Accesing DOS Partitions
Running DOS Programs

Managing Linux

Updating Your Linux System
Installing Packages with RPM
Uninstalling Packages w/ RPM
Upgrading Packages with RPM
Querying Packages with RPM

AltStyle によって変換されたページ (->オリジナル) /