Linux Programming CA
UNIT - I
1. (a) Describe in detail about the structure of LINUX.
The structure of Linux typically involves a kernel, which is the core of the operating system,
interacting directly with hardware. Above the kernel are the system calls, which provide an
interface for user programs to interact with the kernel. The shell acts as a command-line
interpreter, allowing users to execute commands and scripts. Beyond the shell, there are
utilities and applications that provide various functionalities. The file system organizes data in
a hierarchical structure.
1. (b) What are the features of Linux operating system
Features of the Linux operating system include:
● Open Source: Its source code is freely available for use, modification, and distribution.
● Multi-user: Multiple users can access and work on the system simultaneously.
● Multitasking: It can run multiple processes or applications concurrently.
● Portability: It can run on a wide range of hardware platforms.
● Security: It offers robust security features, including user authentication and file
permissions.
● Stability: It is known for its stability and reliability, often running for long periods without
needing reboots.
● Flexibility: It can be customized for various purposes, from embedded systems to
supercomputers.
● Extensive Networking: It has strong networking capabilities built-in.
2. What information is presented when the following commands are entered?
● (a) date: When date is entered, the current system date and time are presented.
● (b) who: When who is entered, information about users currently logged into the system
is presented, including their terminal line, login time, and originating host.
● (c) passwd: When passwd is entered (usually without arguments by a user), it prompts
the user to change their password. If run by root, it can change other users' passwords.
The command itself doesn't "present information" in the same way as date or who, but
facilitates a password change.
● (d) bc: When bc (arbitrary-precision calculator) is entered, it launches an interactive
mathematical expression processor, allowing users to perform calculations.
● (e) script: When script is entered, it starts a session recording all terminal input and
output into a specified file. It presents the user with a new shell session.
3. (a) Define vi Editor and explain its modes.
The vi editor is a powerful, screen-oriented text editor commonly found on Unix-like operating
systems. It operates in distinct modes:
● Command Mode (Normal Mode): This is the default mode when vi starts. In this mode,
keystrokes are interpreted as commands (e.g., for navigation, deletion, copying, pasting).
Text cannot be directly entered here.
● Insert Mode: To enter text, you must switch to this mode from command mode (e.g., by
pressing i, a, o). In insert mode, typed characters are inserted into the document.
● Last Line Mode (Ex Mode): This mode is entered from command mode by pressing
colon (:). It allows you to execute more complex commands, such as saving files (:w),
quitting (:q), or searching (:/pattern).
3. (b) Brief about the commands used in the vi Editor.
Common commands in vi editor include:
● Navigation: h (left), j (down), k (up), l (right), w (next word), b (previous word), G (go to
end of file), gg (go to start of file).
● Insertion: i (insert at cursor), a (append after cursor), o (open new line below).
● Deletion: x (delete character), dd (delete line), dw (delete word).
● Copy/Paste: yy (yank/copy line), p (paste after cursor).
● Undo: u (undo last change).
● Save/Quit: :w (write/save), :q (quit), :wq (write and quit), :q! (quit without saving).
4. What are the file types available in Linux? Discuss file operators with suitable examples.
Common file types in Linux include:
● Regular files (-): These are ordinary files that contain data, such as text files, executable
programs, or images. Example: ls -l mydocument.txt would show -rw-r--r--.
● Directories (d): These are special files that contain references to other files and
directories. Example: ls -l mydirectory would show drwxr-xr-x.
● Symbolic links (l): Also known as soft links, these are pointers to other files or
directories. Example: ls -l mylink would show lrwxrwxrwx.
● Hard links (-): These are direct references to a file's inode, meaning multiple names can
point to the same file content.
● Block device files (b): Represent block devices like hard drives or CD-ROM drives.
Example: /dev/sda1.
● Character device files (c): Represent character devices like terminals or printers.
Example: /dev/tty.
● Named pipes (p): Also known as FIFOs (First-In, First-Out), used for inter-process
communication.
● Sockets (s): Used for inter-process communication across a network.
File operators are symbols used in shell scripting or command line for various purposes, often
related to file testing or redirection. Examples:
● >: Redirects standard output to a file, overwriting existing content. echo "hello" > file.txt
● >>: Appends standard output to a file. echo "world" >> file.txt
● <: Redirects standard input from a file. sort < unsorted.txt
● |: Pipes the output of one command as the input to another. ls -l | grep .txt
● -f file: True if file exists and is a regular file. [ -f mydata.txt ]
● -d directory: True if directory exists and is a directory. [ -d myfolder ]
● -e file: True if file exists. [ -e myfile.txt ]
5. (a) Explain the security levels provided in Linux environment. How to change permissions of
a file?
Linux security levels are primarily based on:
● User Accounts: Each user has a unique username and password, authenticating them to
the system.
● Groups: Users can be members of one or more groups, which simplify managing
permissions for multiple users.
● File Permissions (Discretionary Access Control - DAC): This is the fundamental level
of access control, determining who can read, write, or execute files and directories.
Permissions are set for three categories: owner, group, and others.
● Ownership: Every file and directory has an owner (a user) and a group.
● SELinux/AppArmor (Mandatory Access Control - MAC): These provide an additional
layer of security by enforcing policies that restrict what programs and users can do,
regardless of standard file permissions.
To change permissions of a file, the chmod command is used. It can be used with symbolic
modes or octal (numeric) modes.
● Symbolic mode: chmod u+w filename (add write permission for owner), chmod o-r
filename (remove read permission for others).
● Octal mode: Permissions are represented by a three-digit octal number (read=4,
write=2, execute=1). chmod 755 filename (owner rwx, group rx, others rx), chmod 644
filename (owner rw, group r, others r).
5. (b) Brief umask command.
The umask command sets the default file permission mask for newly created files and
directories. The umask value is subtracted from the default maximum permissions (666 for
files, 777 for directories) to determine the actual permissions. For example, if umask is 0022,
new files will have 644 permissions (666 - 022) and new directories will have 755 permissions
(777 - 022).
6. What is user and group in Linux? Explain the related commands for changing
ownership and group.
● User: In Linux, a user is an entity that can log into the system and own files and
processes. Each user has a unique user ID (UID).
● Group: A group is a collection of users. Groups simplify permission management; instead
of setting permissions for each individual user, you can set permissions for a group, and
all members of that group inherit those permissions. Each group has a unique group ID
(GID).
Related commands for changing ownership and group:
● chown (change owner): Used to change the user owner of files or directories.
○ chown newowner filename: Changes the owner of filename to newowner.
○ chown newowner:newgroup filename: Changes both the owner and group of
filename.
● chgrp (change group): Used to change the group owner of files or directories.
○ chgrp newgroup filename: Changes the group of filename to newgroup.
7. Write about the operations unique to directories alone.
Operations unique to directories alone include:
● Creating: mkdir directoryname (creates a new directory).
● Removing (empty): rmdir directoryname (removes an empty directory).
● Changing current directory: cd directoryname (changes the shell's current working
directory).
● Listing contents: ls directoryname (lists the files and subdirectories within a directory).
● Traversing: Moving through the directory hierarchy.
● Setting sticky bit: Prevents users from deleting or renaming files in a directory unless
they own the file or the directory.
8. Write about the operations that can be performed on both directories and file.
Operations that can be performed on both directories and files include:
● Listing (detailed): ls -l filename/directoryname (shows detailed information).
● Changing permissions: chmod permissions filename/directoryname.
● Changing ownership: chown owner filename/directoryname.
● Changing group ownership: chgrp group filename/directoryname.
● Moving/Renaming: mv oldname newname.
● Copying: cp source destination (for directories, cp -r is typically used).
● Deleting (recursively for directories): rm filename or rm -r directoryname.
● Viewing metadata: stat filename/directoryname.
9. What is meant by path and pathname in Linux? Explain them in detail.
In Linux, a path is a unique location of a file or directory within the file system. A pathname is
the string representation of that path. There are two types of pathnames:
● Absolute Pathname: Starts from the root directory (/). It specifies the exact location of a
file or directory from the topmost level of the file system. It is unambiguous regardless of
the current working directory.
○ Example: /home/user/documents/report.txt or /var/log.
● Relative Pathname: Specifies the location of a file or directory relative to the current
working directory. It does not start with /.
○ Example: If your current directory is /home/user/documents, then report.txt refers to
/home/user/documents/report.txt, and ../pictures refers to /home/user/pictures.
○ ./ refers to the current directory.
○ ../ refers to the parent directory.
10. a) Distinguish between time - sharing and client/server environment.
● Time-sharing environment: In a time-sharing system, a single powerful central
computer (mainframe or server) is shared among multiple users simultaneously. The
operating system rapidly switches between users, giving each the illusion of having
dedicated access. Resources are shared, and users typically interact with the system via
dumb terminals. The focus is on maximizing CPU utilization and providing interactive
computing to many users on one machine.
● Client/server environment: This model distributes computing tasks between clients
(which request services) and servers (which provide services). Clients send requests
(e.g., for data, processing) to servers, and servers process these requests and send back
responses. This architecture is highly scalable and allows for specialized servers (e.g., file
servers, database servers, web servers) and diverse client types (e.g., desktop
applications, web browsers, mobile apps). The focus is on distributed processing and
resource sharing across a network.
10. b) Name the two categories of regular files. Does Linux recognize the difference between
these two categories? Explain your answer.
The two categories of regular files are generally considered:
1. Text files: Files containing human-readable characters, typically ASCII or Unicode.
2. Binary files: Files containing non-human-readable data, such as executable programs,
images, audio, or compressed archives.
Linux, at the file system level, does not inherently distinguish between these two categories
based on their content type in the same way an application might. All regular files are just
sequences of bytes. However:
● The file command can attempt to determine the content type by looking for magic
numbers or patterns within the file.
● Executable permissions are explicitly set for binary executables.
● Text processing utilities (like grep, awk, sed) are designed to work with text files and may
behave unpredictably or inefficiently with binary files.
● Applications that open files (e.g., a text editor vs. an image viewer) interpret the bytes
differently based on their expected format.
So, while the kernel and file system treat them similarly as streams of bytes, higher-level tools
and user conventions recognize and operate differently based on the expected content
format.
10. c) Write syntax for changing ownership and group name on a given file/s
The syntax for changing ownership and group name using chown is:
chown [OPTIONS] USER[:GROUP] FILE...
Example: chown john:developers myfile.txt
10. d) Discuss about various modes of vi editor.
(Answered in 3. (a) above.)
10. e) Write the command for the following: To display time in format hour:minute:second
The command to display time in the format hour:minute:second is:
date +%H:%M:%S
UNIT 2
1. a) In how many ways a command can be executed?
A command can be executed in several ways in Linux:
● Directly typing the command in the shell prompt and pressing Enter.
● As part of a shell script.
● Through a graphical user interface (GUI) by clicking icons or menu items that trigger
command execution.
● Via process substitution or command substitution.
● Through cron jobs for scheduled execution.
● From within another program using system calls (e.g., system() in C).
● As an alias or a function defined in the shell.
1. b) write a shell script to count number of lines present in a text file
Bash
#!/bin/bash
# Script to count lines in a text file
if [ -z "$1" ]; then
echo "Usage: $0 <filename>"
exit 1
fi
filename="$1"
if [ -f "$filename" ]; then
line_count=$(wc -l < "$filename")
echo "The file '$filename' has $line_count lines."
else
echo "Error: File '$filename' not found or is not a regular file."
exit 1
fi
2. What is redirection? Explain it in detail.
Redirection in Linux allows you to change the standard input or standard output of a
command from its default location (keyboard for input, screen for output) to a file or another
command.
● Standard Input (stdin - file descriptor 0): By default, commands take input from the
keyboard. Input redirection (<) changes this to read from a file.
○ Example: sort < input.txt (sorts lines from input.txt instead of keyboard input).
● Standard Output (stdout - file descriptor 1): By default, command output goes to the
screen (terminal). Output redirection (>) sends it to a file.
○ command > file: Overwrites file with the command's stdout. Example: ls > files.txt.
○ command >> file: Appends the command's stdout to file. Example: echo "new line" >>
log.txt.
● Standard Error (stderr - file descriptor 2): Error messages typically go to the screen.
Error redirection sends them to a file.
○ command 2> error.log: Redirects stderr to error.log.
○ command &> all_output.log or command > output.log 2>&1: Redirects both stdout
and stderr to the same file.
3. (a) How quotes are used in Linux, explain with example.
Quotes are used in Linux shells to control word splitting, pathname expansion (globbing), and
variable expansion, particularly when dealing with strings that contain spaces or special
characters.
● Double Quotes ("): Preserve literal values of most characters within them. They do allow
variable expansion ($VAR), command substitution (`command` or $(command)), and
backslash escaping (\).
○ Example: my_var="Hello World"
○ echo "$my_var" outputs Hello World. If my_var wasn't quoted, echo $my_var would
output Hello World but could cause issues if my_var contained globbing characters.
○ echo "The date is $(date)" outputs The date is Wed Jun 11 02:53:01 AM WAT 2025.
● Single Quotes ('): Preserve the literal value of every character within them. No variable
expansion, command substitution, or backslash escaping occurs.
○ Example: my_var='Hello $USER'
○ echo '$my_var' outputs Hello $USER.
○ echo 'The date is $(date)' outputs The date is $(date).
● Backticks (`) or $(...) (Command Substitution): Execute the enclosed command and
substitute its output into the command line. $(...) is generally preferred as it handles
nesting better.
○ Example: file_list=$(ls -l) or file_list=ls -l``
3. (b) Brief about command line editing.
Command line editing refers to the ability to modify commands entered at the shell prompt
before executing them. Most modern Linux shells (like Bash or Zsh) use readline library
features for this, providing Emacs-like keybindings by default, though Vi-like bindings are also
available.
Common editing actions include:
● Navigation: Arrow keys (left/right to move cursor), Ctrl+a (beginning of line), Ctrl+e (end
of line).
● Deletion: Ctrl+k (delete from cursor to end of line), Ctrl+u (delete from cursor to
beginning of line), Ctrl+d (delete character under cursor).
● Pasting: Ctrl+y (paste last deleted text).
● History: Up/down arrow keys (navigate through previous commands), Ctrl+r (reverse
incremental search).
● Word jumps: Alt+f (forward a word), Alt+b (backward a word).
4. Explain in detail foreground and background jobs. Give example.
In a shell, processes can run in the foreground or background.
● Foreground Job: A job running in the foreground occupies the terminal. You cannot
execute other commands or interact with the shell until the foreground job completes or
is suspended. Standard input and output are directly connected to the terminal.
○ Example: grep "error" /var/log/syslog (The shell waits for grep to finish).
● Background Job: A job running in the background does not occupy the terminal. You
can continue to execute other commands while the background job runs. Its standard
input is usually detached (reading from /dev/null), and standard output/error often
continues to the terminal unless redirected.
○ To run a command in the background, append & to the command.
○ Example: find / -name "*.log" > ~/logs.txt 2>/dev/null & (The find command runs in the
background, and you get your shell prompt back immediately).
○ You can move a foreground job to the background by pressing Ctrl+z (suspends it)
and then typing bg (resumes it in the background).
○ Use jobs to list background jobs, fg %job_number to bring a background job to the
foreground, and kill %job_number to terminate a background job.
5. (a) Explain concatenate command with its options.
The cat command (short for concatenate) is primarily used to display file content, combine
multiple files, or create new files.
Common options:
● cat filename: Displays the content of filename to standard output.
○ Example: cat mydocument.txt
● cat file1 file2 > combined.txt: Concatenates file1 and file2 and redirects the combined
output to combined.txt.
● cat > newfile.txt: Creates a new file called newfile.txt. You type content, and press Ctrl+d
to save and exit.
● -n (or --number): Numbers all output lines.
○ Example: cat -n script.sh
● -b (or --number-nonblank): Numbers non-blank output lines.
● -s (or --squeeze-blank): Squeezes multiple adjacent blank lines into a single blank line.
● -E (or --show-ends): Displays $ at the end of each line.
● -T (or --show-tabs): Displays TAB characters as ^I.
● -v (or --show-nonprinting): Displays non-printing characters (except for tabs and
newlines) using ^ and M- notation.
● -A (or --show-all): Equivalent to -vET (show all non-printing characters, tabs, and line
endings).
5. (b) Explain sort command with its options.
The sort command sorts lines of text files. By default, it sorts lines alphabetically based on the
entire line.
Common options:
● sort filename: Sorts the lines in filename and displays the sorted output.
○ Example: sort names.txt
● -r (or --reverse): Sorts in reverse (descending) order.
○ Example: sort -r numbers.txt
● -n (or --numeric-sort): Sorts numerically. Useful for files containing numbers.
○ Example: sort -n scores.txt
● -k N (or --key=N): Sorts based on the Nth field (column). Fields are typically delimited
by whitespace.
○ Example: sort -k 2 data.txt (sorts by the second column).
● -t CHAR (or --field-separator=CHAR): Specifies a different field delimiter character
instead of whitespace.
○ Example: sort -t ',' -k 1 data.csv (sorts by the first column, using comma as delimiter).
● -u (or --unique): Outputs only unique lines (removes duplicates).
○ Example: sort -u words.txt
● -f (or --ignore-case): Ignores case when sorting.
● -o OUTPUT_FILE (or --output=OUTPUT_FILE): Writes the sorted output to
OUTPUT_FILE instead of standard output.
○ Example: sort original.txt -o sorted.txt
6. What command is used for translating characters? Also explain its options with examples.
The tr command is used for translating or deleting characters. It reads from standard input,
transforms characters, and writes to standard output.
Syntax: tr [OPTION] SET1 [SET2]
SET1 and SET2 are character sets. tr translates characters in SET1 to corresponding
characters in SET2. If SET2 is shorter than SET1, the last character of SET2 is repeated.
Common options:
● Translation:
○ tr 'a-z' 'A-Z': Translates all lowercase letters to uppercase.
■ Example: echo "hello world" | tr 'a-z' 'A-Z' outputs HELLO WORLD.
○ tr '[:lower:]' '[:upper:]': Same as above using character classes.
○ tr -s ' ' '\n': Replaces multiple spaces with a single newline (useful for word counting).
■ Example: echo "one two three" | tr -s ' ' '\n' outputs each word on a new line.
● Deletion:
○ -d (or --delete): Deletes characters in SET1.
■ Example: echo "hello 123 world" | tr -d '0-9' outputs hello world.
○ tr -d '[:digit:]': Deletes all digits.
● Squeezing (compressing sequences of repeated characters):
○ -s (or --squeeze-repeats): Replaces each sequence of a repeated character that is in
SET1 with a single occurrence of that character.
■ Example: echo "Heeellooo" | tr -s 'o' outputs Heeello.
■ Example: echo "This is a test" | tr -s ' ' outputs This is a test.
7. What is a filter? Illustrate all filters with appropriate examples.
In Linux, a filter is a command that reads text from its standard input, performs some
transformation or processing on that text, and then writes the transformed text to its standard
output. Filters are designed to work well with pipes (|), allowing the output of one command to
become the input of another.
Illustrative examples of common filters:
● grep: Filters lines that match a pattern.
○ Example: cat access.log | grep "error" (shows lines containing "error").
● sort: Filters by sorting lines.
○ Example: ls -l | sort -k 5 -n (sorts files by size).
● uniq: Filters unique or duplicate lines.
○ Example: cat words.txt | sort | uniq (lists unique words).
● wc (word count): Filters by counting lines, words, or characters.
○ Example: cat my_text.txt | wc -l (counts lines).
● head: Filters by showing the beginning of files.
○ Example: cat long_file.txt | head -n 10 (shows the first 10 lines).
● tail: Filters by showing the end of files.
○ Example: cat log_file.txt | tail -n 5 (shows the last 5 lines).
● cut: Filters by extracting columns/fields.
○ Example: cat data.csv | cut -d ',' -f 1,3 (extracts first and third comma-separated
fields).
● sed (stream editor): Filters by performing text transformations.
○ Example: cat report.txt | sed 's/old_text/new_text/g' (replaces all occurrences of
"old_text" with "new_text").
● awk: Filters by processing text based on patterns and actions.
○ Example: cat data.txt | awk '{print $2}' (prints the second field of each line).
● tr: Filters by translating or deleting characters.
○ Example: cat file.txt | tr 'a-z' 'A-Z' (converts content to uppercase).
8. What information is presented when the following commands are entered?
● (a) cmp: When cmp is entered, it compares two files byte by byte and reports the first
differing byte and line number.
● (b) diff: When diff is entered, it compares two files line by line and outputs the
differences between them, indicating which lines need to be added, deleted, or changed
to make the files identical.
● (c) comm: When comm is entered, it compares two sorted files line by line and presents
output in three columns: lines unique to file1, lines unique to file2, and lines common to
both.
● (d) cut: When cut is entered, it extracts (cuts) specified sections (columns/fields) from
each line of input, typically delimited by a character.
● (e) paste: When paste is entered, it merges corresponding lines of multiple files into
single lines, typically separated by tabs.
9. Write a shell program for counting characters, words and line?
Bash
#!/bin/bash
# Script to count characters, words, and lines in a file
if [ -z "$1" ]; then
echo "Usage: $0 <filename>"
exit 1
fi
filename="$1"
if [ -f "<span class="math-inline">filename" \]; then
lines\=</span>(wc -l < "<span class="math-inline">filename"\)
words\=</span>(wc -w < "<span class="math-inline">filename"\)
chars\=</span>(wc -c < "$filename") # -m for characters in multi-byte locales
echo "File: $filename"
echo "Lines: $lines"
echo "Words: $words"
echo "Characters: $chars"
else
echo "Error: File '$filename' not found or is not a regular file."
exit 1
Fi
10. Explain (a) Aliases (b) Linux session
● (a) Aliases: An alias is a shortcut or a customized command name for a longer command
or a sequence of commands. It allows users to define their own commands to simplify
frequently used or complex commands, making them easier to remember and type.
Aliases are typically defined in shell configuration files (e.g., .bashrc, .zshrc).
○ Example: alias ll='ls -l'. Now, typing ll executes ls -l.
○ Example: alias cls='clear'
● (b) Linux session: A Linux session refers to the period during which a user is logged into
the system and interacting with it. It begins when a user successfully logs in (e.g., via a
terminal, SSH, or graphical login manager) and ends when the user logs out, the system
shuts down, or the connection is terminated. During a session, the user has access to
their environment variables, shell processes, and files, and can run commands. Multiple
sessions can run concurrently (e.g., different terminal windows, remote SSH
connections).
10. a) Define pipes.
Pipes (|) are a mechanism in Linux (and Unix-like systems) that allow the standard output of
one command to be directly fed as the standard input to another command. This creates a
"pipeline" where data flows from one program to the next, enabling complex operations by
chaining together simpler tools.
10. b) Explain tee command.
The tee command reads standard input, writes it to standard output, and simultaneously
writes it to one or more files. It's useful when you want to view the output of a command on
the screen while also saving it to a file.
● Example: ls -l | tee file_listing.txt (lists files to the screen and saves the same list to
file_listing.txt).
● Example: command | tee -a logfile.txt (appends the output to logfile.txt).
10. c) Compare and contrast the cmp command with the comm Command.
Feature cmp command comm command
Purpose Compares two files byte by Compares two sorted files
byte. line by line.
Output Reports the first byte and Produces three columns:
line number that differ. If unique to file1, unique to
files are identical, it file2, and common lines.
produces no output
(returns exit code 0).
Requirement Files can be unsorted. Both input files must be
sorted for correct results.
Use Case Quick check for file Finding commonalities or
identity; finding first differences between text
difference in binary or large files (e.g., lists, logs).
files.
Granularity Byte-level. Line-level.
10. d) If your login shell is korn shell, can you create another korn shell as the child
shell? What file descriptor designates the standard input stream, standard output
stream and the standard error stream?
● Yes, if your login shell is Korn shell (ksh), you can create another Korn shell (or any other
shell like bash or zsh) as a child shell. When you type ksh (or bash, zsh) at the prompt, a
new instance of that shell is launched as a child process of your current shell.
● File Descriptors:
○ Standard Input stream: 0
○ Standard Output stream: 1
○ Standard Error stream: 2
10. e) What does "tail" command do.
The tail command outputs the last part (lines or bytes) of files. By default, it displays the last
10 lines of a file. It is often used to monitor log files in real-time using its -f (follow) option.
UNIT 3
1. Differentiate atoms and operators in detail.
In the context of regular expressions:
● Atoms: Are the fundamental building blocks of regular expressions. They match a single
character or a set of characters. Atoms represent the literal characters or character
classes that the regex engine tries to match in the input text.
○ Examples:
■ a: Matches the literal character 'a'.
■ .: Matches any single character (except newline).
■ [abc]: Matches 'a', 'b', or 'c'.
■ \d: Matches any digit (0-9).
■ \s: Matches any whitespace character.
■ \(: Matches a literal parenthesis.
● Operators (Quantifiers and Anchors): Are special characters or sequences that act
upon atoms or groups of atoms to specify how many times an atom should repeat, or
where a pattern should occur within a line or string. They do not match characters
themselves but modify the matching behavior of atoms.
○ Quantifiers:
■ *: Matches zero or more occurrences of the preceding atom. (e.g., a* matches "",
"a", "aa", etc.)
■ +: Matches one or more occurrences. (e.g., a+ matches "a", "aa", etc.)
■ ?: Matches zero or one occurrence. (e.g., a? matches "" or "a")
■ {n}: Matches exactly n occurrences.
■ {n,}: Matches n or more occurrences.
■ {n,m}: Matches between n and m occurrences.
○ Anchors:
■ ^: Matches the beginning of a line.
■ $: Matches the end of a line.
■ \b: Matches a word boundary.
○ Alternation:
■ |: Acts as an OR operator. (e.g., cat|dog matches "cat" or "dog")
○ Grouping:
■ (...): Groups atoms together so an operator can apply to the entire group. Also
creates a backreference.
2. a) What is grep? How it is useful?
grep (Global Regular Expression Print) is a command-line utility in Unix-like systems used to
search for lines that match a specified pattern in one or more files. The pattern is typically a
regular expression.
It is useful for:
● Searching logs: Quickly finding specific error messages or events in large log files.
● Filtering output: Processing the output of other commands to extract relevant lines.
● Code analysis: Finding specific function calls, variable definitions, or patterns in source
code.
● Data extraction: Extracting data that conforms to a specific format from text files.
● System administration: Locating configuration settings or checking process status.
2. b) Illustrate grep command functionality with following options (-c,-l,-n,-S,-V,-X,-f)
Let's assume a file named data.txt with the following content:
apple
banana
Apple pie
orange
grape
Apple juice
● -c (or --count): Prints only a count of the lines that match the pattern.
○ grep -c "apple" data.txt outputs 1 (case-sensitive)
● -l (or --files-with-matches): Lists only the names of files that contain matches.
○ grep -l "Apple" data.txt outputs data.txt
● -n (or --line-number): Prints the line number along with the matching line.
○ grep -n "Apple" data.txt outputs:
3:Apple pie
6:Apple juice
● -S (or --binary-files=skip): (Not a standard grep option; typically -I or -a relate to
binary files). Assuming it relates to how grep handles binary files, -I implies "ignore binary
files" by default. If it was intended as a custom option for "silent" or "suppress errors," it's
non-standard. For standard grep, -I would skip binary files, which is its default behavior
for many patterns.
● -V (or --version): Prints the grep version information and exits.
○ grep -V outputs: grep (GNU grep) 3.10 (or similar version info).
● -X: (Not a standard grep option in most common versions like GNU grep). This option
might be specific to certain grep implementations or custom scripts. Standard grep does
not have a -X option.
● -f FILE (or --file=FILE): Obtains patterns from FILE, one pattern per line.
○ Suppose patterns.txt contains:
apple
grape
○ grep -f patterns.txt data.txt outputs:
apple
grape
3. Describe "Dot, class, anchor, back reference and single character" atoms.
This question is a bit mixed, as "Dot, class, anchor" are types of atoms/metacharacters, while
"back reference" is a mechanism related to grouping, and "single character" is a basic atom
type.
● Dot (.): An atom that matches any single character except a newline character.
○ Example: a.b matches "aab", "acb", "a1b", etc.
● Character Class ([...] or [:...:]): An atom that matches any single character within a
specified set.
○ Square brackets [] define a custom character set. Example: [aeiou] matches any
single vowel.
○ Predefined character classes ([:alnum:], [:digit:], [:lower:], [:upper:], [:space:], etc.)
match characters belonging to common categories. Example: [[:digit:]] matches any
single digit.
● Anchor (^ and $): Special characters that do not match actual characters but match a
position within a line or string.
○ ^: Matches the beginning of the line. Example: ^start matches lines that begin with
"start".
○ $: Matches the end of the line. Example: end$ matches lines that end with "end".
● Back Reference (\N): A mechanism to refer back to a previously captured group (a
subexpression enclosed in parentheses). \N matches the exact text that was matched by
the Nth capturing group.
○ Example: (.)\1 matches two identical consecutive characters (e.g., "aa", "bb"). Here, (.)
captures any character into group 1, and \1 refers to whatever was captured by group
1.
● Single Character (Literal Character): The simplest form of an atom, where a character
literally matches itself.
○ Example: a matches the character 'a'. 5 matches the character '5'.
4. Differentiate among grep, egrep and fgrep.
These are different versions or modes of the grep command, primarily differing in the type of
regular expressions they support and their performance characteristics.
● grep (Basic Regular Expressions - BRE):
○ Uses Basic Regular Expressions (BREs).
○ Metacharacters like ?, +, {, |, ( and ) usually need to be escaped with a backslash (\?,
\+, etc.) to be interpreted as special regex operators. Otherwise, they are treated as
literal characters.
○ Example: grep "a\{2\}" file.txt (matches "aa").
● egrep (Extended Regular Expressions - ERE):
○ Uses Extended Regular Expressions (EREs).
○ Metacharacters like ?, +, {, |, ( and ) are interpreted as special regex operators by
default, without needing to be escaped. This makes writing more complex patterns
easier.
○ egrep is essentially equivalent to grep -E.
○ Example: egrep "a{2}" file.txt (matches "aa").
○ Example: egrep "word1|word2" file.txt (matches lines containing "word1" or "word2").
● fgrep (Fixed Strings - FST):
○ Treats all patterns as fixed strings, not regular expressions. No metacharacters are
interpreted as special regex operators; they are all matched literally.
○ fgrep is essentially equivalent to grep -F.
○ It is generally faster for simple string searches because it doesn't have the overhead
of regex parsing.
○ Example: fgrep "a.b" file.txt will literally search for "a.b", not "a" followed by any
character followed by "b".
5. a) How does an "eval" command works.
The eval command is a shell built-in that takes its arguments, constructs a command string
from them, and then executes that command string in the current shell context. It performs an
extra layer of interpretation.
It's often used when a command or its arguments are built dynamically as strings or stored in
variables, and those strings need to be interpreted as shell commands.
Example:
Bash
command_str="ls -l"
eval "<span class="math-inline">command\_str" \# Executes 'ls \-l'
file\_num\=1
var\_name\="file</span>{file_num}_name"
value="document.txt"
eval "$var_name=\"$value\"" # Creates a variable named file1_name and sets its value
echo "$file1_name" # Outputs: document.txt
While powerful, eval should be used with caution, as it can be a security risk if used with
untrusted input, as it allows arbitrary code execution.
5. b) Define Environmental variable? Explain about Environmental variables used in
korn shell.
● Environmental Variable: An environmental variable (or environment variable) is a named
value stored by the operating system that can be accessed by processes and programs.
They are part of the environment in which a process runs and are typically used to
configure system behavior or provide information to programs (e.g., path to executables,
user's home directory, locale settings).
● Environmental Variables in Korn Shell (ksh): In ksh, environmental variables are
distinct from shell variables. Environmental variables are inherited by child processes,
while shell variables are not unless explicitly exported.
○ Common ksh environment variables:
■ PATH: A colon-separated list of directories where the shell searches for
executable commands.
■ HOME: The path to the current user's home directory.
■ USER (or LOGNAME): The current username.
■ SHELL: The path to the user's login shell.
■ PWD: The current working directory.
■ OLDPWD: The previous working directory.
■ EDITOR: The default text editor to be used (e.g., vi, nano).
■ MAIL: Path to the user's mailbox file.
■ TERM: The type of terminal emulation being used.
■ PS1: The primary shell prompt string.
■ HISTFILE: The path to the history file where commands are stored.
■ HISTSIZE: The number of commands to remember in the history list.
○ Variables are set using VAR_NAME=value. To make them environmental, they must be
exported: export VAR_NAME. You can also declare and export at once: export
VAR_NAME=value.
○ You can view all environmental variables with the env command or printenv.
6. How do you use a variable in korn shell? Describe different types of variables used in korn
shell.
In Korn shell (ksh), variables are used to store data, which can then be referenced by their
name.
● Setting a variable: variable_name=value (no spaces around =).
○ Example: my_string="Hello"
○ Example: count=10
● Accessing a variable: Prefix the variable name with $ (dollar sign).
○ Example: echo $my_string outputs Hello.
○ Example: echo "Current count: $count"
Different types of variables used in Korn shell:
● Shell Variables: These are local to the current shell instance. They are not automatically
passed to child processes.
○ Example: local_var="I'm local"
● Environmental Variables: These are shell variables that have been exported, making
them available to any child processes spawned from the current shell.
○ Example: export PATH="/usr/local/bin:$PATH"
● Positional Parameters: Special variables ($1, $2, $3, etc.) that hold the arguments
passed to a script or function. $0 holds the script's name.
○ Example: If myscript.sh arg1 arg2, then $1 is arg1, $2 is arg2.
● Special Variables: Predefined variables that hold specific shell-related information.
○ $#: Number of positional parameters.
○ $@: All positional parameters as separate words.
○ $*: All positional parameters as a single word.
○ $?: Exit status of the last executed command.
○ $$: Process ID (PID) of the current shell.
○ $!: PID of the last background command.
○ $-: Current options set in the shell.
● Array Variables: (Associative arrays available in ksh93 and later). Can store multiple
values indexed by numbers or strings.
○ Indexed array: my_array[0]="first" my_array[1]="second"
○ Associative array: typeset -A my_assoc_array; my_assoc_array["key"]="value"
● Integer Variables: Declared with integer or typeset -i for arithmetic operations.
○ Example: integer i=0; i=i+1
● Read-only Variables: Declared with readonly or typeset -r. Their values cannot be
changed after declaration.
○ Example: readonly MY_CONSTANT="fixed_value"
7. a) How do you check exit status of a command.
The exit status (or exit code) of the last executed command is stored in the special shell
variable $?. A value of 0 typically indicates successful execution, while any non-zero value
(1-255) usually indicates an error or abnormal termination.
Example:
Bash
ls /nonexistent_directory # This command will fail
echo "Exit status: $?" # Will output a non-zero value (e.g., 1 or 2)
ls /tmp # This command will succeed
echo "Exit status: $?" # Will output 0
7. b) Illustrate decision making statements used in korn shell.
Korn shell provides if/elif/else and case statements for decision making.
● if/elif/else statement:
Code snippet
#!/bin/ksh
echo "Enter a number:"
read num
if (( num > 10 )); then
echo "$num is greater than 10."
elif (( num == 10 )); then
echo "$num is equal to 10."
else
echo "$num is less than 10."
fi
# Using test command or [ ]
if [ -f "myfile.txt" ]; then
echo "myfile.txt exists."
fi
Note: ((...)) is for arithmetic evaluation, [[...]] is for conditional expressions (more powerful
than []), and [ ... ] (the test command) is for simpler conditional expressions.
● case statement: Used for multi-way branching based on pattern matching.
Code snippet
#!/bin/ksh
echo "Enter a color (red/green/blue):"
read color
case "$color" in
red|RED)
echo "You chose red."
;;
green|GREEN)
echo "You chose green."
;;
blue|BLUE)
echo "You chose blue."
;;
*) # Default case
echo "Unknown color."
;;
esac
8. What is an expression? Describe about different expression forms used in korn shell.
In programming, an expression is a combination of values, variables, operators, and functions
that evaluates to a single result.
Different expression forms in Korn shell:
● Arithmetic Expressions (((...)) or let): Used for performing integer arithmetic.
○ (( result = 5 + 3 ))
○ let result="5 * 3"
○ if (( a > b )); then ... fi
● Conditional Expressions ([[...]] or [... ]): Used for testing conditions, typically in if or
while statements.
○ [[ "$name" = "John" ]]: String comparison.
○ [[ -f "file.txt" ]]: File test (checks if file exists).
○ [ "$var" -eq 10 ]: Numeric comparison (using -eq, -ne, -gt, -lt, etc.).
● String Expressions: Involve string manipulation and comparison.
○ string1 = string2: Checks if strings are equal.
○ string1 != string2: Checks if strings are not equal.
○ string1 < string2: Lexicographical comparison.
○ Pattern matching using == or != with wildcards within [[...]].
■ [[ "$filename" == *.txt ]]: Checks if filename ends with .txt.
● Command Substitution ($(command) or `command`): The output of a command
becomes part of an expression.
○ today=$(date +%Y-%m-%d)
● Parameter Expansion (${var}): Used to access variable values and perform various
manipulations like substring extraction, length calculation, or default value assignment.
○ ${#my_string}: Length of string.
○ ${my_string#pattern}: Remove shortest matching prefix.
9. a) How do for-in and until loops work in korn shell?
● for-in loop: The for-in loop iterates over a list of items (words, files, numbers, etc.). For
each item in the list, the commands within the loop body are executed, with the loop
variable taking on the value of the current item.
Code snippet
#!/bin/ksh
# Loop over a list of fruits
for fruit in apple banana cherry; do
echo "I like $fruit."
done
# Loop over files in a directory
for file in *.txt; do
echo "Processing $file"
# ... commands ...
done
● until loop: The until loop executes a block of commands repeatedly as long as the test
condition evaluates to false. It continues to loop until the condition becomes true.
Code snippet
#!/bin/ksh
count=0
until (( count >= 5 )); do
echo "Count is: $count"
(( count = count + 1 ))
done
echo "Loop finished."
9. b) What are the special parameters and variables used in kornshell.
(Answered in 6. above, under "Special Variables".)
10. What would be the effect of the following commands:
Assuming file1 exists and contains text.
● (a) grep "^[A-Z]" file1: This command would print all lines from file1 that start with an
uppercase letter (A through Z).
● (b) egrep "LINUX|Linux|linux" file1: This command would print all lines from file1 that
contain the word "LINUX" (all uppercase), "Linux" (capital L), or "linux" (all lowercase).
egrep uses Extended Regular Expressions, so | acts as an OR operator.
● (c) grep "Linux$" file1: This command would print all lines from file1 that end with the
word "Linux".
● (d) grep "^Linux$" file1: This command would print all lines from file1 that contain only
the word "Linux" (and nothing else, not even leading/trailing spaces). The line must start
and end with "Linux".
● (e) grep ^...$ file1: This command would print all lines from file1 that consist of exactly
three characters. The . matches any single character (except newline), and ^ and $
anchor the match to the beginning and end of the line respectively.
UNIT 4
1. Narrate how variable evaluation and substitution is done in korn shell with examples.
Variable evaluation and substitution in Korn shell (ksh) is the process by which the shell
replaces a variable's name with its stored value. This occurs during command line parsing.
● Basic Substitution: When the shell encounters a $ followed by a variable name (e.g.,
$VAR or ${VAR}), it replaces that expression with the current value of the variable.
○ Example:
Code snippet
name="Alice"
echo "Hello, $name!" # Output: Hello, Alice!
● Brace Expansion ({VAR}): Using braces around the variable name (${VAR}) is generally
safer and often required in complex scenarios, such as when concatenating strings
directly after a variable, to avoid ambiguity.
○ Example:
Code snippet
fruit="apple"
echo "${fruit}s" # Output: apples (without braces, `echo $fruits` would look for
variable `fruits`)
● Default Value Substitution:
○ ${VAR:-word}: If VAR is unset or null, word is substituted. VAR itself remains
unchanged.
■ Example: echo ${my_var:-"default_value"}
○ ${VAR:=word}: If VAR is unset or null, word is substituted, and VAR is set to word.
■ Example: echo ${my_var:="set_default"}
○ ${VAR:?message}: If VAR is unset or null, message is printed to standard error, and
the shell exits.
■ Example: echo ${REQUIRED_VAR:?"Error: REQUIRED_VAR is not set"}
○ ${VAR:+word}: If VAR is set and not null, word is substituted. Otherwise, nothing.
■ Example: echo ${my_var:+"has_value"}
● Substring Extraction:
○ ${VAR:offset}: Substring starting from offset.
○ ${VAR:offset:length}: Substring of length starting from offset.
■ Example: path="/usr/local/bin"; echo ${path:5:5} (Output: local)
● Pattern Removal:
○ ${VAR#pattern}: Removes the shortest matching pattern from the beginning of VAR.
○ ${VAR##pattern}: Removes the longest matching pattern from the beginning of VAR.
○ ${VAR%pattern}: Removes the shortest matching pattern from the end of VAR.
○ ${VAR%%pattern}: Removes the longest matching pattern from the end of VAR.
■ Example: filename="document.tar.gz"; echo ${filename%.gz} (Output:
document.tar)
■ Example: echo ${filename%%.*} (Output: document)
● Length:
○ ${#VAR}: Returns the length of the string VAR.
■ Example: text="hello"; echo ${#text} (Output: 5)
● Command Substitution: (See Unit 2, Question 3.a)
○ today=$(date)
2. a) What is here document operator? write a shell script using it.
A here document (also known as a "here-string" or "here-script") is a form of input redirection
that allows you to provide multiple lines of input to a command as if they were typed on the
keyboard, directly within the script itself. It's specified by << DELIMITER, where DELIMITER is a
string (e.g., EOF, STOP). All lines following the << DELIMITER until DELIMITER is encountered
on a line by itself (with no leading/trailing spaces) are treated as input to the command.
Shell script using here document:
Bash
#!/bin/ksh
# This script creates a sample configuration file using a here document.
cat << EOF > my_config.conf
# My Application Configuration
[Database]
Host=localhost
Port=5432
User=admin
Password=secure_pass
[Logging]
Level=INFO
FilePath=/var/log/myapp.log
EOF
echo "Configuration file 'my_config.conf' created successfully."
# Another example: providing multi-line input to sed
sed 's/old/new/g' << 'END_SED_INPUT'
This is an old line.
This is another old line.
A third line with old.
END_SED_INPUT
2. b) Define function. Describe How do you use functions in korn shell.
● Function: In programming, a function is a block of organized, reusable code that
performs a specific task. Functions allow for modularity, reusability, and easier
maintenance of code by breaking down large problems into smaller, manageable pieces.
● Using functions in Korn shell (ksh): Functions in ksh are defined using the function
keyword or the funcname () syntax. They are executed by simply calling their name.
○ Definition Syntax 1 (recommended for ksh):
Code snippet
function my_function {
# Commands inside the function
echo "Hello from my_function!"
echo "Arguments received: $@"
return 0 # Exit status of the function
}
○ Definition Syntax 2 (POSIX compliant, compatible with bash):
Code snippet
another_function () {
echo "This is another function."
}
○ Calling a function:
Code snippet
my_function "arg1" "arg2"
another_function
○ Function Scope: Variables declared inside a function are global by default unless
declared with typeset (or local in ksh93).
○ Arguments: Functions accept arguments just like scripts, using positional
parameters ($1, $2, etc.). $# is the number of arguments, and $@ refers to all
arguments.
○ Return Value: Functions return an exit status using return N (where N is 0-255).
Output can be returned via echo to stdout.
3. Narrate all built-in commands in korn shell With the help of shell prompt.
Built-in commands are commands that are integral parts of the shell itself, not separate
executable programs found in the file system (like /bin/ls). This makes them faster to execute
as the shell doesn't need to search for them.
Here's a narration of some common ksh built-in commands, often demonstrated with shell
prompts:
Code snippet
# ksh prompt, often ends with $ or #
# 1. cd (change directory)
$ pwd
/home/user
$ cd /tmp
$ pwd
/tmp
# 2. echo (display text)
$ echo "Hello, world!"
Hello, world!
$ name="Korn Shell"
$ echo "Using $name"
Using Korn Shell
# 3. read (read input from user)
$ echo "Enter your name:"
$ read user_name
Enter your name:
Alice
$ echo "Welcome, $user_name!"
Welcome, Alice!
# 4. exit (exit the shell or script)
$ echo "Exiting in 3 seconds..."
$ sleep 3
$ exit 0 # Exit with success status
# 5. export (make shell variable available to child processes)
$ my_local_var="local_value"
$ export my_env_var="environmental_value"
$ ksh -c 'echo "Local: $my_local_var"; echo "Env: $my_env_var"' # Child shell
Local:
Env: environmental_value
# 6. typeset (declare variable attributes, similar to 'declare' in Bash)
$ typeset -i count=10 # Declare an integer variable
$ (( count = count + 5 ))
$ echo $count
15
$ typeset -r MY_CONSTANT="permanent" # Declare a read-only variable
$ MY_CONSTANT="new"
ksh: MY_CONSTANT: is read only
# 7. alias (create command shortcuts)
$ alias ll='ls -lh'
$ ll
# (Outputs long listing in human-readable format)
# 8. unalias (remove an alias)
$ unalias ll
$ ll
ksh: ll: not found # (ll is no longer recognized as an alias)
# 9. pwd (print working directory)
$ pwd
/home/user/myproject
# 10. test or [ ] (evaluate conditional expressions)
$ var=10
$ if test $var -gt 5; then echo "Greater"; fi
Greater
$ if [ $var -eq 10 ]; then echo "Equal"; fi
Equal
$ if [[ $var == "10" ]]; then echo "String Equal"; fi
String Equal
# 11. source or . (read and execute commands from a file in the current shell)
# (Assuming my_vars.sh contains: MY_VAR="hello"; export MY_VAR)
$ source my_vars.sh
$ echo $MY_VAR
hello
# 12. break (exit from a loop) and continue (skip to next iteration)
$ for i in 1 2 3 4 5; do if (( i == 3 )); then break; fi; echo $i; done
1
2
$ for i in 1 2 3 4 5; do if (( i == 3 )); then continue; fi; echo $i; done
1
2
4
5
# 13. shift (shift positional parameters)
$ set -- one two three
$ echo "$1 $2 $3"
one two three
$ shift
$ echo "$1 $2 $3"
two three
# 14. unset (remove a variable or function)
$ my_temp_var="temporary"
$ echo $my_temp_var
temporary
$ unset my_temp_var
$ echo $my_temp_var # (empty output)
# 15. history (display command history)
$ history 10 # Displays last 10 commands
# 16. whence (show how a command would be interpreted)
$ whence ls
/bin/ls
$ whence cd
cd is a shell builtin
# 17. jobs (list active jobs)
$ sleep 100 &
[1] 12345
$ jobs
[1] + Running sleep 100
$ fg %1 # Bring job 1 to foreground
4. Write a shell script which simulate calculator.
Bash
#!/bin/ksh
# A simple calculator script
echo "Simple Shell Calculator"
echo "Enter first number:"
read num1
echo "Enter operator (+, -, *, /, %):"
read operator
echo "Enter second number:"
read num2
result=0
case "$operator" in
+)
(( result = num1 + num2 ))
;;
-)
(( result = num1 - num2 ))
;;
\*) # Escape '*' for case pattern matching
(( result = num1 * num2 ))
;;
/)
if (( num2 == 0 )); then
echo "Error: Division by zero!"
exit 1
fi
(( result = num1 / num2 ))
;;
%)
if (( num2 == 0 )); then
echo "Error: Modulo by zero!"
exit 1
fi
(( result = num1 % num2 ))
;;
*)
echo "Invalid operator: $operator"
exit 1
;;
esac
echo "Result: $result"
5. a) What are the system variables used by "awk".
awk uses several built-in system variables:
● NR: Number of Records (current line number).
● FNR: File Number of Records (current line number in the current file). Useful when
processing multiple files.
● NF: Number of Fields (number of fields in the current record/line).
● $0: The entire current record (line).
● $1, $2, ...: The first, second, and subsequent fields of the current record.
● FS: Field Separator (default is whitespace). Can be changed (e.g., awk -F':').
● OFS: Output Field Separator (default is space).
● RS: Record Separator (default is newline).
● ORS: Output Record Separator (default is newline).
● FILENAME: The name of the current input file.
● ARGC: Number of command-line arguments.
● ARGV: Array of command-line arguments.
● ENVIRON: An associative array of environment variables.
5. b) Describe 3 data processing concepts used in "awk".
awk excels at text processing based on a "pattern-action" paradigm. Key data processing
concepts are:
1. Record and Field Processing: awk processes input data one record (line) at a time.
Each record is automatically split into fields based on a defined field separator (FS). This
allows for easy manipulation of individual data elements within each line. For example, $1
refers to the first field, $2 to the second, and so on. This is fundamental to awk's ability to
extract, reorder, or perform calculations on columnar data.
2. Pattern-Action Pairs: awk programs consist of a series of pattern { action } pairs.
○ pattern: A regular expression or a conditional expression that awk tries to match
against each input record. If the pattern matches (or if no pattern is specified), the
associated action is executed.
○ action: A block of awk commands enclosed in curly braces {} that are performed on
the records that match the pattern. Actions can include printing fields, performing
calculations, assigning variables, or controlling flow.
○ This allows awk to perform selective processing on data.
3. Associative Arrays: awk supports associative arrays, which are powerful data structures
where elements are indexed by strings (or numbers), not just consecutive integers. This
allows awk to easily store and retrieve data based on meaningful keys, making it suitable
for tasks like counting occurrences, grouping data, or building lookup tables.
○ Example: awk '{ count[$1]++ } END { for (word in count) print word, count[word] }'
file.txt (counts word frequencies).
6. a) Illustrate "awk" process flow.
The awk process flow generally follows these steps:
1. BEGIN Block Execution (Optional): If a BEGIN { actions } block is present, awk executes
the commands within this block once, before reading any input data. This is typically
used for initializing variables, setting field separators (FS), or printing headers.
2. Record Processing (Main Loop):
○ awk reads the input file(s) line by line (record by record).
○ For each record:
■ The record is parsed and divided into fields based on the FS (Field Separator).
■ Each pattern { action } pair in the awk script is evaluated against the current
record.
■ If a pattern matches the record (or if there is no pattern, which implies a match
for every record), the corresponding action block is executed.
3. END Block Execution (Optional): If an END { actions } block is present, awk executes
the commands within this block once, after all input records have been processed. This is
typically used for printing summaries, totals, or footers.
+-----------+
| BEGIN | (Optional) Executed once at the start.
+-----------+
|
V
+-----------------------------------+
| Read first line (record) from input |
+-----------------------------------+
|
V
+------------------------------+
| Parse line into fields ($1, $2, etc.) |
+------------------------------+
|
V
+--------------------------+
| For each pattern { action } pair: |
| Is pattern true for current line? |
| Yes /|\ |
| | | No |
| V | |
| Execute action block |
| |
+--------------------------+
|
V
+-----------------------------------+
| Are there more lines in input? |
| Yes /|\ |
| | | No |
| V | |
+-----------------------------------+
|
V
+-----------+
| END | (Optional) Executed once at the end.
+-----------+
|
V
+-----------+
| Exit |
+-----------+
6. b) calculate student average using "awk" script.
Assume students.txt contains data like:
John Doe 85 90 78
Jane Smith 92 88 95
Peter Jones 70 65 80 75
Here, $1 is first name, $2 is last name, and subsequent fields are scores.
Awk
#!/usr/bin/awk -f
# Script to calculate student average from a file
BEGIN {
print "Student Name\tAverage Score"
print "-----------------------------"
}
{
# Calculate sum of scores for current student
sum = 0
# Loop from the 3rd field to the last field (NF)
for (i = 3; i <= NF; i++) {
sum += $i
}
# Calculate average
# (NF - 2) because the first two fields are name, not scores
num_scores = NF - 2
if (num_scores > 0) {
average = sum / num_scores
printf "%s %s\t%.2f\n", $1, $2, average
} else {
printf "%s %s\tNo scores available\n", $1, $2
}
}
END {
print "-----------------------------"
print "Average calculation complete."
}
To run this: awk -f script_name.awk students.txt or awk '{ ... }' students.txt
7. Define Associative array? Explain it with example "awk" scripts.
● Associative Array: An associative array (also known as a hash map or dictionary) is a
data structure that stores data as key-value pairs. Unlike traditional indexed arrays
(where elements are accessed by integer indices like 0, 1, 2), elements in an associative
array are accessed using unique keys, which can be strings or numbers. This allows for
more intuitive data organization and retrieval based on meaningful labels.
● Example "awk" script (Counting word occurrences):
This script counts how many times each unique word appears in an input file.
Awk
#!/usr/bin/awk -f
# This script counts the frequency of each word in a file.
BEGIN {
# Set field separator to any non-alphanumeric character to easily get words
FS = "[^a-zA-Z]+"
}
{
# Loop through each field (word) in the current line
for (i = 1; i <= NF; i++) {
# Convert word to lowercase for case-insensitive counting
word = tolower($i)
# Increment the count for this word in the 'counts' associative array
# If 'word' is not yet a key, it's initialized to 0 then incremented
counts[word]++
}
}
END {
# After processing all lines, iterate through the 'counts' array
# Print each word and its corresponding count
print "Word Frequencies:"
for (word_key in counts) {
if (word_key != "") { # Avoid counting empty strings
print word_key ": " counts[word_key]
}
}
}
To run this: echo "Apple, orange. Apple and banana." | awk -f word_count.awk
Output would be similar to:
Word Frequencies:
apple: 2
orange: 1
and: 1
banana: 1
Here, counts is an associative array, and word (e.g., "apple", "orange") serves as the key.
8. Write about following string handling functions: Length, index, substring, split, substitution,
match and toupper, tolower.
awk provides rich string manipulation functions:
● length(string): Returns the length of the string (number of characters).
○ Example: print length("hello") outputs 5.
● index(string, substring): Returns the starting position (1-based index) of the first
occurrence of substring within string. Returns 0 if substring is not found.
○ Example: print index("hello world", "world") outputs 7.
● substr(string, start_pos, length): Returns a substring of string starting at start_pos
(1-based) with the specified length. If length is omitted, it extracts to the end of the
string.
○ Example: print substr("programming", 3, 4) outputs ogra.
● split(string, array, separator): Divides string into fields based on the separator (regex)
and stores them in the array. Returns the number of fields created.
○ Example: split("one,two,three", my_array, ",") would create my_array[1]="one",
my_array[2]="two", my_array[3]="three".
● Substitution (sub, gsub):
○ sub(regex, replacement, string): Replaces the first occurrence of regex in string
with replacement. If string is omitted, $0 (current record) is used. Returns 1 if a
substitution is made, 0 otherwise.
■ Example: line="apple apple orange"; sub(/apple/, "banana", line); print line
outputs banana apple orange.
○ gsub(regex, replacement, string): Replaces all occurrences of regex in string with
replacement. If string is omitted, $0 is used. Returns the number of substitutions
made.
■ Example: line="apple apple orange"; gsub(/apple/, "banana", line); print line
outputs banana banana orange.
● match(string, regex): Tests if regex matches string. Sets RSTART (start position of
match, 1-based) and RLENGTH (length of match). Returns RSTART if a match is found, 0
otherwise.
○ Example: if (match("hello", /ello/)) print RSTART, RLENGTH outputs 2 4.
● toupper(string): Returns string with all lowercase letters converted to uppercase.
○ Example: print toupper("Hello World") outputs HELLO WORLD.
● tolower(string): Returns string with all uppercase letters converted to lowercase.
○ Example: print tolower("Hello World") outputs hello world.
9. Write an awk script to merge contents in two files.
To merge contents from two files line by line (like paste command):
Assuming file1.txt and file2.txt are the input files.
Awk
#!/usr/bin/awk -f
# This script merges contents of two files line by line.
BEGIN {
# Set the input record separator to empty string to read entire files into single records (not typical for
line-by-line merge)
# A more common approach for 'paste'-like behavior is to use NR and FNR
}
# Process file1
FNR == NR {
line1[FNR] = $0 # Store lines of the first file in an array
next # Move to the next line in the current file
}
# Process file2 after file1 has been read
{
# Print the line from file1 and then the current line from file2
# Ensure line1[FNR] exists, otherwise print empty string
printf "%s\t%s\n", line1[FNR], $0
}
# After all files are processed, print any remaining lines from the first file
# if file1 was longer than file2
END {
# This part handles if file1 has more lines than file2
# This logic is simpler for a generic 'paste' equivalent.
# For a perfect merge, we typically pass multiple files and track them.
# Simpler conceptual merge:
# If using 'awk file1.txt file2.txt', then FNR==NR works for first file
# Then FNR resets for second file, but NR continues.
# A more robust way to emulate 'paste' in awk for two files:
# Use getline in the BEGIN block for first file and then process second.
# Or, rely on external piping of 'paste' if simple merge is desired.
# For a simple 'paste' functionality, you'd typically run:
# paste file1.txt file2.txt
# If the request specifically implies an AWK script to *emulate* paste,
# the solution involves tracking line numbers and reading from both files.
# The previous code block (using FNR == NR) is a common pattern for processing multiple files.
# A better emulation of `paste` with `awk`:
# awk '{
# if (FILENAME == ARGV[1]) { # If processing the first file
# lines1[NR] = $0
# } else { # If processing the second file
# # Print the line from the first file (if it exists) and current line from second
# printf "%s\t%s\n", lines1[FNR], $0
# }
# }
# END {
# # Print any remaining lines from the first file if it was longer
# for (i = FNR + 1; i <= length(lines1); i++) {
# print lines1[i]
# }
# }' file1.txt file2.txt
# This is a more complete emulation of `paste`.
}
To run the awk script that emulates paste:
awk '{ if (FILENAME == ARGV[1]) { lines1[NR] = $0 } else { printf "%s\t%s\n", lines1[FNR], $0 } }
END { for (i = FNR + 1; i <= length(lines1); i++) { print lines1[i] } }' file1.txt file2.txt
10. Narrate all "awk" statements in detail With example scripts.
"awk" statements are commands or constructs within the action block or BEGIN/END blocks
that perform operations.
● Print Statement (print): Displays text or field values to standard output.
○ print $0: Prints the entire current record.
○ print $1, $3: Prints the first and third fields, separated by OFS.
○ print "Hello", name: Prints literal strings and variable values.
○ Example: awk '{ print "Line " NR ": " $0 }' file.txt (prints line number and content).
● Printf Statement (printf): Provides formatted output, similar to C's printf.
○ printf "Name: %s, Age: %d\n", $1, $2
○ Example: awk '{ sum+=$3 } END { printf "Total sum: %.2f\n", sum }' data.txt (prints
formatted sum).
● Variable Assignment: Assigns a value to a variable.
○ count = count + 1
○ name = $1
○ Example: awk '{ total_sales += $4 } END { print "Overall Sales:", total_sales }' sales.csv
● Conditional Statements (if-else): Executes code blocks based on conditions.
○ if (condition) { action } [else { action }]
○ Example: awk '{ if ($3 > 100) print $1, "is high" else print $1, "is low" }' data.txt
● Looping Statements (for, while, do-while): Iterate over code blocks.
○ for loop (C-style): for (i=start; i<=end; i++) { action }
■ Example: awk '{ for (i=1; i<=NF; i++) print "Field " i ": " $i }' data.txt
○ for-in loop (for associative arrays): for (key in array) { action }
■ Example: awk '{ count[$1]++ } END { for (word in count) print word, count[word] }'
words.txt
○ while loop: while (condition) { action }
○ do-while loop: do { action } while (condition)
● Control Flow Statements (next, exit, break, continue):
○ next: Skips the rest of the current record's action block and proceeds to read the
next input record.
■ Example: awk '{ if ($1 == "skip") next; print $0 }' file.txt (skips lines starting with
"skip").
○ exit [status]: Exits the awk script immediately. If status is provided, it's the exit code.
The END block (if present) is executed before exiting.
■ Example: awk '{ if (NR > 100) exit; print $0 }' largefile.txt (prints only first 100
lines).
○ break: Exits the innermost for, while, or do-while loop.
○ continue: Skips the rest of the current iteration of the innermost loop and proceeds
to the next iteration.
● Array Manipulation:
○ delete array[index]: Deletes an element from an array.
■ Example: delete counts["the"]
○ delete array: Deletes all elements from an array.
UNIT-5
1. a) Write awk script to display only names and phone numbers from given file.
Assume a file contacts.txt with format:
John Doe [email protected] 123-456-7890
Jane Smith [email protected] 987-654-3210
Awk
#!/usr/bin/awk -f
# Script to display names and phone numbers
{
# Assuming name is $1 $2 and phone number is the last field
printf "%s %s\t%s\n", $1, $2, $NF
}
To run: awk -f script_name.awk contacts.txt or awk '{printf "%s %s\t%s\n", $1, $2, $NF}'
contacts.txt
1. b) Write awk script to count number of lines and words in a file.
Awk
#!/usr/bin/awk -f
# Script to count lines and words in a file
BEGIN {
line_count = 0
word_count = 0
}
{
line_count++ # Increment line count for each record
word_count += NF # Add the number of fields (words) in current record to total word count
}
END {
print "Total Lines:", line_count
print "Total Words:", word_count
}
To run: awk -f script_name.awk my_file.txt or awk '{line_count++; word_count+=NF} END{print
"Total Lines:", line_count; print "Total Words:", word_count}' my_file.txt
2. How do you find a line that matches a regular expression using awk.
In awk, you find a line that matches a regular expression by placing the regular expression
directly as the pattern part of a pattern { action } pair. If the regex matches the current record
($0), the associated action is executed.
Example: To find lines containing "error" in logfile.txt:
awk '/error/ { print $0 }' logfile.txt
or simply:
awk '/error/' logfile.txt (The default action when a pattern matches is print $0).
To find lines starting with "FAIL":
awk '/^FAIL/ { print }' results.txt
To find lines containing "warning" or "ERROR":
awk '/(warning|ERROR)/ { print }' logfile.txt
3. What is remote access? Describe about few communication utilities.
● Remote Access: Remote access refers to the ability to access and control a computer
system or network from a geographically distant location. It allows users to work on a
machine as if they were sitting directly in front of it, enabling administration, file transfer,
and running applications without physical presence.
● Few communication utilities:
○ SSH (Secure Shell): The most common and secure protocol for remote access to
Linux/Unix servers. It provides a secure, encrypted channel over an unsecured
network. It's used for remote command execution, file transfer (via SCP/SFTP), and
tunneling.
■ Example: ssh user@remote_host
○ Telnet (TErminal NETwork): An older, insecure protocol for remote access. It sends
data in plain text, making it vulnerable to eavesdropping. Largely superseded by SSH
for secure access.
■ Example: telnet remote_host
○ FTP (File Transfer Protocol): A standard network protocol used to transfer
computer files from a server to a client or vice-versa over a computer network. It is
unencrypted by default.
■ Example: ftp remote_host
○ SCP (Secure Copy Protocol): A secure file transfer utility based on SSH. It allows for
secure copying of files between local and remote hosts.
■ Example: scp local_file.txt user@remote_host:/path/to/destination
○ SFTP (SSH File Transfer Protocol): A secure file transfer protocol that runs over
SSH. It provides a more interactive and feature-rich file transfer experience than SCP,
similar to FTP but with SSH security.
■ Example: sftp user@remote_host
○ VNC (Virtual Network Computing): A graphical desktop sharing system that allows
you to remotely control another computer's graphical desktop. It sends screen
updates from the remote machine to the local machine and input (keyboard/mouse)
back.
■ Example: vncviewer remote_host
○ RDP (Remote Desktop Protocol): Primarily used for Windows remote desktop
connections, but clients exist for Linux to connect to Windows machines.
4. a) Explain how talk and write commands work?
● talk command: The talk command allows two users logged into the same or different
networked Unix-like systems to have a real-time, synchronous, character-by-character
chat. When one user initiates a talk request to another, the target user receives a
message on their terminal. If the target user accepts, their screen splits into two
windows: one for their input and one for the other user's input. Text appears as it's typed.
○ Example: User A on host1 wants to talk to User B on host2. userA@host1$ talk
userB@host2 User B on host2 sees: Message from talk_daemon@host1... talk:
connection requested by userA@host1. talk: respond with: talk userA@host1 User B
responds: userB@host2$ talk userA@host1 Then, both users enter a split-screen
chat.
● write command: The write command allows a user to send a message to another user
logged into the same system. The message is sent directly to the recipient's terminal and
appears immediately, potentially interrupting what they are doing. It's a simple, one-way
messaging tool, unlike talk which is interactive.
○ Example: userA@host$ write userB Hello, are you there? Ctrl+d (to end message)
User B's terminal will show: Message from userA@host on ttyS0 at 10:30 ... Hello, are
you there? EOF
4. b) How does "telnet" command work? explain it with example.
The telnet command is a client program used to open a connection to a remote machine,
typically using the Telnet protocol. The Telnet protocol provides a bidirectional interactive
text-oriented communication facility using a virtual terminal connection.1 It's a basic
client-server protocol.
● How it works:
1. The telnet client establishes a TCP connection to the specified remote host and port
(default port 23 for Telnet service).
2. Once connected, the client acts as a simple terminal, sending keystrokes to the
remote server and displaying output received from the server.
3. The server side runs a Telnet daemon (telnetd) that listens for incoming connections.
When a connection is established, it typically spawns a shell or a specific service
process for the client.
4. All data (including usernames, passwords, and commands) is transmitted in plain
text, which is why it is considered insecure and largely replaced by SSH for remote
login.
● Example (connecting to a public service, though very rare due to insecurity):
Suppose there was a simple echo server on example.com listening on port 8080.
Bash
$ telnet example.com 8080
Trying 192.0.2.1...
Connected to example.com.
Escape character is '^]'.
Hello server! # You type this
Hello server! # Server echoes this back
^] # Press Ctrl+] to enter telnet prompt
telnet> quit # Type 'quit' and press Enter
Connection closed by foreign host.
For remote login to a system with Telnet daemon:
Bash
$ telnet remote_host
Trying 192.168.1.100...
Connected to remote_host.
Escape character is '^]'.
Debian GNU/Linux 11
login: myuser # Type username
Password: # Type password (characters are not echoed)
Last login: Wed Jun 11 02:00:00 2025 from some.ip
$ ls # You are now on the remote system's shell
5. Describe in detail about domain levels used with "mail" utility.
The "mail" utility (often referring to mailx or mail) is a command-line program for sending and
receiving emails. Its understanding of "domain levels" relates to how email addresses are
structured and resolved in the Domain Name System (DNS).
An email address typically follows the format
[email protected], where:
● user: The local part, identifying the mailbox on the mail server.
● domain.tld: The domain name, which is a hierarchical structure used to locate the mail
server responsible for that domain.
Domain Levels (DNS Hierarchy):
Domain names are structured in a hierarchy of "levels," separated by dots, read from right to
left. Each segment is a "label."
● Top-Level Domain (TLD): The highest level in the DNS hierarchy (e.g., .com, .org, .net,
.gov, .edu, .info, .io, and country code TLDs like .uk, .de). These are managed by ICANN.
● Second-Level Domain (SLD): The domain directly to the left of the TLD (e.g.,
google.com, wikipedia.org, example.net). This is typically what an organization registers.
● Third-Level Domain (Subdomain): A domain name that is part of a larger domain. It is
to the left of the SLD (e.g., mail.google.com, en.wikipedia.org). Organizations often use
subdomains to organize services or content.
How "mail" utility uses domain levels:
When you send an email using the mail utility (e.g., mail
[email protected]), the utility
needs to determine which mail server handles mail for example.com.
1. DNS Lookup (MX Records): The mail utility (or the underlying Mail Transfer Agent -
MTA, like Sendmail or Postfix, that mail interfaces with) performs a DNS lookup for Mail
Exchanger (MX) records for the example.com domain.
2. MX Record Priority: MX records specify the mail servers responsible for accepting email
for a domain, along with a preference number (lower numbers indicate higher priority).
3. Delivery: The MTA attempts to connect to the highest-priority mail server listed in the
MX records. Once a connection is established, the email is delivered to that server, which
then handles delivery to the specific user mailbox.
The domain levels thus provide the structured addressing scheme that allows mail systems
across the internet to correctly route emails to the appropriate mail servers, regardless of
their physical location.
6. Narrate about commands used to send a mail in Linux environment.
In the Linux environment, several commands can be used to send email:
● mail or mailx: These are traditional command-line mail user agents (MUAs).
○ Interactive mode:
Bash
mail
[email protected]
Subject: My Test Email
Hello, this is the body of my email.
. # Type a single dot on a new line to end the message
Cc:
[email protected]
Bcc:
[email protected]
○ Non-interactive (piped) mode:
Bash
echo "This is the message body." | mail -s "Subject of the email"
[email protected]
○ From a file:
Bash
mail -s "Report from script" [email protected] < report.txt
● sendmail: This is often the underlying Mail Transfer Agent (MTA) itself. While it can be
used directly from the command line, it's more complex and primarily used by mail
servers and other programs to send mail.
Bash
sendmail [email protected] <<EOF
Subject: My Direct Sendmail Email
From: [email protected]
This email was sent directly via sendmail.
EOF
● mutt: A powerful, text-based email client that supports sending, receiving, and managing
email. It offers more features than mailx.
Bash
mutt -s "Subject from Mutt" -a attachment.txt [email protected] < body.txt
● ssmtp: A simple mail transfer agent that is often used as a sendmail replacement for
simpler setups, acting as a client to a remote SMTP server. Configured in
/etc/ssmtp/ssmtp.conf to point to an external SMTP server. Once configured, you can use
mail or mailx as usual, and ssmtp handles the actual sending.
7. Write in detail about telnet interface commands.
When you connect to a Telnet server using the telnet client, you are typically in "command
mode" before entering "input mode" for the remote host. The telnet prompt (usually telnet>)
allows you to issue local commands. The escape character (often Ctrl+] or Ctrl+^) allows you
to switch from the remote session back to the local telnet> prompt.
Common telnet interface commands:
● open [host [port]]: Connects to the specified host and optional port.
○ Example: telnet> open example.com 25 (connects to SMTP port).
● close or quit: Closes the current Telnet connection and exits the telnet client.
○ Example: telnet> close
● display [argument]: Displays all or specific telnet parameters.
○ Example: telnet> display
● mode type: Sets the Telnet mode.
○ mode character: Character-at-a-time mode (each character is sent as typed).
○ mode line: Line-by-line mode (input is sent after Enter is pressed).
○ mode localchars: Toggles local character echoing.
● set argument value: Sets a Telnet parameter to a value.
○ set escape character: Changes the escape character.
○ set echo: Toggles local echoing of typed characters.
● unset argument: Unsets a Telnet parameter.
● status: Shows the current status of the Telnet client.
● ? or help: Displays help information about telnet commands.
● send argument: Sends special Telnet protocol characters (e.g., ao for Abort Output, ayt
for Are You There).
○ Example: telnet> send ao
8. Narrate about commands used to read a mail in Linux environment.
To read email in the Linux command-line environment, common mail user agents (MUAs) are
used:
● mail or mailx: These are basic text-based mail clients.
○ To start reading mail: Simply type mail or mailx at the prompt.
○ This will open your mailbox (usually /var/mail/your_username or ~/Maildir).
○ It lists new and old messages with numbers.
○ To read a specific message: Type the message number and press Enter (e.g., 1 for
the first message).
○ Commands within mail (or mailx):
■ p or print: Print message.
■ d: Delete message.
■ s [filename]: Save message to a file.
■ r or reply: Reply to sender.
■ R: Reply to sender and all recipients.
■ q or quit: Quit, saving changes (e.g., deleting marked messages).
■ x: Exit without saving changes.
■ ?: Help.
● mutt: A more advanced, full-featured text-based email client offering robust email
management, search, and attachment handling.
○ To launch: mutt
○ It presents a structured interface to navigate folders, view messages, and compose
replies.
● pine / alpine: User-friendly, screen-oriented text-based email clients. alpine is the
successor to pine. They provide menu-driven interfaces, making them easier for
beginners.
○ To launch: alpine
These clients typically read mail from the user's local mail spool (e.g., in /var/mail) or from
Maildir directories in their home directory. They can also be configured to connect to remote
IMAP/POP3 servers.
9. What is ftp? Illustrate how files can be transferred between two systems with
Appropriate commands.
● FTP (File Transfer Protocol): FTP is a standard network protocol used for the transfer of
computer files between a client and server on a computer network.2 It is built on a
client-server model3 and uses separate control and data connections between the client
and the server. The client initiates the connection, and the server responds. FTP is
unencrypted by default, making it insecure for sensitive data.
● Illustrating file transfer with ftp commands:
Assuming you have ftp installed on your local Linux system and an FTP server running on
a remote system (remote_host with user myuser).
1. Connect to the FTP server:
Bash
$ ftp remote_host
Connected to remote_host.
220 (vsFTPd 3.0.3)
Name (remote_host:local_user): myuser # Enter your username
331 Please specify the password.
Password: # Enter your password (not echoed)
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>
2. Navigate directories on the remote server:
○ ls: Lists files and directories on the remote server.
○ cd remote_directory: Changes directory on the remote server.
○ pwd: Shows the current working directory on the remote server.
3. Navigate directories on your local system:
○ !ls: Executes ls command on your local system (prefixed with !).
○ !pwd: Shows current working directory on your local system.
○ lcd local_directory: Changes directory on your local system (local change directory).
4. Transfer files from local to remote (Upload):
○ put local_file.txt: Uploads local_file.txt from your local current directory to the remote
current directory.
○ put local_report.pdf remote_destination/: Uploads local_report.pdf and saves it to a
specific path on the remote server.
○ mput *.txt: Uploads multiple files matching the pattern.
5. Transfer files from remote to local (Download):
○ get remote_file.log: Downloads remote_file.log from the remote current directory to
your local current directory.
○ get /path/to/remote/data.zip local_copy.zip: Downloads data.zip from remote and
saves it as local_copy.zip on your local system.
○ mget *.jpg: Downloads multiple files matching the pattern.
6. Set transfer mode (important for binary vs. text):
○ binary: Sets transfer mode to binary (for images, executables, archives). This is
usually the default and recommended.
○ ascii: Sets transfer mode to ASCII (for plain text files, handles newline conversions).
7. Disconnect:
○ bye or quit: Closes the FTP connection and exits the ftp client.
Bash
ftp> put my_document.txt
200 PORT command successful.
150 Opening BINARY mode data connection for my_document.txt.
226 Transfer complete.
ftp> get remote_log.txt
200 PORT command successful.
150 Opening BINARY mode data connection for remote_log.txt.
226 Transfer complete.
ftp> bye
221 Goodbye.
$
10. Illustrate features of awk in detail with appropriate examples.
awk is a powerful text processing language well-suited for data extraction, manipulation, and
reporting. Key features include:
1. Pattern-Action Paradigm: awk operates on pairs of pattern { action }. If a pattern (a
regular expression or a condition) matches the current input record, the corresponding
action (a block of commands) is executed.
○ Example: Print lines containing "ERROR"
Awk
/ERROR/ { print $0 }
○ Example: Print lines where the third field is greater than 100
Awk
$3 > 100 { print $1, $2 }
2. Field Processing: Automatically parses each input record (line) into fields based on a
field separator (FS, default is whitespace). Fields are accessed by $1, $2, ..., and $0 is the
whole record. NF is the number of fields.
○ Example: Print the first and last field of each line.
Awk
{ print $1, $NF }
○ Example: Change input field separator to comma and print fields.
Awk
BEGIN { FS="," }
{ print $1, $2, $3 }
3. Built-in Variables: Provides numerous special variables that offer information about the
current processing state. (See Unit 4, Question 5.a for a list).
○ Example: Print line number and total lines.
Awk
{ print "Line " NR ": " $0 }
END { print "Total lines processed: " NR }
4. BEGIN and END Blocks: Special blocks that execute before any input is read (BEGIN)
and after all input has been processed (END). Useful for initialization and final summary
reporting.
○ Example: Calculate sum of a column.
Awk
BEGIN { sum = 0; print "Starting calculations..." }
{ sum += $3 } # Add value of third field to sum
END { print "Final Sum:", sum }
5. Associative Arrays: awk supports associative arrays, allowing data to be indexed by
strings (keys) instead of just numbers. This is extremely powerful for counting
occurrences, grouping data, and building lookup tables.
○ Example: Count unique visitors by IP address.
Awk
{ ip_counts[$1]++ } # Assuming IP address is the first field
END {
for (ip in ip_counts) {
print ip, ip_counts[ip]
}
}
6. String Manipulation Functions: Offers a rich set of functions for manipulating strings
(e.g., length, substr, index, sub, gsub, match, tolower, toupper). (See Unit 4, Question 8
for details).
○ Example: Convert first field to uppercase.
Awk
{ print toupper($1), $2 }
○ Example: Replace all "old" with "new".
Awk
{ gsub(/old/, "new"); print }
7. Arithmetic Operations: Supports standard arithmetic operators (+, -, *, /, %).
○ Example: Calculate average of two fields.
Awk
{ avg = ($3 + $4) / 2; print $1, avg }
8. Control Flow Statements: Includes if-else, for, while, do-while loops, and next, break,
continue, exit for program control.
○ Example: Process only lines with a specific condition.
Awk
{
if ($NF ~ /active/) {
print $0, "Status: Active"
} else {
print $0, "Status: Inactive"
}
}