You are on page 1of 36

UNIT 4

EXPLAIN VI EDITOR WITH THEIR VARIOUS MODES AND TEXT PROCESSING EXAMPLES.

vi 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.
Syntax:
vi filename

Input: 

Output:
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.
Starting the vi Editor:
There are following way you can start using vi editor :
Commands and their Description
 vi filename: Creates a new file if it already not exist, otherwise opens existing file.
 vi -R filename : Opens an existing file in read only mode.
 view filename : Opens an existing file in read only mode.

Moving within a File(Navigation):


To move around within a file without affecting text must be in command mode (press Esc twice). Here
are some of the commands can be used to move around one character at a time.
Commands and their Description
 k : Moves the cursor up one line.
 j : Moves the cursor down one line.
 h : Moves the cursor to the left one character position.
 l : Moves the cursor to the right one character position.
 0 or | : Positions cursor at beginning of line.
 $ : Positions cursor at end of line.
 W : Positions cursor to the next word.
 B : Positions cursor to previous word.
 ( : Positions cursor to beginning of current sentence.
 ) : Positions cursor to beginning of next sentence.
 H : Move to top of screen.
 nH : Moves to nth line from the top of the screen.
 M : Move to middle of screen.
 L : Move to bottom of screen.
 nL : Moves to nth line from the bottom of the screen.
 colon along with x : Colon followed by a number would position the cursor on line number
represented by x.

Control Commands(Scrolling): There are following useful commands which can used along
with Control Key:
Commands and their Description:
 CTRL+d : Move forward 1/2 screen.
 CTRL+f : Move forward one full screen.
 CTRL+u : Move backward 1/2 screen.
 CTRL+b : Move backward one full screen.
 CTRL+e : Moves screen up one line.
 CTRL+y : Moves screen down one line.
 CTRL+u : Moves screen up 1/2 page.
 CTRL+d : Moves screen down 1/2 page.
 CTRL+b : Moves screen up one page.
 CTRL+f : Moves screen down one page.
 CTRL+I : Redraws screen.

Editing and inserting in Files(Entering and Replacing Text): To edit the file, we need to be in the insert
mode. There are many ways to enter insert mode from the command mode.
 I : Inserts text before current cursor location.
 I : Inserts text at beginning of current line.
 A : Inserts text after current cursor location.
 A : Inserts text at end of current line.
 O : Creates a new line for text entry below cursor location.
 O : Creates a new line for text entry above cursor location.
 R : Replace single character under the cursor with the next character typed.
 R : Replaces text from the cursor to right.
 S : Replaces single character under the cursor with any number of characters.
 S :Replaces entire line.

Deleting Characters: Here is the list of important commands which can be used to delete characters
and lines in an opened file.
 X : Deletes the character under the cursor location.
 X : Deletes the character before the cursor location.
 Dw : Deletes from the current cursor location to the next word.
 d^ : Deletes from current cursor position to the beginning of the line.
 d$ : Deletes from current cursor position to the end of the line.
 Dd : Deletes the line the cursor is on.

Copy and Past Commands: Copy lines or words from one place and paste them on another place by
using the following commands.
 Yy : Copies the current line.
 9yy : Yank current line and 9 lines below.
 p : Puts the copied text after the cursor.
 P : Puts the yanked text before the cursor.

Save and Exit Commands of the ex Mode : Need to press [Esc] key followed by the colon (:) before
typing the following 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
Input:

Output: finding the first match for “geeksforgeeks” in text will then be highlighted.

The syntax for replacing one string with another string in the current line is:
:s/pattern/replace/
Here “pattern” represents the old string and “replace” represents the new string. For example, to
replace each occurrence of the word “geeks” in a line with “geeksforgeeks” type:
:s/geeksforgeeks/gfg/
Input:

Output:
The syntax for replacing every occurrence of a string in the entire text is similar. The only difference is
the addition of a “%” in front of the “s”:
:%s/pattern/replace/
Thus repeating the previous example for the entire text instead of just for a single line would be:
:%s/gfg/geeksforgeeks/
COMMANDS WITH SYNTAX:

WHO

who  command is used to find out the following information :


