Unix Assignment3 by Srishti
Unix Assignment3 by Srishti
Unix Assignment3 by Srishti
MCA -2
IV semester
2190212054
Q1) State general structure of an awk and discuss about its
content.
AWK is abbreviated from the names of the developers – Aho, Weinberger, and
Kernighan.
Contents of AWK-
The AWK language is a data-driven scripting language consisting of a set of actions to
be taken against streams of textual data – either run directly on files or used as part of
a pipeline – for purposes of extracting or transforming text, such as producing formatted
reports. The language extensively uses the string datatype, associative arrays (that is,
arrays indexed by key strings), and regular expressions. While AWK has a limited
intended application domain and was especially designed to support one-liner
programs, the language is Turing-complete.
AWK is line oriented. That is, the pattern specifies a test that is performed with each line
read as input. If the condition is true, then the action is taken. The default pattern is
something that matches every line. This is the blank or null pattern. Two other important
patterns are specified by the keywords "BEGIN" and "END". These two words specify
actions to be taken before any lines are read, and after the last line is read. The AWK
program below:
adds one line before and one line after the input file. A typical AWK program:
The input is split into records, where by default records are separated by newline
characters so that the input is split into lines.
The program tests each record against each of the conditions in turn, and executes
the action for each expression that is true. Either the condition or the action may be
omitted. The condition defaults to matching every record. The default action is to print
the record. This is the same pattern-action structure as sed.
What can we do by using AWK?
1. AWK operations-
(a) Scans a 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:
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 for the input field separator.
AWK’s built-in variables include the field variables- $1, $2, $3, that breaks a
line of text into individual words or pieces called fields.
Number of fields variable (NF)
Number of records variable (NR)
Record separator variable (RS)
Output record separator variable (ORS)
Current filename variable (FILENAME)
When a script does not work properly, we need to determine the location of the
problem. The UNIX/Linux shells provide a debugging mode. Run the entire script in
debug mode or just a portion of the script. To run an entire script in debug mode, add -
x after the #!/bin/[shell] on the first line:
For Example :
#!/bin/sh –x
To run an entire script in debug mode from the command line, add a -x to the sh
command used to execute the script: $ sh -x script_name
Run several portions of a script in debug mode by placing the set -x option at the point
where debugging is to begin and the set +x option where you want it to stop. The debug
options are shown in Table below:
set –x : Prints the statements after interpreting meta characters and variables
set –v : Prints the statements before interpreting meta characters and variables
# cat script.sh
#!/bin/sh
set -x
echo "Your home is : $HOME"
set +x
Directory-
Suppose if we create a Linux directory that can be used by all the users of the Linux
system for creating files and users can create, delete or rename files according to their
convenience in this directory. There exists, for example, /tmp directory in the Linux
system that can be used by different Linux users to create temporary files. What will
happen If a user accidentally or deliberately deletes (or rename) a file created by some
other user in this directory. Well, to avoid these kind of issues, the concept of sticky bit
is used.
A Sticky bit is a permission bit that is set on a file or a directory that lets only the
owner of the file/directory or the root user to delete or rename the file. No other
user is given privileges to delete the file created by some other user.
Create a directory and provide all the users read-write-execute access to it:
#mkdir allAccess
For example:
# ls -l allAccess/
total 0
The files user_file_0 and user_file_1 are created by different users but have read-write-
execute access on for all the users. This means that the user ‘guest’ can delete or
rename the file created by user ‘guest-2’.In order to avoid this, sticky bit can be set on
the directory allAccess.
Now, turn ON the sticky bit on the directory by using +t flag of chmod command.
# chmod +t allAccess/
# ls -ld allAccess/
drwxrwxrwt 2 srishti srishti 4096 Oct 24 16:19 allAccess/
As can be observed, a permission bit ‘t’ is introduced in the permission bits of the
directory.
Now, if the user ‘guest’ tries to rename the file ‘user_file_1’, here is what happens :
$ mv /home/srishti/allAccess/user_file_1 /home/himanshu/allAccess/user_file_0
Here is an example :
# chmod -t allAccess/
# ls -ld allAccess/
Different OS behave differently with sticky bits as explained in this wikipedia article. For
example, Linux only looks for sticky bit if a user tries to rename a file. It will not check
the sticky bit if a file is being deleted.
grep command-
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:
Options Description-
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
-o : Print only the matched parts of a matching line, with each such part on a separate output
line.
EXAMPLES-
$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:
2. Displaying the count of number of matches : We can find the number of lines that
matches the given string/pattern.
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
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.
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.
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.
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.
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
1) 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.
2) 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.
Q6) What are the possible redirection operators? Give an example for each.
Redirection is a feature in Unix/Linux such that when executing a command, you can
change the standard input/output devices. The basic workflow of any Linux command is
that it takes an input and give an output.
These allow you to control the input and output of your commands. They can appear
anywhere within a simple command or may follow a command. Redirections are
processed in the order they appear, from left to right.
sed awk
The sed is a command line utility that The awk is a command line utility designed
parses and transforms text, using a for text processing that allows writing
simple, compact programming language. effective programs in the form of
statements.
Unix has a graphical user interface similar to the Windows operating system that makes
it easy for navigation and a good supportive environment. The internal design view of
this operating system can be known from its architecture. The architecture of this
operating system is four layered. It consists of Hardware, Kernel, System Call
interface(shell) and application libraries/tools, utilities, etc…The kernel controls the
hardware of the computer and resides at the core of the architecture. System calls acts
as the interface between the kernel and other libraries. These libraries include general
functions and built on top of the system calls. Shell is a special application that provides
an interface to the other applications of the architecture.
Kernel
For this operating system, Kernel is the central core that interacts directly with the
hardware of the system. The main functions of Kernel are-
Computer hardware such as memory, disc, printers, etc.. are controlled by the
kernel.
The kernel schedules the processes, control and executes various user-defined
tasks.
Manages the data storage and control the computer accesses by several users.
The kernel is composed of several sub-components such as configurations
including boot code, device drivers to control hardware, header files.
Shell
It is the interface between the user and the kernel. Users can interact with the shell
using shell commands. Shell has two main responsibilities which include interpreting the
commands given by the users and execute them using the kernel, providing
programming ability to the users to write shell commands for a shell script to perform
specific tasks.
Commands
Some of the major categories of commands used by the Unix operating system are –
‘sh’ – shell commands providing a primary user interface, ‘utilities’ forming the core
toolkit of Unix commands includes sub-categories such as system utilities supporting
administrative tools and User utilities for environment management tools.