Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
47 views

Introduction To Unix

The document discusses initialization files in Unix operating systems. There are two types of initialization files - system-wide initialization files stored in /etc/profile and /etc/.login that configure the overall environment, and user-specific initialization files stored in users' home directories that configure individual user environments. It also provides examples of common Unix commands like touch, cat, cp, mv, ls, and describes what each command does.

Uploaded by

ksenthil_kava
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Introduction To Unix

The document discusses initialization files in Unix operating systems. There are two types of initialization files - system-wide initialization files stored in /etc/profile and /etc/.login that configure the overall environment, and user-specific initialization files stored in users' home directories that configure individual user environments. It also provides examples of common Unix commands like touch, cat, cp, mv, ls, and describes what each command does.

Uploaded by

ksenthil_kava
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 45

The

Unix Operating System

1
Understanding Initialization Files
 When users log in to the system, their login shells look for and execute two different types of
initialization files. The first type controls the system-wide environment. The second type controls
the user’s environment.

 The two main system initialization files are called /etc/profile and /etc/.login.

 The Bourne and Korn login shells look for and execute the system initialization file /etc/profile
during login.

 The C login shell looks for and executes the system initialization file /etc/.login during the login
process.

 The default files /etc/profile and /etc/.login check disk usage quotas, print the message of the day
from the /etc/motd file,and check for mail.

 user’s initialization files are setup in the user’s home directory. They define the characteristics of a
user’s work environment, such as a user’s search path, environment variables, and windowing
environment.

2
Layered System

Multi-user, Multitasking system

USER Ot USER
ds he
el
l a n r Ap

Sh
m pl
Sh

e
o ica

ll
C Kernel tio
n ix ns
U
HARDWARE

Co
m S
pi
ler BM ll
Sh s RD e
ell Sh

USER USER

3
UNIX File System

ro o t (/)

b in dev e tc u n ix lib usr tm p lo s t+ fo u n d m nt

hom e

jo h n ra k e s h syed

4
bin system binary executables
dev file names for system devices, terminals, disks etc

etc system administration files and tools, system scripts


(also tcp/ip information files)

unix unix kernel

lib library routines

usr application and system specific information

tmp scratch pad directory for temporary files. Must have read/write
access

lost+found unreferenced files are placed here by fsck

mnt empty directory used for mounting temporary file systems

5
UNIX Commands
$ touch sample

 Creates a file called ‘sample’ with zero size if ‘sample’ is not present in the current directory
 Changes the last modified time and access time to current system date and time if ‘sample’ is
already an existing file
 -a Change the access time of file.
 -m Change the modification time of file.
 -t time Use the specified time instead of the current time. time will be a decimal number of
the form: [[CC]YY]MMDDhhmm [.SS]
MM The month of the year [01-12]
DD The day of the month [01-31]
hh The hour of the day [00-23]
mm The minute of the hour [00-59]
CC The first two digits of the year
YY The second two digits of the year
SS The second of the minute [00-61]

$ cat test

 Displays the contents of the file ‘test’ on to standard output if already exists

6
$ cat > test

 creates the file ‘test’ and writes the contents of standard output to ‘test’ till ctrl+d is pressed.

$ cat sample1 sample2 > sample3

 creates the file sample3 and writes the contents of sample1 & sample2 to sample3.
 If sample3 already exists, it overwrites its content with sample1 & sample2
 if >> operator is used,contents of sample1 & sample2 will be appended to content of sample3

$ cp temp.txt test.txt

 Copy the contents of temp.txt to test.txt.


 Creates test.txt if not exists,otherwise overwrites the contents of test.txt

$ cp temp1 temp2 mydir

 copy the files temp1 & temp2 to the directory ‘mydir’

$ cp /usr/home/mydir/temp1 /usr/home/testdir/temp2

 copy the file temp1 from one path to another path with filename as temp2

7
$ mv mydir tempdir

 Renames the directory ‘mydir’ to ‘tempdir’.

$ mv file1 file2 /export/home/mydir

 file1 & file2 are moved to the ‘mydir’ and are no longer present in the current directory.