1. Time of last system boot
2. Current run level of the system
3. List of logged in users and more.
Description :  The who command is used to get information about currently logged in user on to
system.
Syntax : $who [options] [filename]
Examples : 
1. The who command displays the following information for each user currently logged in to the
system if no option is provided :
1. Login name of the users
2. Terminal line numbers
3. Login time of the users in to system
4. Remote host name of the user
hduser@mahesh-Inspiron-3543:~$ who
hduser tty7 2018-03-18 19:08 (:0)
hduser@mahesh-Inspiron-3543:~$

WHO AM I

whoami  command is used both in  Unix Operating System  and as well as in  Windows Operating
System.
 It is basically the concatenation of the strings  “who”,”am”,”i”  as  whoami.
 It displays the username of the current user when this command is invoked.
 Syntax:

 geekforgeeks@HP~: whoami
 Output :
 geekforgeeks
 Example :
 Options of Whoami
 Syntax:
 geekforgeeks@HP~: whoami [OPTION]
 1. –help Option :
It gives the help message and exit.
Syntax :
 geekforgeeks@HP~: whoami --help
 Example :

2. –version Option :
It gives the version information and exit.
Syntax:
 geekforgeeks@HP~: whoami --version
 Example :
RM

rm stands for  remove  here. rm command is used to remove objects such as files, directories, symbolic
links and so on from the file system like UNIX. To be more precise, rm removes references to objects
from the filesystem, where those objects might have had multiple references (for example, a file with
two different names).  By default, it does not remove directories.
This command normally works silently and you should be very careful while running  rm  command
because once you delete the files then you are not able to recover the contents of files and directories.
Syntax:
rm [OPTION]... FILE...
Let us consider 5 files having name  a.txt, b.txt  and so on till  e.txt.
$ ls
a.txt b.txt c.txt d.txt e.txt
Removing one file at a time
$ rm a.txt

$ ls
b.txt c.txt d.txt e.txt

Removing more than one file at a time


$ rm b.txt c.txt

$ ls
d.txt e.txt
Note:  No output is produced by  rm, since it typically only generates messages in the case of an error.

Options:
1. -i (Interactive Deletion):  Like in  cp, the -i option makes the command ask the user for confirmation
before removing each file, you have to press  y  for confirm deletion, any other key leaves the file un-
deleted.
$ rm -i d.txt
rm: remove regular empty file 'd.txt'? y

$ ls
e.txt

2. -f (Force Deletion):  rm  prompts for confirmation removal if a file is  write protected. The  -f  option
overrides this minor protection and removes the file forcefully.
$ ls -l
total 0
-r--r--r--+ 1 User User 0 Jan 2 22:56 e.txt

$ rm e.txt
rm: remove write-protected regular empty file 'e.txt'? n

$ ls
e.txt

$ rm -f e.txt

$ ls

Note:  -f  option of rm command will not work for write-protect directories.

3. -r (Recursive Deletion):  With  -r(or -R)  option rm command performs a tree-walk and will delete all
the files and sub-directories recursively of the parent directory. At each stage it deletes everything it
finds. Normally,  rm  wouldn’t delete the directories but when used with this option, it will delete.
Below is the tree of directories and files:
$ ls
A

$ cd A

$ ls
B C

$ ls B
a.txt b.txt

$ ls C
c.txt d.txt

Now, deletion from  A  directory(as parent directory) will be done as:


$ rm *
rm: cannot remove 'B': Is a directory
rm: cannot remove 'C': Is a directory

$ rm -r *

$ ls

Every directory and file inside  A  directory is deleted.


4. –version:  This option is used to display the version of  rm  which is currently running on your system.
$ rm --version
rm (GNU coreutils) 8.26
Packaged by Cygwin (8.26-2)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Paul Rubin, David MacKenzie, Richard M. Stallman,


and Jim Meyering.
CHMOD

In Unix-like operating systems, the  chmod  command is used to change the access mode of a file.
The name is an abbreviation of  change mode.
Syntax :
chmod [reference][operator][mode] file...
The references are used to distinguish the users to whom the permissions apply i.e. they are list of
letters that specifies whom to give permissions. The references are represented by one or more of the
following letters:
Reference Class Description
u owner file's owner

