Piping in Unix or Linux
Piping in Unix or Linux
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. You can make it do so by using the pipe character ‘|’.
Pipe is used to combine two or more commands, and in this, the output of one command acts
as input to another command, and this command’s output may act as input to the next
command and so on. It can also be visualized as a temporary connection between two or
more commands/ programs/ processes. The command line programs that do the further
processing are referred to as filters.
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.
Listing all files and directories and give it as input to more command.
-v
Prints all lines that do not match pattern.
-n
Prints the matched line and its line number.
-l
Prints only the names of files with matching lines (letter "l")
-c
Prints only the count of matching lines.
-i
Matches either upper or lowercase.
Let us now use a regular expression that tells grep to find lines with "carol", followed by
zero or other characters abbreviated in a regular expression as ".*"), then followed by "Aug".
−
Here, we are using the -i option to have case insensitive search −
$ls -l | grep -i "carol.*aug"
-rw-rw-r-- 1 carol doc 1605 Aug 23 07:35 macros
$
$ ls -l | more
Use sort and uniq command to sort a file and print unique values.
$ sort record.txt | uniq
This will sort the given file and print the unique values only.
Use head and tail to print lines in a particular range in a file.
$ cat sample2.txt | head -7 | tail -5
This command select first 7 lines and last 5 lines from the file and print those lines which are
common to both of them.
The pg and more Commands
A long output can normally be zipped by you on the screen, but if you run text through more
or use the pg command as a filter; the display stops once the screen is full of text.
Let's assume that you have a long directory listing. To make it easier to read the sorted
listing, pipe the output through more as follows −
$ls -l | grep "Aug" | sort +4n | more
-rw-rw-r-- 1 carol doc 1605 Aug 23 07:35 macros
-rw-rw-r-- 1 john doc 2488 Aug 15 10:51 intro
-rw-rw-rw- 1 john doc 8515 Aug 6 15:30 ch07
-rw-rw-r-- 1 john doc 14827 Aug 9 12:40 ch03
.
.
.
-rw-rw-rw- 1 john doc 16867 Aug 6 15:56 ch05
--More--(74%)
The screen will fill up once the screen is full of text consisting of lines sorted by the order of
the file size. At the bottom of the screen is the more prompt, where you can type a command
to move through the sorted text.
Editor in UNIX
The default editor that comes with the UNIX operating system is called vi (visual editor).
Using vi editor, we can edit an existing file or create a new file from scratch. we can also use
this editor to just read a text file.
An improved version of the vi editor which is called the VIM has also been made available
now. Here, VIM stands for Vi IMproved.
vi is generally considered the de facto standard in Unix editors because −
It's usually available on all the flavors of Unix system.
Its implementations are very similar across the board.
It requires very few resources.
It is more user-friendly than other editors such as the ed or the ex.
You can use the vi editor to edit an existing file or to create a new file from scratch. You can
also use this editor to just read a text file.
Syntax:
vi filename
Modes of Operation in vi editor There are three modes of operation in vi:
Command Mode: When vi starts up, it is in Command Mode. This mode is where vi
interprets any characters we type as commands and thus does not display them in the
window. This mode allows us to move through a file, and to delete, copy, or paste a
piece of text.
To enter into Command Mode from any other mode, it requires pressing the [Esc] key.
If we press [Esc] when we are already in Command Mode, then vi will beep or flash the
screen.
Insert mode: This mode enables you to insert text into the file. Everything that’s typed
in this mode is interpreted as input and finally, it is put in the file. The vi always starts
in command mode. To enter text, you must be in insert mode. To come in insert mode
you simply type i. To get out of insert mode, press the Esc key, which will put you back
into command mode.
Last Line Mode(Escape Mode): Line Mode is invoked by typing a colon [:], while vi
is in Command Mode. The cursor will jump to the last line of the screen and vi will wait
for a command. This mode enables you to perform tasks such as saving files, executing
commands.
q : Quit
q! : Quit without saving changes i.e. discard changes.
r fileName : Read data from file called fileName.
wq : Write and quit (save and exit).
w fileName : Write to file called fileName (save as).
w! fileName : Overwrite to file called fileName (save as forcefully).
!cmd : Runs shell commands and returns to Command mode.
Searching and Replacing in (ex Mode): vi also has powerful search and replace
capabilities. The formal syntax for searching is:
:s/string
For example, suppose we want to search some text for the string “geeksforgeeks” Type the
following and press ENTER:
:s/geeksforgeeks