$ mv test.txt sample.txt

 renames the file test.txt to sample.txt

$ ls -l

 Gives the directory listing. -l option gives the long list of the contents
 -a Lists all entries, including those that begin with a dot (.), which are normally not listed.
 -t Sorts by time stamp (latest first) instead of by name.
 -r Reverses the order of sort to get reverse alphabetic
 -i Displays the inode number of the file specified

8
$ rm file1

 deletes the file1 from current directory


 -I option removes files interactively
 -r option recursively removes files from the directory and the directory also.
 -f option removes files forcibly,irrespective of whether you have write permission or not.

$ chmod 700 myfile

 This command changes the file permissions for owner,group & others
 read permission wil have a value 4,write has 2, execute has 1 and 0 has no permission
 The ‘myfile’ will have permission for owner as -rwx, for group ---, for others ---
 +r,+w,+x will give read,write & execute permissions to all the three. -r,-w,-x is vise versa

$ umask
022

 Umask stands for user file creation mask.The value of this variable decides the masking of
file permissions when a file is created
 By default UNIX assumes that the permissions for file should be 666 and for directory 777. If
the umask value is 022 then the file actually created with (666 - 022 = 644).

9
$ mkdir mydir
 creates the directory ‘mydir’

$ mkdir -p work/temp/mydir
 - p option creates multiple generations of directories at one go.It first creats ‘work’ then within it
creates ‘temp’ and in temp creates ‘mydir’

$ rmdir work/temp/mydir
 rmdir removes only the empty directories.Removes the directory ‘mydir’.
 To remove the parent directories, use -p option.It removes only if the directories are empty.

$ cd work/temp/mydir
 changes the directory path to work/temp/mydir.
 If only ‘cd’ is is given without any arguments, the path changes to user’s home directory.

$ pwd
 Displays the present working directory.

$ who
Displays the login information login name, terminal number, date & time of login about all the users
who have logged into the system currently.

10
$ who am i
 Displays your login information login name, terminal number, date & time of login

$ ps -ef
 The ps command prints information about active processes. Without options, ps prints
information about processes that have the same effective user ID and the same controlling
terminal as the invoker. The output contains only the process ID, terminal identifier,
cumulative execution time, and the command name.
 -e option lists information about every process now running.
 -f option generates a full listing.

$ tty
 Displays the name of your terminal file.
 For each terminal on the n/w, Unix uses a file.The output displayed on its monitor is picked
from the file associated with the terminal.All these files are present in ‘/dev’ directory.
 Unix understands each terminal as a special file.

$ logname
 Prints the login name of the user

$ id
 Prints the userid and group id of the user

11
$ uname
 Displays the name of the unix system we are using.

$ df
 The df command displays the amount of disk space occupied by mounted or unmounted file
systems, the amount of used and available space, and how much of the file system's total
capacity has been used. The file system is specified by device, or by referring to a file or
directory on the specified file system.
 -b option print the total number of kilobytes free.
 -e option print only the number of files free.

$ du
Summarize the disk usage
 The du utility writes to standard output the size of the file space allocated to, and the size of
the file space allocated to each subdirectory of, the file hierarchy rooted in each of the specified
files.
 -k Write the files sizes in units of 1024 bytes, rather than the default 512-byte units.
 -s Instead of the default output, report only the total sum for each of the specified files.

$ file temp
 Determines the file type of the file ‘temp’ in the current directory.
 If ‘*’ is specified instead of a file name,it displays the type for all files in the current directory.

12
$ wc file1 file2
 prints the number if lines, number of words and number of characters in each file
 -l option displays only the number of lines in the file
 -w option displays only the number of words in the file
 -c option displays only the number of characters in the file

$ sort file1 file2


 The sort command sorts lines of all the named files together and writes the result on the
standard output.

$ sort -o test file1 file2


 The -o option sorts the files and saves the result in the file ‘test’
 -m option merges the files if already sorted.
 -u option eliminates the duplicate lines and prints only the unique lines in the files specified.
 -f option is to ignore case while sorting
 -r reverse the sort order
 -tc separates the fields with character (default is Tab)
 -b option ignores the leading spaces and tabs