g group users who are members of


the file's group

o others users who are neither the


file's owner nor members of
the file's group

a all All three of the above, same as ugo


The operator is used to specify how the modes of a file should be adjusted. The following operators
are accepted:
Operator Description
+ Adds the specified modes to the
specified classes

- Removes the specified modes from


the specified classes

= The modes specified are to be made


the exact modes for the specified
classes
Note :  Putting blank space(s) around operator would make the command fail.
The modes indicate which permissions are to be granted or removed from the specified classes. There
are three basic modes which correspond to the basic permissions:
r Permission to read the file.
w Permission to write (or delete) the file.
x Permission to execute the file, or, in
the case of a directory, search it.
ECHO

echo command in linux is used to display line of text/string that are passed as an argument . This is a
built in command that is mostly used in shell scripts and batch files to output status text to the screen
or a file.
Syntax :
echo [option] [string]
Displaying a text/string :
Syntax :
echo [string]
Example :

Options of echo command


NOTE :- -e here enables the interpretation of backslash escapes
1. \b : it removes all the spaces in between the text
Example :
echo -e "Geeks \bfor \bGeeks"

2. \c : suppress trailing new line with backspace interpretor ‘-e‘ to continue without emitting new line.
Example :
echo -e "Geeks \cfor Geeks"

In above example, text after \c is not printed and omitted trailing new line.

3. \n : this option creates new line from where it is used.


Example :
echo -e "Geeks \nfor \nGeeks"

4. \t : this option is used to create horizontal tab spaces.


Example :
echo -e "Geeks \tfor \tGeeks"
5. \r : carriage return with backspace interpretor ‘-e‘ to have specified carriage return in output.
Example :
echo -e "Geeks \rfor Geeks"

In the above example, text before \r is not printed.


6. \v : this option is used to create vertical tab spaces.
Example :
echo -e "Geeks \vfor \vGeeks

7. \a : alert return with backspace interpretor ‘-e‘ to have sound alert.


Example :
echo -e "\aGeeks for Geeks
This command when executed, it will produce an alert sound or Bel .
8. echo * : this command will print all files/folders, similar to ls command .

Example :
echo *

9. -n : this option is used to omit echoing trailing newline .


Example :
echo -n "Geeks for Geeks"
CP

cp  stands for  copy. This command is used to copy files or group of files or directory. It creates an exact
image of a file on a disk with different file name.  cp  command require at least two filenames in its
arguments.
Syntax:
cp [OPTION] Source Destination
cp [OPTION] Source Directory
cp [OPTION] Source-1 Source-2 Source-3 Source-n Directory

First and second syntax is used to copy Source file to Destination file or Directory.
Third syntax is used to copy multiple Sources(files) to Directory.
cp  command works on three principal modes of operation and these operations depend upon number
and type of arguments passed in cp command :
1. Two file names :  If the command contains two file names, then it copy the contents of  1st
file  to the  2nd file. If the 2nd file doesn’t exist, then first it creates one and content is copied to
it. But if it existed then it is simply overwritten without any warning. So be careful when you
choose destination file name.
2. cp Src_file Dest_file
Suppose there is a directory named  geeksforgeeks  having a text file  a.txt.
Example:
$ ls
a.txt

$ cp a.txt b.txt

$ ls
a.txt b.txt
3. One or more arguments :  If the command has one or more arguments, specifying file names
and following those arguments, an argument specifying directory name then this command
copies each source file to the destination directory with the same name, created if not existed
but if already existed then it will be overwritten, so be careful !!.
4. cp Src_file1 Src_file2 Src_file3 Dest_directory
Suppose there is a directory named  geeksforgeeks  having a text file  a.txt, b.txtand a directory
name  new  in which we are going to copy all files.
Example:

$ ls
a.txt b.txt new

Initially new is empty


$ ls new

$ cp a.txt b.txt new


