You are on page 1of 5

Section #9

1 Pipe-lining
By “Pipe-lining” we mean taking the output of a certain command and passing it as an input to another
command.

This is how pipe-lining works:

Command_1 | Command_2
The “vertical bar” symbol is the one used to redirect the output of the command into another
command.

* This syntax can be extended, which means you can use as many commands as needed
command_1 | command_2 | command_3 | command_4 | …

command_1 will be carried out and its output will be the input to command_2 which in turn gets
executed and have its output moved to command_3 as an input .. and so on.

[ahmad@localhost /home]$ ls | wc -l

This, as mentioned above, takes the output of the command “ls” and uses it as an input to the
commands “wc -l”

The command “wc” takes a string input and counts


1. number of lines
2. number of words
3. number of characters
When we specify the “-l” option, the command outputs only the numbers of lines.
So, give it a try, use the command once with the “-l” option and another without it.

Back to our pipe-lined command, now we know that it shows the #lines in the output of the command
“ls”.

Now let’s move on to another example, but before that let’s explain first the commands that will be used
in it.

[ahmad@localhost /home]$ cat /etc/passwd

the famous “cat” command .. prints the contents of the file “passwd”.
If we look closely at the output we get something like this
root:x:0:0:root:/root:/bin/bash
7 fields, each separated from the other by a colon “:”
So in this file, our delimiter is the colon.
Now, if we wanted to extract the 7th field (/bin/bash) only we have to use another command called
“cut”
the command “cut” works by specifying
1. the delimiter
2. the field you want to output

[ahmad@localhost /home]$ cut -d : -f 7 /etc/passwd

in this example, we used the “-d” option to tell the command that the delimiter in this file is the “:”
colon character … then we used the “-f” option to say that we need the field #7

Now if we look at the output, we get only the 7th field of each line.

The delimiter doesn’t have to be a colon; depending on the structure of your file, you specify the
appropriate delimiter.

Back to pipe-lining
ahmad@localhost /home]$ cut -d : -f 7 /etc/passwd | sort

This redirects the output of the first command that we’ve just seen into the command “sort”

The command “sort” is self-explanatory. It simple sorts whatever the thing that’s passed to it.

So if we considered the previous output sample, the “sort” command will output this.

/bin/bash
/bin/sync
/usr/sbin/nologin
/usr/sbin/nologin
/usr/sbin/nologin
/usr/sbin/nologin

ahmad@localhost /home]$ cut -d : -f 7 /etc/passwd | sort | uniq

the command “uniq” removes duplicates, so we expect the output to be something like:

/bin/bash
/bin/sync
/usr/sbin/nologin

ahmad@localhost /home]$ cut -d : -f 7 /etc/passwd | sort | uniq | wc -l

Try this one for yourself, and try to figure out what it does.
2 Editing files using nano
to start editing files with nano, simple write the word “nano” followed by the file
name .. note that if the file doesn’t exist nano creates it.

[ahmad@localhost /home]$ nano my_first_file_with_nano

it opens up a window like this, where all the shortcuts needed to (save, search, exit,…) are listed at the
bottom of the window.

Start typing, then to


1. Save (Write out) ==> Ctrl + O
2. Cut a line ==> Ctrl + K
3. Paste a line ==> Ctrl + U
4. Search for a word ==> Ctrl + W
5. Exit ==> Ctrl + X
3 Editing files using vim
to start editing files with vim, write the word “vim” followed by the file name .. note
that if the file doesn’t exist vim creates it.

[ahmad@localhost /home]$ vim my_first_file_with_vim

it opens up a window like this, But NO Shortcuts, nothing at all but an empty space .. even if you try to
type something, it will probably show you some error message at the bottom.

VIM has two modes of operation

1. The Command mode (Allows you to save, exit, exit without saving, ...)
2. The Insert mode

to move from one mode to the other you have to press the Escape button first on
your keyboard.

1. The Command mode [First hit the Esc button on the keyboard] then to

1.1 save ==> :w ( w = Write)


1.2 save and exit ==> :wq ( w = Write & q = Quit)
1.3 exit without saving ==> :q!
1.4 show a dialog asking to save (the edited file) before existing
or exit directly if no changes occurred to the file
==> :q

1.5 add line numbering ==> :set number


1.6 remove line numbering ==> :set nonumber
1.7 remove line numbering if set, and add it if not fount
==> :set invnumber
( invnumber = Inverse number)
i
2. The Insert mode [First hit the Esc button on the keyboard] then press ‘ ’

You’ll notice that the bottom line changed into ‘--INSERT--’

start typing, and then to save what you’ve written go back to the command mode
by hitting the Esc button and then ‘:w’

.Vimrc Setting file


If you wanted to show line numbers by default on every file you open with vim
you have to tell vim that explicitly …
We can do that by creating a file named .vimrc inside of out home directory

Steps [ showing line numbers by default ]


1. open a new file named .vimrc with any editor you want
Make sure your current directory is the home directory

2. add the following line inside of it


:set number

3. Save the file, and then open any file using vim, you’ll notice line numbers
were added.

Another thing that can be done with .vimrc is creating shortcuts .. If you’re into
the netbeans editor you probably know already that you can write the main
method of a java program by typing psvm and then hitting tab

The same thing can be done in vim..

Steps [ Creating a shortcut that expands into another line ]


1. open .vimrc with any editor of your choice
2. add the following line inside of it
abbr psvm public static void main(String[] args) {}
3. Save the file
4. Open a new file using vim, type psvm and then hit enter
5. The psvm gets replaced with
public static void main(String[] args) {}

One last thing is creating a shortcut (like Ctrl + S that is used in most editors to
save changed made to a file).

Steps [ Creating a shortcut that expands into another line ]


1. open .vimrc with any editor of your choice
2. add the following line inside of it
nmap <C-N> :set invnumber<CR>
3. Save the file
4. Open a new file using vim, hit CTRL + N, You’ll notice line numbers appeared
5. hit CTRL + N again, you’ll notice line numbers disappeared.

Shortcuts is created using the following syntax

nmap <Shortcut> Command<CR>

You might also like