13
$ cut -f 2,7 myfile
 Use the cut utility to cut out columns from a table or fields from each line of a file.
 -f option is for splitting based on fields (Tab delimited). If specified 2,7 it means cut only 2nd and
7th fields.If specified as 2-7 it means cut from 2nd to 7th field and display on to standard output or
redirect to a file
 -d option to recognize the field separator other than tab like ‘:’ or ‘,’

$ grep unix myfile


 searches for the pattern ‘unix’ in the file myfile and prints all the lines that contain that pattern.
 -c option returns only the number of matches,without quoting the text
 -i option ignores case while searching
 -l option returns only filenames containing a match,without quoting the text
 -n option returns line number of matched text, as well as the text itself
 -s option suppress the error messages if any.
 -v option returns lines that do not match the pattern.

$ head -15 myfile


 head command displays the first few lines of a file.Here it displays the first 15 lines of myfile
 unless specified,by default it displays the first 10 lines of the file

$ tail -15 myfile


 tail command displays the last few lines of a file.Here it displays the last 15 lines of myfile
 unless specified,by default it displays the last 10 lines of the file
 -f option displays last few lines continuously as it grows

14
$ cat file1 > file2
 The contents of file1 are copied to file2.It overwrites the content of file2 if file2 already exits
otherwise file2 is created and written with file1 content.
 The ‘>>’ operator appends the content of file1 to file2.

$ ls | wc -l
 Here the piping command ‘|’ is used to to join commands.In this case, wc command takes the
input from ‘ls’ command.Piping makes the output of first command as an input to the second
command.

$ uniq myfile
 report or filter out repeated lines in a file
 The uniq utility will read an input file comparing adjacent lines, and write one copy of each
input line on the output.The second and succeeding copies of repeated adjacent input lines
will not be written.Repeated lines in the input will not be detected if they are not adjacent.
 -d option suppress the writing of lines that are not repeated in the input.
 -u option suppress the writing of lines that are repeated in the input

$ cmp file1 file2


 The cmp utility compares two files. cmp will write no output if the files are the same. Under
default options, if they differ, it writes to standard output the byte and line numbers at
which the first difference occurred.

15
$ diff file1 file2
 The diff utility will compare the contents of file1 and file2 and write to standard output a
list of changes necessary to convert file1 into file2. This list should be minimal. No output
will be produced if the files are identical.
 -b option Ignores trailing blanks (spaces and tabs) and treats other strings of blanks as
equivalent.
 -i option Ignores the case of letters; for example, `A' will compare equal to `a'.

$ find $HOME -name ‘myfile’ -print


 The find utility recursively descends the directory hierarchy for each path seeking files that
match an expression
 ‘-atime n’ option evaluates to true if the file was accessed n days ago. The access time of
directories in path is changed by find itself.
 ‘ -ctime n’ option evaluates to true if the file's status was changed n days ago.
 ‘-mtime n’ option evaluates to true if the file's data was modified n days ago.
 ‘-exec cmd’ execute cmd if file meets conditions
 ‘-links n’ number of links to file
 ‘-name file’ specify filename
 ‘ -newer file’ file modified more recently than file

$ find $HOME \( -name a.out -o -name a.txt \) \ -atime +7 -exec rm {} \;


 Remove all files in your home directory named a.out or a.txt that have not been accessed
for a week

16
$ tee -a myfile
 The tee utility will copy standard input to standard output,making a copy in zero or more files.
tee will not buffer its output.The options determine if the specified files are overwritten or
appended to.
 -a option append the output to the files rather than overwriting them.

$ finger hpsid
 Displays information about local and remote users.

$ history
 Process command history list .The command history list references commands by number. The
first number in the list is selected arbitrarily. The relationship of a number to its command will
not change except when the user logs in and no other process is accessing the list, at which
time the system may reset the numbering to start the oldest retained command at another
number (usually1).
 Use ‘set history = n’ to set the number of history commands to be displayed to ‘n’.

$ kill 21496
 kill will terminate a process by its ID