$ ls new
a.txt b.txt
Note:  For this case last argument  must  be a  directory  name. For the above command to
work,  Dest_directory  must exist because  cp  command won’t create it.
5. Two directory names :  If the command contains two directory names,  cp  copies all files of the
source directory to the destination directory, creating any files or directories needed. This mode
of operation requires an additional option, typically  R, to indicate the recursive copying of
directories.
6. cp -R Src_directory Dest_directory
In the above command,  cp  behavior depend upon whether  Dest_directory  is exist or not. If
the  Dest_directory  doesn’t exist, cp creates it and copies content of  Src_directory  recursively as
it is. But if Dest_directory exists then copy of  Src_directory  becomes sub-directory
under  Dest_directory.
Options:
There are many options of  cp  command, here we will discuss some of the useful options:
Suppose a directory named  geeksforgeeks  contains two files having some content named
as  a.txt  and  b.txt. This scenario is useful in understanding the following options.
$ ls geeksforgeeks
a.txt b.txt

$ cat a.txt
GFG

$ cat b.txt
GeeksforGeeks
1. -i(interactive):  i  stands for Interactive copying. With this option system first warns the user before
overwriting the destination file.  cp  prompts for a response, if you press  y  then it overwrites the file
and with any other option leave it uncopied.
$ cp -i a.txt b.txt
cp: overwrite 'b.txt'? y

$ cat b.txt
GFG
2. -b(backup):  With this option  cp  command creates the backup of the destination file in the same
folder with the different name and in different format.
$ ls
a.txt b.txt

$ cp -b a.txt b.txt

$ ls
a.txt b.txt b.txt~
3. -f(force):  If the system is unable to open destination file for writing operation because the user
doesn’t have writing permission for this file then by using  -f  option with  cp  command, destination file
is deleted first and then copying of content is done from source to destination file.
$ ls -l b.txt
-r-xr-xr-x+ 1 User User 3 Nov 24 08:45 b.txt
User, group and others doesn't have writing permission.

Without -f option, command not executed


$ cp a.txt b.txt
cp: cannot create regular file 'b.txt': Permission denied

With -f option, command executed successfully


$ cp -f a.txt b.txt
4. -r or -R:  Copying directory structure. With this option  cp  command shows its recursive behavior by
copying the entire directory structure recursively.
Suppose we want to copy  geeksforgeeks  directory containing many files, directories
into  gfg  directory(not exist).
$ ls geeksforgeeks/
a.txt b.txt b.txt~ Folder1 Folder2

Without -r option, error


$ cp geeksforgeeks gfg
cp: -r not specified; omitting directory 'geeksforgeeks'

With -r, execute successfully


$ cp -r geeksforgeeks gfg

$ ls gfg/
a.txt b.txt b.txt~ Folder1 Folder2

5. -p(preserve):  With  -p  option  cp  preserves the following characteristics of each source file in the
corresponding destination file: the time of the last data modification and the time of the last access,
the ownership (only if it has permissions to do this), and the file permission-bits.
Note:  For the preservation of characteristics you must be the  root user  of the system, otherwise
characteristics changes.
$ ls -l a.txt
-rwxr-xr-x+ 1 User User 3 Nov 24 08:13 a.txt

$ cp -p a.txt c.txt

$ ls -l c.txt
-rwxr-xr-x+ 1 User User 3 Nov 24 08:13 c.txt
As we can see above both  a.txt  and  c.txt(created by copying) have same characteristics.
Examples:
Copying using * wildcard:  The star wildcard represents anything i.e. all files and directories. Suppose
we have many text document in a directory and wants to copy it another directory, it takes lots of
time if we copy files 1 by 1 or command becomes too long if specify all these file names as the
argument, but by using * wildcard it becomes simple.
Initially Folder1 is empty
$ ls
a.txt b.txt c.txt d.txt e.txt Folder1
$ cp *.txt Folder1

$ ls Folder1
a.txt b.txt c.txt d.txt e.txt
GREP

The grep filter searches a file for a particular pattern of characters, and displays all lines that contain
that pattern. The pattern that is searched in the file is referred to as the regular expression (grep
stands for globally search for regular expression and print out).
Syntax:
grep [options] pattern [files]
Options Description
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line,
with each such part on a separate output line.
Sample Commands
Consider the below file as an input.
$cat > geekfile.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
1. Case insensitive search :  The -i option enables to search for a string case insensitively in the give
file. It matches the words like “UNIX”, “Unix”, “unix”.
$grep -i "UNix" geekfile.txt
Output:

