Skip to main content

Making Commands Powerful

pipes and tees

As I concern as these commands are most powerful Linux commands. You might heard about pipes and tees used for plumbing works. The tees and pipes used for diverge water into different direction. The pipe and tee commands will do the same job for you. The pipes and tees are used for take the output one command and apply it on another command.

pipe

Pipe
A pipe allows output from one command to be used as the input for another command. The pipe symbol is vertical line | . For example:

  • To show first ten entries of ls command it can be pipe through head command like ls | head.
If your working directory is Desktop,
cat /etc/network/interfaces | network_interfaces.txt
will create a new text file named network_intetfaces on your Desktop also it make a catalog on your terminal window. You can also try this command on other directories.

tees

Tee
Plumbers are using tees to diverge water flow in different directions. In Linux we are using tees to make the output of one command as the input of different command.

For example if you want two copies of network interfaces file you can use this command
cat /etc/network/interfaces |tee file.txt  file1.txt
 to create to text files containing two text files named file and file1. Also this make a catalog on your terminal window. If you are using Desktop as working directory you can see two text files(file.txt and file1.txt) poping out from Desktop immediately.

Using wildcards

A  wildcard is a character that can be used as a substitute for any of a class of characters in a search, thereby greatly increasing the flexibility and efficiency of searches.

Wildcards are commonly used in Linux and other Unix operating systems. The terminal is a text only user interface and whose main function is to execute commands typed in by users and display their results.

Wildcards are also used in regular expressions and programming languages. Regular expressions are a pattern matching system that uses strings (i.e., sequences of characters) constructed according to pre-defined syntax rules to find desired strings in text.

The term wildcard or wild card was originally used in card games to describe a card that can be assigned any value that its holder desires. However, its usage has spread so that it is now used to describe an unknown or unpredictable factor in a variety of fields.

Star wildcard

Three types of wildcards are used with Linux commands. The most frequently employed and usually the most useful is the star wildcard, which is the same as an asterisk (*). The star wildcard has the broadest meaning of any of the wildcards, as it can represent zero characters, all single characters or any string.
As I mentioned earlier a wild card can represent any string, we can use wildcards even as the extension of file. For example we can use Raspberry.* to represent any type of file that has name Raspberry. If your directory is containing following files Raspberry.jpg, Raspberry.php, Raspberry.py, Raspberry.py, Raspberry.html etc. You can represent all these files only by using Raspberry.* . It is very useful to manipulate the file with same name.

Also it useful to manipulate files with similar names for example, text1.txt, text2.txt, text3.txt.... text100.txt. All these 100 text file can be manipulated only using text*.txt.

Question Mark Wildcard

The question mark (?) is used as a wildcard character in shell commands to represent exactly one character, which can be any single character. Thus, two question marks in succession would represent any two characters in succession, and three question marks in succession would represent any string consisting of three characters.

Thus, for example, the following would return data on all objects in the current directory whose names, inclusive of any extensions, are exactly three characters in length:
ls text?.txt
from the above example will list the 100 text files. Also from the above example ls Raspberry.??? will lists the all kind of files thats extension has only 3 letters. From above example it lists Raspberry.jpg and Raspberry.png only.

The question mark wildcard can also be used in combination with other wildcards when separated by some other character. For example, the following would return a list of all files in the current directory that have a three-character filename extension:
ls *.???

Square Brackets Wildcard