$ nohup test.sh &


 ‘&’ runs the job ‘test.sh’ in the background.
 The ‘nohup’ utility can be used when it is known that command will take a long time to run and
the user wants to logout of the terminal.This ensures the ‘test.sh’ to continue run in the
background even when the user logs out of the terminal.

17
$ nice [ -increment or +increment ] [ command ]
 Changes the priority of a process .The nice utility invokes command, requesting that it be run
with a different system scheduling priority.
 increment must be in the range 1-19; if not specified, an increment of 10 is assumed. An
increment greater than 19 is equivalent to 19. The super-user may run commands with
priority higher than normal by using a negative increment such as -10.
 A negative increment assigned by an unprivileged user is ignored.

$ compress myfile
 The compress utility will attempt to reduce the size of the named files
 Each file will be replaced by one with the extension .Z, while keeping the same
ownership modes, change times and modification times.
 The amount of compression obtained depends on the size of the input, the number of bits
per code, and the distribution of common substrings. Typically, text such as source code or
English is reduced by 50-60%. Compression is generally much

$ uncompress myfile.z
 The uncompress utility will restore files to their original state after they have been
compressed using the compress utility. If no files are specified, the standard input will be
uncompressed to the standard output.

$ zcat myfile.z
 The utility will write to standard output the uncompressed form of files that have been
compressed using compress. It is the equivalent of uncompress -c. Input files are not affected.

18
$ pack myfile
 The pack command attempts to store the specified files in a compressed form. Wherever
possible (and useful), each input file is replaced by a packed file file.z with the same access
modes, access and modified dates, and owner as those of file. If pack is successful, file will
be removed.
 The amount of compression obtained depends on the size of the input file and the
character frequency distribution. Typically, text files are reduced to 60-75% of their original
size.
 -f option forces packing of file. This is useful for causing an entire directory to be packed
even if some of the files will not benefit. Packed files can be restored to their original form
using ‘unpack’ or ‘pcat’.

$ at 17:00
at> echo “Its 5 PM now”
ctrl+d
job 1056140340.a at Fri Jun 20 14:49:00 2003
 The at utility reads commands from standard input and groups them together as an at-job, to
be executed at a later time. The above example shows after the echo statement given,when
ctrl+d is pressed,it displays the job id and time of execution

$ batch
 The batch utility reads commands to be executed at a later time decided by the system
Instead of we specifying that our commands be executed at a precise moment in time

19
$ crontab -e
30 17 * * 5 /usr/bin/banner "Time to go!" > /dev/console

 The crontab utility manages a user's access with cron by copying, creating, listing, and
removing crontab files. If invoked without options, crontab copies the specified file, or the
standard input if no file is specified, into a directory that holds all users' crontabs.

 A crontab file consists of lines of six fields each. The fields are separated by spaces or
tabs. The first five are integer patterns that specify the following: minute (0-59), hour (0-23),
day of the month (1-31), month of the year (1-12), day of the week (06 with 0=Sunday).

 -e option edits a copy of the current user's crontab file, or creates an empty file to edit
if crontab does not exist. When editing is complete, the file is installed as the user's
crontab file. Each entry in the crontab file looks as shown in the example above.

 -l option lists the crontab file for the invoking user.

20
vi Editor Commands
vi filename to enter editor with existing or new file named filename

To exit from the editor:

:w writes the buffer into the current file


:3,10w corn writes lines 3-10 to the file called ‘corn’
:w writes the entire buffer to the current file
:q quits buffer after using the :w command
:wq writes and quits
:q! quits editor without writing buffer contents to the file
ZZ write and quit. Same as :wq

Cursor control:

kG go to line k
/text, ? text forward or backward search for the given text
Arrow keys to move cursor
^F, ^B to scroll the screen forward and backward, respectively

21
To insert text: (ESC to end insert)

i to insert text at the current cursor position


o, or O to open up a blank line below or above, respectively, the current line for the insertion
I, A to insert text at the beginning or the end, respectively, of the current line

To delete text:

nx, nX delete next or previous n characters, respectively; delete 1 character if n is omitted