unix is great os. unix is opensource. unix is free os.


Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
2. Displaying the count of number of matches :  We can find the number of lines that matches the
given string/pattern
$grep -c "unix" geekfile.txt
Output:
2
3. Display the file names that matches the pattern :  We can just display the files that contains the
given string/pattern.
$grep -l "unix" *

or

$grep -l "unix" f1.txt f2.txt f3.xt f4.txt


Output:
geekfile.txt
4. Checking for the whole words in a file :  By default, grep matches the given string/pattern even if it
found as a substring in a file. The -w option to grep makes it match only the whole words.
$ grep -w "unix" geekfile.txt
Output:
unix is great os. unix is opensource. unix is free os.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
5. Displaying only the matched pattern :  By default, grep displays the entire line which has the
matched string. We can make the grep to display only the matched string by using the -o option.
$ grep -o "unix" geekfile.txt
Output:
unix
unix
unix
unix
unix
unix

6. Show line number while displaying the output using grep -n :  To show the line number of file with
the line matched.
$ grep -n "unix" geekfile.txt
Output:

1:unix is great os. unix is opensource. unix is free os.


4:uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
7. Inverting the pattern match :  You can display the lines that are not matched with the specified
search sting pattern using the -v option.
$ grep -v "unix" geekfile.txt
Output:
learn operating system.
Unix linux which one you choose.
8. Matching the lines that start with a string :  The ^ regular expression pattern specifies the start of a
line. This can be used in grep to match the lines which start with the given string or pattern.
$ grep "^unix" geekfile.txt
Output:
unix is great os. unix is opensource. unix is free os.
9. Matching the lines that end with a string :  The $ regular expression pattern specifies the end of a
line. This can be used in grep to match the lines which end with the given string or pattern.
$ grep "os$" geekfile.txt
10.Specifies expression with -e option. Can use multiple times :
$grep –e "Agarwal" –e "Aggarwal" –e "Agrawal" geekfile.txt
11. -f file option Takes patterns from file, one per line.
$cat pattern.txt

Agarwal
Aggarwal
Agrawal
$grep –f pattern.txt geekfile.txt
SED

SED command in UNIX is stands for stream editor and it can perform lot’s of function on file like,
searching, find and replace, insertion or deletion. Though most common use of SED command in UNIX
is for substitution or for find and replace. By using SED you can edit files even without opening it,
which is much quicker way to find and replace something in file, than first opening that file in VI Editor
and then changing it.
 SED is a powerful text stream editor. Can do insertion, deletion, search and
replace(substitution).
 SED command in unix supports regular expression which allows it perform complex pattern
matching.
Syntax:
sed OPTIONS... [SCRIPT] [INPUTFILE...]
Example:
Consider the below text file as an input.
$cat > geekfile.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
Sample Commands
1. Replacing or substituting string :  Sed command is mostly used to replace the text in a file. The
below simple sed command replaces the word “unix” with “linux” in the file.
2. $sed 's/unix/linux/' geekfile.txt
Output :

linux is great os. unix is opensource. unix is free os.


