What Is A Filter I Unix
Intro to Unix
Pipes and Filters
The purpose of this lesson is to introduce you to the mode that you tin construct powerful Unix command lines by combining Unix commands.
Concepts
Unix commands alone are powerful, but when you combine them together, yous tin can accomplish complex tasks with ease. The way you lot combine Unix commands is through using pipes and filters.
Using a Pipe
The symbol | is the Unix piping symbol that is used on the command line. What it ways is that the standard output of the command to the left of the pipe gets sent equally standard input of the command to the right of the piping. Note that this functions a lot like the > symbol used to redirect the standard output of a command to a file. However, the pipage is unlike considering information technology is used to pass the output of a command to some other command, not a file.
Here is an example:
$ true cat apple tree.txt cadre worm seed precious stone $ cat apple.txt | wc iii four 21 $
In this example, at the first shell prompt, I show the contents of the file apple.txt to you. In the side by side shell prompt, I utilize the cat command to display the contents of the apple tree.txt file, but I sent the display non to the screen, but through a pipage to the wc (give-and-take count) control. The wc command then does its chore and counts the lines, words, and characters of what it got equally input.
Yous can combine many commands with pipes on a single command line. Hither'southward an example where I count the characters, words, and lines of the apple.txt file, then post the results to nobody@december.com with the subject line "The count."
$ cat apple tree.txt | wc | mail -s "The count" nobody@december.com
Using a Filter
A filter is a Unix control that does some manipulation of the text of a file. Two of the most powerful and popular Unix filters are the sed and awk commands. Both of these commands are extremely powerful and complex.
sed
Here is a simple way to use the sed control to manipulate the contents of the apple.txt file:
$ cat apple.txt core worm seed jewel $ cat apple.txt | sed -eastward "southward/e/Www/" corWWW worm sWWWed jWWWwel $ cat apple.txt | sed -e "due south/e/J/g" corJ worm sJJd jJwJl $
In this example, at the outset shell prompt, I showed yous the contents of the apple.txt file. At the second vanquish prompt, I used the true cat command to display the contents of the apple.txt file, and send that display through a pipe to the sed control. The sed control I created changed the start occurrence of the letter "eastward" on each line to "Www." The sed took as input the data it got through the pipe. The sed command displayed its output to the screen.
I then used the output of the cat command on the apple.txt file and sent it by a pipe to the sed command to change all the occurrences of an e on each line with J. Note that every occurence of e, even where there were more than than one on a line, changed to J. This is considering of the "g" on the end of the sed pick value cord. This "g" stands for global replace.
It is important to note that, in this example, the contents of the apple.txt file itself were non changed in the file. Only the display of its contents changed.
awk
The Unix command awk is some other powerful filter. Yous can use awk to manipulate the contents of a file. Here is an example:
$ true cat basket.txt Layer1 = cloth Layer2 = strawberries Layer3 = fish Layer4 = chocolate Layer5 = punch cards $ cat handbasket.txt | awk -F= '{print $1}' Layer1 Layer2 Layer3 Layer4 Layer5 $ cat handbasket.txt | awk -F= '{print "HAS: " $2}' HAS: cloth HAS: strawberries HAS: fish HAS: chocolate HAS: punch cards $
In this example, I first showed you the contents of the basket.txt file. Then I displayed the contents and sent the output through a pipe to the awk control. I set up the awk command to display the first give-and-take on each line that comes before the = sign.
Then I did something a bit unlike. I used the true cat control to display basket.txt and so transport that output through a piping to awk, just this time appending the characters HAS: to the get-go of every ouput line followed past the second word on each line in basket.txt, because = as the separator betwixt words on a line in basket.txt.
grep
The Unix grep command helps you search for strings in a file. Hither is how I tin can find the lines that comprise the string "jewel" and display those lines to the standard output:
$ cat apple.txt core worm seed jewel $ grep jewel apple.txt jewel $
Do: Try out some pipes and filters
Create a simple text file that contains words and symbols. Try using combinations of the pipe symbol, cat, grep, awk, and sed commands to manipulate its contents.
What Is A Filter I Unix,
Source: https://www.december.com/unix/tutor/pipesfilters.html
Posted by: blairroyes1951.blogspot.com
0 Response to "What Is A Filter I Unix"
Post a Comment