D delete to end of line
J join next line
ndw, ndb delete next or previous n words, respectively
ndd delete n lines, starting from the current line; delete 1 line if n is omitted
dG delete the current line through end of file
:i,jd delete lines i to j; j must be  i. Use "$" to indicate the line at the end of the file,
"." for the current line, and "-k" for current line minus k.
d$ delete the current character through end of line
d) delete the current character through end of sentence
dtchar delete the current character up to char in current line
ckw change away k words forward

22
Cut and paste:

:i,jmk move lines i through j to after line k


kdd cut k lines from the current line, then move cursor to a new position, then
type
p or P paste the k lines cut above to after or before the line of the new cursor
position

Text Mode Input - End with ESC:

A append text after the cursor


i insert text before the cursor
o open a new line below cursor
O open a new line above cursor
R replace characters on screen, starting at the cursor

23
Command Mode - after execution, returns to command mode

r replace the character under the cursor


/happy search sequences, look for next occurrence of 'happy'
?lark search sequence, look for previous occurrence of 'lark'
n use after / or ? to advance to next occurrence of the search pattern
u undo the last command
U undo all the changes on the current line
x delete character under the cursor
[del] delete character to left of cursor
#
[ctrl-h]
[ctrl-d] scrolls screen down one half page at a time
[ctrl-u] scrolls screen up one half page at a time
[ctrl-f] scrolls screen down one page at a time
[ctrl-s] scrolls screen up one page at a time
nG position the cursor at line n in the file
[ctrl-g] identify the line number where the cursor is located

24
Operators used in the Command Mode:

d deletes indicated text starting at the cursor.


 dw deletes a word

 dd deletes a line

 3dd deletes three lines

deleted text is stored in a temporary buffer whose contents can be pasted using the p
command.

c deletes indicated text starting at the cursor, then enters the text input mode.
ce deletes from the cursor to the end of the word
y copies the indicated text, staring at the cursor, and stores it in a buffer. There are
nine unnamed buffers that store the last nine delete or yank operations and 26 named
buffers (a-z) that can also be used for storage. A double quote is used to
specify the name of the buffer.
"cy$ stores the text from the cursor to the end of the line in buffer c
p the put command, places the delete or yank buffer contents after the cursor or on the
next line.

p puts the last item yanked or deleted back into the file just after the cursor "cp puts
the contents of buffer c after the cursor

P identical to the p command, but places the text before the cursor

25
Scopes for use with operators:

e the end of the current word


