Pipes, Redirection and REGEX
Pipes, Redirection
and REGEX
PIPES:
The pipe
| character can be used to send the
output of one command to another. The output of
one command becomes input for the next command. This can be a powerful tool,
especially when looking for specific data; piping is often used to refine the results of an
initial command.
Commands:
Command
|
Syntax
|
Description
|
head
|
$ head <file path>
|
It will display first 10lines of file.
|
tail
|
$ tail <file path>
|
It will display last 10 lines of
file.
|
$ head -3 /etc/group
|
First three lines of
/etc/group |
|
$ head -n 3 /etc/group
|
First three lines of
/etc/group |
|
$ help | head
|
First three lines of
/etc/group |
|
$ ls /etc | head (|-Pipe Symbol)
|
It will listing first 10 lines in
etc.
|
|
$ ls –l /etc/ppp | nl |tail -5
|
It will list last 5 lines with
numbering in/etc/ppp.
|
Redirection:
Input/Output (I/O) redirection allows for
command line information to be passed to different streams.
Streams:
1. STDIN
Standard input, or STDIN, is information
entered normally by the user via the keyboard. When a command prompts the shell
for data, the shell provides the user with the ability to type commands that,
in turn, are sent to the command as STDIN.
2. STDOUT
Standard output, or STDOUT, is the normal
output of commands. When a command functions correctly (without errors) the
output it produces is called STDOUT. By default, STDOUT is displayed in the
terminal window (screen) where the command is executing.
3. STDERR
Standard errors, or STDERR, are error messages generated by
commands. By default, STDERR is displayed in the terminal window (screen) where
the command is executing.
I/O redirection allows the user to redirect STDIN so data comes
from a file and STDOUT/STDERR so output goes to a file. Redirection is achieved
by using the arrow characters:
< and >.
Commands:
Command
|
Syntax
|
Description
|
STDOUT Redirection
|
$ echo <text> or command > <STDOUT file>
> -Redirection Symbol
|
Text will store STDOUT file
Directly.
|
$ echo <text> or command >> <STDOUT file>
>> -Redirection
Symbol without overwrite
|
Text will store in same STDOUT file
without over write. Will be in second line.
|
|
Redirect with Multiple Commands
|
$ ls <path1> <path2> > <STDOUT file>
|
It will store all outputs in same file.
|
$ ls <path1> <path2> 2> <STDERR file>
|
It will store any error in output .if
not, it will display output.
|
|
$ ls <path1> <path2> &> <STD file>
|
It will store both output and error in
same file.
|
|
$ ls <path1> <path2> > <STDOUT file> 2> <STDERR file>
|
It will store Output and Error in corresponding
files.
|
Searching
Commands:
Commands
|
Syntax
|
Description
|
Search by filename
|
$ find <path> -name <Keyword>
|
It will find the keyword related
files in respective path.
|
$ find <path> -name <Keyword>
2> <STDERR file>
|
It will display only founded files name.
Errors will stored in stderr file.
|
|
$ find <path> -name <Keyword>
2> /dev/null
|
Error will not store anywhere.
|
|
$ find <path> -name <Keyword>
-ls 2> /dev/null
|
It will display the file with
details.
|
|
Search by Size
|
$ find <path> -Size <Size>
-ls 2> /dev/null
Size: 10C – 10byte
10K -10kilobytes
10M -10megabytes
10G -10gegabyte
|
It will display the specified size
of files.
Note: if using + before size, it
will display more than size of files. – for vice versa.
|
Regular Expression (REGEX)
. - Any single character.
[] - A list or range
of characters to match one character, unless the first character is the caret
^, and then it means any character not in the list.
* - Previous
character repeated zero or more times.
^ - Following text must appear at beginning of
line.
$ - Preceding text must appear at the end of the line.
Commands
|
Syntax
|
Descrption
|
. -Expression
|
$ grep –-color ‘a..’ <filename>
|
It will display the texts start with abc in
coloring. The first . is ‘B’ and second is ‘C’.
|
[]- Expression
|
$ grep --color ‘[ab]’ <file name>
|
It will display the lines start with ab
with coloring.
|
$ grep --color ‘[^ab]c’ <file name>
|
It will display text which are having third
digit is C.
|
|
$ grep --color ‘d*’ <file name>
|
It will display texts start with d.
|
|
$ -Expression
|
$ grep –color “d$” <filename>
|
It will display texts end with d.
|

Comments
Post a Comment