learn operating system.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
Here the “s” specifies the substitution operation. The “/” are delimiters. The “unix” is the search
pattern and the “linux” is the replacement string.
By default, the sed command replaces the first occurrence of the pattern in each line and it
won’t replace the second, third…occurrence in the line.
3. Replacing the nth occurrence of a pattern in a line :  Use the /1, /2 etc flags to replace the first,
second occurrence of a pattern in a line. The below command replaces the second occurrence of
the word “unix” with “linux” in a line.
4. $sed 's/unix/linux/2' geekfile.txt
Output:
unix is great os. linux is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.linux is a multiuser os.Learn unix .unix is a powerful.
5. Replacing all the occurrence of the pattern in a line :  The substitute flag /g (global
replacement) specifies the sed command to replace all the occurrences of the string in the line.
6. $sed 's/unix/linux/g' geekfile.txt
Output :
linux is great os. linux is opensource. linux is free os.
learn operating system.
linux linux which one you choose.
linux is easy to learn.linux is a multiuser os.Learn linux .linux is a powerful.
7. Replacing from nth occurrence to all occurrences in a line :  Use the combination of /1, /2 etc
and /g to replace all the patterns from the nth occurrence of a pattern in a line. The following
sed command replaces the third, fourth, fifth… “unix” word with “linux” word in a line.
8. $sed 's/unix/linux/3g' geekfile.txt
Output:
unix is great os. unix is opensource. linux is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn linux .linux is a powerful.
9. Parenthesize first character of each word :  This sed example prints the first character of every
word in paranthesis.
10. $ echo "Welcome To The Geek Stuff" | sed 's/\(\b[A-Z]\)/\(\1\)/g'
Output:
(W)elcome (T)o (T)he (G)eek (S)tuff
11. Replacing string on a specific line number :  You can restrict the sed command to replace the
string on a specific line number. An example is
12. $sed '3 s/unix/linux/' geekfile.txt
Output:
unix is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
The above sed command replaces the string only on the third line.
13. Duplicating the replaced line with /p flag :  The /p print flag prints the replaced line twice on
the terminal. If a line does not have the search pattern and is not replaced, then the /p prints
that line only once.
14. $sed 's/unix/linux/p' geekfile.txt
Output:
linux is great os. unix is opensource. unix is free os.
linux is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
15. Printing only the replaced lines :  Use the -n option along with the /p print flag to display only
the replaced lines. Here the -n option suppresses the duplicate rows generated by the /p flag
and prints the replaced lines only one time.
16. $sed -n 's/unix/linux/p' geekfile.txt
Output:
linux is great os. unix is opensource. unix is free os.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
If you use -n alone without /p, then the sed does not print anything.
17. Replacing string on a range of lines :  You can specify a range of line numbers to the sed
command for replacing a string.
18. $sed '1,3 s/unix/linux/' geekfile.txt
Output:
linux is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
Here the sed command replaces the lines with range from 1 to 3. Another example is
$sed '2,$ s/unix/linux/' geekfile.txt
Output:
unix is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful
Here $ indicates the last line in the file. So the sed command replaces the text from second line
to last line in the file.
19. Deleting lines from a particular file :  SED command can also be used for deleting lines from a
particular file. SED command is used for performing deletion operation without even opening
the file
Examples:
1. To Delete a particular line say n in this example
20. Syntax:
21. $ sed 'nd' filename.txt
22. Example:
23. $ sed '5d' filename.txt
2. To Delete a last line
Syntax:
$ sed '$d' filename.txt
3. To Delete line from range x to y
Syntax:
$ sed 'x,yd' filename.txt
Example:
$ sed '3,6d' filename.txt
5. To Delete from nth to last line
Syntax:
$ sed 'nth,$d' filename.txt
Example:
$ sed '12,$d' filename.txt
6. To Delete pattern matching line
Syntax:
$ sed '/pattern/d' filename.txt
Example:
$ sed '/abc/d' filename.txt
AWK

Awk is a scripting language used for manipulating data and generating reports.The awk command
programming language requires no compiling, and allows the user to use variables, numeric
functions, string functions, and logical operators.
Awk is a utility that enables a programmer to write tiny but effective programs in the form of
statements that define text patterns that are to be searched for in each line of a document and the
action that is to be taken when a match is found within a line. Awk is mostly used for pattern scanning
and processing. It searches one or more files to see if they contain lines that matches with the
specified patterns and then performs the associated actions.
Awk is abbreviated from the names of the developers – Aho, Weinberger, and Kernighan.
WHAT CAN WE DO WITH AWK ?
1. AWK Operations:
(a) Scans a file line by line
(b) Splits each input line into fields
(c) Compares input line/fields to pattern
(d) Performs action(s) on matched lines