w from the cursor to the beginning of the next word, including the space
b from the character before the cursor, backwards, to the beginning of the word
$ from the cursor to the end of the line
0 from just before the cursor to the beginning of the line
) from the cursor to the beginning of the next sentence (. ! ? return or two spaces)
( from just before the cursor back to the beginning of the sentence which contains the
cursor
} from the cursor to the end of the paragraph. A paragraph begins after a blank line
{ from just before the cursor back to the beginning of a paragraph

26
Shell Programming

 simple statements which when combined, create powerful scripts


 support for conditional execution and conditional repetition
 command substitution (the output of a command can be fed back into the script for execution)
 support for shell variables

# for files in the root directory


for files in /*
do
echo $files
done

 In the above example, a shell variable called files is created and made equal to each file (the
wild card *) in the root directory (/) in turn. For each occurrence of a directory entry, the shell
variable files is assigned the name of the file, then its contents (denoted by $files) is echoed to
the console screen.

27
Shell Arguments and quotes:

 the shell supports pattern matching and recognition


 the asterisk (*) symbol matches all strings
 the question mark (?) symbol matches a single character
 the special symbols < > * ? [ ] have special meaning
 to use special symbols, they are enclosed in single or double quotes, else the shell will try to execute
them as commands or use them as parameters to commands
 within double quotes, the symbols $ \ ` " retain their special meaning. To use these characters inside a
double quoted string, precede these symbols with a backslash (\) symbol

Shell command substitution:

 use backquote (`) symbols to enclose the command


 when executed, the output of the command substitutes for the command itself
 often used to assign shell variables

Positional Parameters:

 Shell creates positional parameters which match each argument supplied to the shell script
 The name of shell program is stored in $0
 The first argument is stored in $1
 The second argument is stored in $2 and so on till $9.Use ‘shift’ keyword to store the subsequent
parameters. when it is used, each parameter moves one to the left

28
 Total number of arguments supplied is stored in $#
 $* List all the shell arguments.It cannot yield each argument separately.
 $@ yield each argument separately when enclosed in double quotes.

Example:
# echoall
while test $# != 0
do
echo $1 $2 $3 $4 $5 $6 $7 $8 $9
shift
done

Special Parameters:

 $? returns the exit or return status of the recent command executed


 $$ returns the PID of the current process
 $! reruns the PID of the last background process
 $- returns the current shell settings
 $0 returns the name of the command being executed

29
Shell Conditional Tests:

 If test evaluates as false, it returns a 'non-zero' status


 The test command evaluates the expression
 If test evaluates as true, it returns a 'zero' status

30
Example:

if test $# -lt 1
echo must supply a single argument
else
echo the argument is $1
fi

Using eval to rescan the command line:

 Command lines are often built within the shell script


 The eval command rescans the line to form the command

Example:

command = who
output = ‘| wc -l’
eval $command $output

The above eval condition evaluates to: who | wc -l

31
Taking decisions:

The If - then -fi statement:


 Conditional execution of a command
 The if command is terminated by fi

Example:

if cp $source $target
then
echo File copied successfully
else
echo Failed to copy the file
fi

32
The case control structure:
 multiple test conditions
 shell attempts to match each pattern in turn
 a double semi-colon terminates the command
 for each pattern list only one pattern is matched by the shell
 the exit status is that of the last command executed
 if no command was executed, a zero status is returned the pattern *) is a default used if none of the
other patterns match

case string in
pattern) command-list ;;
pattern) command-list ;;
*) command-list;;
esac

Example:
echo “Enter a number from 1to 3: \c”
read num
case $num in
1) echo You have entered 1 ;;
2) echo You have entered 2 ;;
3) echo You have entered 3 ;;
*) echo Enter digits only from 1 to 3 ;;
esac

33
The While loop:
 Repeats a group of commands a number of times, depending on a condition
 The commands are repeated whilst the exit status of that for the condition is zero

while control command


do
command1
command2
done

example:

count=1
while [$count -le 10]
do
echo $count
count=`expr $count + 1`
done

34
The until loop:
 Repeats a group of commands a number of times, depending on a condition
 The commands are repeated whilst the exit status of that for the condition is non zero

until control command doesn’t return true


do
command1
command2
done

example:
# print numbers from 1to 10
i=1
until [$i -gt 10]
do
echo $i
i=`expr $i + 1`
done

35
The for loop:
 A shell variable is used to match a series of patterns
 The commands are executed once for each match

for control-variable in value1 value2 value3...


do
command1
command2
command3
done

example:
# print all the command line arguments provided to the program

for word in $*
do
echo $word
done

36
Shell functions:
 A sequence of shell commands within a script file
 Similar to procedures in PASCAL or functions in C

myfunc() {
command list;
}

 \C do not print a newline (use within double quotes)


 -n do not print a newline
 >&2 associate output of command with stderr

37
The SED utility
 SED stands for Stream line Editor
 SED reads lines one at a time from the input files,it applies the commands from the list,in order,
to each line and writes its edited form on the standard output.
 SED does not alter the contents of its input files. It writes on standard output.It outputs each line
automatically.

sed 'list of commands' file file2 file3 ...

Example:
#search & replace a string from every line of input files and write to a file

sed 's/HP-UNIX/SUNOS/g' sedtest1.txt sedtest2.txt > sedtest3.txt

Example:
# The substitution deletes all characters upto (.*) and including the rightmost tab (->).

du -a *.sh | sed 's/.* ->//'

Example:
# To print first few lines from an input file.

sed 3q sedtest1.txt

38
 To print its input up to and including the first line matching pattern.

sed '/pattern/q' filename

 To delete every line that contains pattern.Does not actually delete from input file.

sed '/pattern/d' filename

 Automatic printing can be turned off by -n option.In this case,only lines explicitly printed with
a 'p' command appear in the out put.

sed -n '/pattern/p'

 To replace each string of blanks or tabs with a newline and split its input into one word per
line.

sed 's/[ ->][ ->]*/\


> /g'

 To select a range of lines,we can use line numbers or pair of regular expressions.

sed -n '20,30p' filename #Print only lines 20 through 30


sed '1,10d' filename #Delete lines 1 through 10
sed '1,/^$/d' filename #Delete up to and including first blank line

39
sed -n '/^$/,/^end/p' filename # print each group of lines from an empty line to line starting with
end.

sed '$d' filename filename #Deletes last line

 SED has ability to write to multiple files.

sed -n '/HPS/w file1 # Writes lines matching HPS on file1 & not matching to file2.
> /HPS/!w file2' filenames # ‘>‘ indicates the sed command should be continued by
pressing the return key to write to multiple files.

Example:
# To write the entire output to a file and also writes just the changed lines to another file

sed 's/HPS/HCL/gw u.out' filenames >sedresult.out

 The above command write the entire output to sedresult.out and also writes just the
changed lines to u.out.

40
The AWK utility
 AWK is a pattern scanning & processing language
 The idea in AWK is much the same as in SED,but the details are based on the C language.

awk 'program' filenames..

 program has: pattern {action}

 AWk reads the input from the filenames one at a time.Each line is compared with each pattern
in order.
 For each pattern that matches the line,corresponding action is performed.

awk 'regular expression/ {print}' filenames..

 Here either pattern or action is optional.If action is omitted,the default action is to print matched
lines.

 If the pattern is omitted,then action part is done for every input line.

 As with SED,it is possible to present the program to AWK from a file.

awk -f cmdfile filenames...

41
 AWK splits each input line automatically into fields.i.e;strings of non-blank characters seperated
by blanks or tabs.

 AWk calls the fields $1,$2,.....$NF, where NF is a variable whose value is set to no.of fields.
 $NF is the last field on the line.
 In AWK unlike in shell,fields begin with $.They are not variables here.

Examples:

#Print the 2nd field from the output of ‘du -a’ command
du -a | awk '{print $2}’

#Print the 1st and 5th fields from the output of ‘du -a’ command
du -a |awk '{print $1,$5}'

#To print the name & time of login sorted by time


who|awk '{print $5,$1}'|sort

 Although AWk is easy to use for operations like above,it is usually slower compared to SED.

 AWK normally assumes white space (any no.of blanks & tabs) separates fields.Here the
separator can be changed to a single character.

42
Examples:
# To print the user names which come from the first field

sed 3q /etc/passwd |awk -F: '{print $1}' # -F option changes the field seperator to ':'

Patterns in AWK:

#To look in etc/passwd file for people who have no passwords.

awk -F: '$2 =="" {print}' /etc/passwd

 The above pattern can also be represented as:

$2 ~ /^$/ #2nd field matches empty string

$2 != /./ #2nd field doesn't match any character

length($2) == 0 #Length of 2nd field is zero

43
Control Flow in AWK:

#To identify the double adjacent words in any file.

awk '
FILENAME != prevfile { # new file
NR =1 # reset line number
prevfile = FILENAME
}
NF > 0 {
if ($1 ==lastrecord)
printf "double %s,file %s,line %d\n",$1,FILENAME,NR
for (i=2;i<=NF;i++)
if ($i == $(i-1))
printf "double %s,file %s,line %d\n",$i,FILENAME,NR
if (NF > 0)
lastword = $NF
}' $*

44
Built-in variables used by awk:

Variables Description

ARGC Command line argument count


ARGV Command line argument array
FILENAME Name of the current input file
FNR Ordinal number of the current record in the current file
FS Input field separator (Default blank)
OFS Output field separator (Default blank)
NR Ordinal number of the current record
NF Number of field in the current record
RS Record separator (Default new-line)
ORS Output record separator (Default new line)
OFMT Output format for numbers

45

You might also like