UNIX COmmand
UNIX COmmand
UNIX COmmand
one command serves as input to the next. In short, the output of each process directly as input
to the next one like a pipeline. The symbol '|' denotes a pipe. A pipe is a form of redirection
(transfer of standard output to some other destination) that is used in Linux and other Unix-like
operating systems to send the output of one command/program/process to another
command/program/process for further processing. The Unix/Linux systems allow stdout of a
command to be connected to stdin of another command.
This direct connection between commands/ programs/ processes allows them to
operate simultaneously and permits data to be transferred between them
continuously rather than having to pass it through temporary text files or through the
display screen.
Pipes are unidirectional i.e data flows from left to right through the pipeline.
Syntax :
command_1 | command_2 | command_3 | .... | command_N
Example :
1. Listing all files and directories and give it as input to more command.
$ ls -l | more
Output :
This ps and wc command pipeline gives you an idea of how many processes
are currently running on your system:
ps aux | wc -l
The number returned is actually high by one because the first line of output
from the ps command is a header, and not a process.
Every program we run on the command line automatically has three data streams connected to
it.
Overview
Grep is a powerful tool for matching a regular expression against text in a
file, multiple files, or a stream of input. It searches for the PATTERN of
text that you specify on the command line, and outputs the results for
you.
The basic usage of grep command is to search for a specific string in the specified file
as shown below.
Syntax:
This is also a basic usage of grep command. For this example, let us copy the
demo_file to demo_file1. The grep output will also include the file name in front of
the line that matched the specific pattern as shown below. When the Linux shell sees
the meta character, it does the expansion and gives all the files as input to grep.
$ cp demo_file demo_file1
This is also a basic usage of the grep. This searches for the given string/pattern case
insensitively. So it matches all the words such as “the”, “THE” and “The” case
insensitively as shown below.
This Line Has All Its First Character Of The Word With Upper Case.
This is a very powerful feature, if you can use use regular expression effectively. In
the following example, it searches for all the pattern that starts with “lines” and ends
with “empty” with anything in-between. i.e To search “lines[anything in-
between]empty” in the demo_file.
From documentation of grep: A regular expression may be followed by one of several
repetition operators:
The following example is the regular grep where it is searching for “is”. When you
search for “is”, without any option it will show out “is”, “his”, “this” and everything
which has the substring “is”.
$ grep -i "is" demo_file
This Line Has All Its First Character Of The Word With Upper Case.
The following example is the WORD grep where it is searching only for the word “is”.
Please note that this output does not contain the line “This Line Has All Its First
Character Of The Word With Upper Case”, even though “is” is there in the “This”, as
the following is looking only for the word “is” and not for “this”.
$ grep -iw "is" demo_file
You may want to do several navigation in relation to the words, such as:
Syntax:
The following example prints the matched line, along with the 3 lines after it.
$ grep -A 3 -i "example" demo_text
Syntax:
When you had option to show the N lines after match, you have the -B option for the
opposite.
$ grep -B 2 "single WORD" demo_text
Example to show the difference between WORD and word
When you do the following export you will get the highlighting of the matched
searches. In the following example, it will highlight all the this when you set the
GREP_OPTIONS environment variable as shown below.
$ export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'
$ grep -r "ramesh" *
When you want to display the lines which does not matches the given string/pattern,
use the option -v as shown below. This example will display all the lines that did not
match the word “go”.
$ grep -v "go" demo_text
You may want to do several navigation in relation to the words, such as:
$ cat test-file.txt
Syntax:
When you want do find out how many lines matches the pattern
$ grep -c this demo_file
3
When you want do find out how many lines that does not match the pattern
$ grep -v -c this demo_file
12. Display only the file names which matches the given
pattern using grep -l
If you want the grep to show out only the file names which matched the given
pattern, use the -l (lower-case L) option.
When you give multiple files to the grep as input, it displays the names of file which
contains the text that matches the pattern, will be very handy when you try to find
some notes in your whole directory structure.
$ grep -l this demo_*
demo_file
demo_file1
It might not be that much useful when you give the string straight forward. But it
becomes very useful when you give a regex pattern and trying to see what it matches
as
$ grep -o "is.*line" demo_file
is line
$ cat temp-file.txt
12345
12345
2:3
8:3
Note: The output of the grep command above is not the position in the line, it is byte
offset of the whole file.