Professional Documents
Culture Documents
UNIT 4 Topics Sol
UNIT 4 Topics Sol
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.
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 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
$ 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
$ rm -r *
$ ls
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
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 :
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.
Example :
echo *
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
$ 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.
$ 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:
or
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:
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 :
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.
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.
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.
Syntax
-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:
-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.