The square bracket [ is interpreted by the shell as a sign to generate filenames, matching any of the characters between [ and the first subsequent ]. The order in this list between the brackets is not important. Each pair of brackets is replaced by exactly one character.

Thus, for example, the following would provide information about all objects in the current directory that have an j, p and/or t in them:
Raspberry*[jpt]*

And the following would list all files that had an extension that begins with j, p or t:
ls *.[jpt]*

The same results can be achieved by merely using the star and question mark wildcards. However, it is clearly more efficient to use the bracket wildcard.

The bash shell will also understand ranges of characters between brackets. When a hyphen is used between two characters in the square brackets wildcard, it indicates a range inclusive of those two characters. For example, the following would provide information about all of the objects in the current directory that begin with any letter from a through f and trying to list following file and observe what is going to happen:
ls Raspberry[a-f]*.*

Try above example with [a-z] and observe result. And the following would provide information about every object in the current directory whose name includes at least one numeral:
ls text[0-9].*

Lists all the 9 files text1.txt to text9.txt. Try the above command with:
ls text[0-100].*

Discussion


How can I store result of a command?

The result of a command can be stored using as explained above using pipes and tees. There is another way to store result of a command as follows:

cat /etc/network/interfaces > network.txt

The > symbol is known as stdout of I/O redirection. You can see that if you redirect output to network.txt again, it will overwrite your network.txt file. So use >> instead > to append output of another command to network.txt.

Comments

Popular posts from this blog

8051 Simple Programs

Sum of 8-bit Numbers Stored in Memory Here  we will discuss about 8051 programs but we can't discuss about all of the 8051 instructions. For programming 8051 we should know about assembler directives as well as instruction set. Click  here to download Atmel c51 user guide that will discuss about 8051(c51 family microcontroller) instruction set, assembler directives, c51 cross assembler from Atmel.  Program ORG 00H MOV R0,#50H   ; get memory location in memory pointer R0 MOV R1,#51H   ; get memory location on memory pointer register R1 MOV A,@R0       ; get content of memory location 50H to accumulator ADD A,@R1        ; add content of A with content of memory location 51H and store result in A MOV R0,#52H    ; get 52H to memory pointer R0 MOV@R0,A         ; copy content of A to memory location 52H END Add 16-bit Numbers ...

Introduction to 8051 embedded C

For 8051 we need to include the file reg51.h. This file contains the all the definitions of 8051 registers. With this information C compiler produces hex file that can be downloaded into the ROM of the microcontroller. It is important to note that the size of the hex file produced by the assembly language is much larger than the hex file produced by C compiler. Apart from this fact, there is many reasons for writing programs in C instead of assembly: ●It is much easier and less time consuming to write programs in C assembly. ●C is more flexible; it is easier to modify and update. ●Programming in C allows to use code available in function libraries. ●Program written inC for one microcontroller is portable to other microcontrollers with little or no modifications. Data Types in 8051 Embedded C The table shown below lists the data types that are available in typical C51 compiler. The gives information about the size of the data variable and it's value range. Data type ...

Frequency of Oscillation of RC Phase Shift Oscillator

Derivation of Frequency of Oscillation We have to find out the transfer function of RC feedback network. Feedback Circuit of RC Phase Shift Oscillator Applying KVL to various loops on the figure, we get, $$I_1 \left(R+\frac{1}{j \omega C }\right) -I_2R=V_i \text{ ....(1)}$$ $$-I_1R+I_2\left (2R+\frac {1}{j\omega C}\right)-I_3R=0\text{ ... (2)}$$ $$0-I_2R+I_3\left(2R+ \frac{1}{j\omega C}\right)=0\text{ ...(3)}$$ Replacing \(j\omega\) with \(s\) and writing equations in the matrix form, $$\begin{bmatrix}R+\frac{1}{sC} & -R & 0 \\-R & 2R+\frac{1}{sC} & -R \\0 & -R & 2R+\frac{1}{2sC} \end{bmatrix}\begin{bmatrix}I_1\\I_2\\I_3\end{bmatrix}=\begin{bmatrix}V_i\\0\\0\end{bmatrix}$$ Using Cramer's rule to find out \(I_3\), $$\text{Let, }D=\begin{bmatrix}R+\frac{1}{sC} & -R & 0 \\-R & 2R+\frac{1}{sC} & -R \\0 & -R & 2R+\frac{1}{2sC} \end{bmatrix}$$ \(|D|=\begin{vmatrix}R+\frac{1}{sC} & -R & 0 \\-R & 2R+\frac{1}{...