2. Useful For:
(a) Transform data files
(b) Produce formatted reports
3. Programming Constructs:
(a) Format output lines
(b) Arithmetic and string operations
(c) Conditionals and loops
Syntax:
awk options 'selection _criteria {action }' input-file > output-file
Options:
-f program-file : Reads the AWK program source from the file
program-file, instead of from the
first command line argument.
-F fs : Use fs for the input field separator
Sample Commands
Example:
Consider the following text file as the input file for all cases below.
$cat > employee.txt
ajay manager account 45000
sunil clerk account 25000
varun manager sales 50000
amit manager account 47000
tarun peon sales 15000
deepak clerk sales 23000
sunil peon sales 13000
satvik director purchase 80000
1. Default behavior of Awk :  By default Awk prints every line of data from the specified file.
$ awk '{print}' employee.txt
Output:
ajay manager account 45000
sunil clerk account 25000
varun manager sales 50000
amit manager account 47000
tarun peon sales 15000
deepak clerk sales 23000
sunil peon sales 13000
satvik director purchase 80000
In the above example, no pattern is given. So the actions are applicable to all the lines. Action print
without any argument prints the whole line by default, so it prints all the lines of the file without
failure.
2. Print the lines which matches with the given pattern.

$ awk '/manager/ {print}' employee.txt


Output:
ajay manager account 45000
varun manager sales 50000
amit manager account 47000
In the above example, the awk command prints all the line which matches with the ‘manager’.
3. Spliting a Line Into Fields :  For each record i.e line, the awk command splits the record delimited by
whitespace character by default and stores it in the $n variables. If the line has 4 words, it will be
stored in $1, $2, $3 and $4 respectively. Also, $0 represents the whole line.
$ awk '{print $1,$4}' employee.txt
Output:
ajay 45000
sunil 25000
varun 50000
amit 47000
tarun 15000
deepak 23000
sunil 13000
satvik 80000
In the above example, $1 and $4 represents Name and Salary fields respectively.
Built In Variables In Awk
Awk’s built-in variables include the field variables—$1, $2, $3, and so on ($0 is the entire line) — that
break a line of text into individual words or pieces called fields.
NR:  NR command keeps a current count of the number of input records. Remember that records are
usually lines. Awk command performs the pattern/action statements once for each record in a file.
NF:  NF command keeps a count of the number of fields within the current input record.
FS:  FS command contains the field separator character which is used to divide fields on the input line.
The default is “white space”, meaning space and tab characters. FS can be reassigned to another
character (typically in BEGIN) to change the field separator.

RS:  RS command stores the current record separator character. Since, by default, an input line is the
input record, the default record separator character is a newline.
OFS:  OFS command stores the output field separator, which separates the fields when Awk prints
them. The default is a blank space. Whenever print has several parameters separated with commas, it
will print the value of OFS in between each parameter.
ORS:  ORS command stores the output record separator, which separates the output lines when Awk
prints them. The default is a newline character. print automatically outputs the contents of ORS at the
end of whatever it is given to print.
Examples:
Use of NR built-in variables (Display Line Number)
$ awk '{print NR,$0}' employee.txt
Output:
1 ajay manager account 45000
2 sunil clerk account 25000
3 varun manager sales 50000
4 amit manager account 47000
5 tarun peon sales 15000
6 deepak clerk sales 23000
7 sunil peon sales 13000
8 satvik director purchase 80000
In the above example, the awk command with NR prints all the lines along with the line number.
Use of NF built-in variables (Display Last Field)
$ awk '{print $1,$NF}' employee.txt
Output:
ajay 45000
sunil 25000
varun 50000
amit 47000
tarun 15000
deepak 23000
sunil 13000
satvik 80000
In the above example $1 represents Name and $NF represents Salary. We can get the Salary using $NF
, where $NF represents last field.

Another use of NR built-in variables (Display Line From 3 to 6)


$ awk 'NR==3, NR==6 {print NR,$0}' employee.txt
Output:
3 varun manager sales 50000
4 amit manager account 47000
5 tarun peon sales 15000
6 deepak clerk sales 23000
More Examples
For the given text file:
$cat > geeksforgeeks.txt

A B C
Tarun A12 1
Man B6 2
Praveen M42 3
1) To print the first item along with the row number(NR) separated with ” – “ from each line in
geeksforgeeks.txt:
$ awk '{print NR "- " $1 }' geeksforgeeks.txt
1 - Tarun
2 – Manav
3 - Praveen
2) To return the second row/item from geeksforgeeks.txt:
$ awk '{print $2}' geeksforgeeks.txt
A12
B6
M42
3) To print any non empty line if present
$ awk 'NF > 0' geeksforgeeks.txt
0
4) To find the length of the longest line present in the file:
$ awk '{ if (length($0) > max) max = length($0) } END { print max }' geeksforgeeks.txt
13
5) To count the lines in a file:
$ awk 'END { print NR }' geeksforgeeks.txt
3
6) Printing lines with more than 10 characters:
$ awk 'length($0) > 10' geeksforgeeks.txt
Tarun A12 1
Praveen M42 3
7) To find/check for any string in any column:
$ awk '{ if($3 == "B6") print $0;}' geeksforgeeks.txt
8) To print the squares of first numbers from 1 to n say 6:
$ awk 'BEGIN { for(i=1;i<=6;i++) print "square of", i, "is",i*i; }'
square of 1 is 1
square of 2 is 4
square of 3 is 9
square of 4 is 16
square of 5 is 25
square of 6 is 36
YACC

On  Unix-like  operating systems, the  yacc  command is a  compilerthat generates a Look-Ahead, Left-
to-right, Rightmost-derivation (LALR) parser. It is a compiler-compiler: a compiler which creates a
compiler. Yacc takes as input a formal description of a  programming language, and its output is a
parser which executes certain actions, specific to a target architecture, when compiling a program
written in the language.

The name "yacc" is an acronym for "Yet Another Compiler-


Compiler." It was extremely popular and was once standard on
all Unix systems. It has since been supplanted by more recent
compiler-compilers, which are mostly backward compatible with
yacc.

Syntax

yacc [-d] [-l] [-t] [-V] [-v] [ -b file_prefix ] [ -Q [y | n ] ]

[ -P parser ] [ -p sym_prefix ] file

-d Generates the file  y.tab.h  with the  #define  statements that associate the  yaccuser-assigned
"token codes" with the user-declared "token names." This association allows source files other
than  y.tab.c  to access the token codes.

-l Specifies that the code produced in  y.tab.c  will not contain any  #line  constructs. This option
should only be used after the grammar and the associated actions are fully debugged.

-t Compiles runtime debugging code by default. Runtime debugging code is always generated
in  y.tab.c  under conditional compilation control. By default, this code is not included
when  y.tab.c  is compiled. Whether or not the  -t  option is used, the runtime debugging code is
under the control of  YYDEBUG, a preprocessor symbol. If  YYDEBUG  has a non-zero value, then the
debugging code is included. If its value is  0, then the code will not be included. The size and
execution time of a program produced without the runtime debugging code will be smaller and
slightly faster.

-v Prepares the file  y.output, which contains a description of the parsing tables and a report on
conflicts generated by ambiguities in the grammar.

-V Prints on the standard error output the version information for  yacc.

-b  file_prefix Use  file_prefix  instead of  y  as the prefix for all output files. The code file  y.tab.c, the header
file  y.tab.h  (created when  -d  is specified), and the description file  y.output  (created when  -v  is
specified), will be changed to  file_prefix.tab.c,  file_prefix.tab.h, and  file_prefix.output,
respectively.

-Q  [y|n] The  -Qy  option puts the version stamping information in  y.tab.c. This allows you to know what
version of  yacc  built the file. The  -Qn  option (the default) writes no version information.

-P  parser Allows you to specify the parser of your choice instead of  /usr/ccs/bin/yaccpar. For example, you
can specify:

yacc -P ~/myparser parser.y

-p  sym_prefi Use  sym_prefix  instead of  yy  as the prefix for all external names produced by  yacc. The names
x affected include the functions  yyparse(),  yylex()  and  yyerror(), and the
variables  yylval,  yychar  and  yydebug. (In the remainder of this section, the six symbols cited are
referenced using their default names only as a notational convenience.) Local names may also be
affected by the  -p  option; however, the  -p  option does not affect  #define  symbols generated
by  yacc.

file A  path name  of a file containing instructions for which a parser is to be created.

You might also like