Download as pdf or txt
Download as pdf or txt
You are on page 1of 61

label label

Mirunalini.P (SSNCE) Unix October 18, 2023 1 / 60


Filter Commands

Mirunalini.P

SSNCE

October 18, 2023

Mirunalini.P (SSNCE) Unix October 18, 2023 1 / 60


Table of Contents

1 Basic Concepts - Filters

2 Display Beginning and End of Files

3 cut and Paste

4 Sorting

5 References

Mirunalini.P (SSNCE) Unix October 18, 2023 2 / 60


Session Objective

Introduction to Filters
Head and Tail commands
Cut and Paste Commands

Mirunalini.P (SSNCE) Unix October 18, 2023 3 / 60


Session Outcome

At the end of this session, participants will be able to


Understand the Filter commands system
Understand the head and tail commands
Work with cut and paste commands

Mirunalini.P (SSNCE) Unix October 18, 2023 4 / 60


Filters

Filter is any command that gets its input from the standard input
stream, manipulates the input, and then sends the result to the
standard output stream.
Some filters can receive data directly from a file
More - passes all data from input to output with pauses at the
end of the each screen of data.
12 more simple filters are there.

Mirunalini.P (SSNCE) Unix October 18, 2023 5 / 60


Common Filters

Filter Action
cat Passes all the data from input to output
cmp Compares two files
comm Identifies common lines in two files
cut Passes only specified columns
diff Identifies differences between two
files or between common files in two directories
head Passes the number of specified lines
at the beginning of the data.
paste Combines columns
sort Arranges data in sequence
tail Passes the number of specified lines at the end of the data
Table 1: Common filters

Mirunalini.P (SSNCE) Unix October 18, 2023 6 / 60


Common Filters

Filter Action
tr Translates one or characters as specified
uniq Deletes duplicate lines
wc Counts characters,words or lines
grep Passes only specified lines
sed Passes edited lines
awk Passes edited lines - parses lines
Table 2: Common filters

Mirunalini.P (SSNCE) Unix October 18, 2023 7 / 60


Filters and Pipes

Two commands can be combined together so that the


output of one command becomes the input of the next
command.
Two or more commands are connected in this way by means of
pipe |.
Pipe is a vertical bar (|) on the command line between two
commands.
Filters work naturally with pipes.
A filter command can be used on the left of a pipe, between two
pipes, and on the right of the pipe.

Mirunalini.P (SSNCE) Unix October 18, 2023 8 / 60


Concatenating Files

UNIX provides powerful utility to concatenate commands, called


as cat as short.
cat command copies entire files.
It combines one or more files by appending them in the order they
are listed in the command.
The input can come from the keyboard; the output goes to the
monitor.

Mirunalini.P (SSNCE) Unix October 18, 2023 9 / 60


Concatenating Files

Given one or more input files, the cat command writes them one
after another to standard output.
The result is that all the input files are combined and becomes one
output

1. $ cat f i l e 1 f i l e 2 f i l e 3
This i s f i l e 1 .
This i s f i l e 2 .
This i s f i l e 3 .
The c o n t e n t w i l l be c o n c a t e n a t e d and d i s p l a y e d

Mirunalini.P (SSNCE) Unix October 18, 2023 10 / 60


Using Cat to Display file

Even primary job is to combine files cat acts as a useful tool to


display a file.
When only one input is provided, the file is concatenated with a
null file. The result is that the input file is displayed on the
monitor.

$ cat computers
A broad range of industrial and consumer products use
computers as control systems. Simple special-purpose
devices like microwave ovens and remote controls are
included,as are factory devices like industrial robots
and computer-aided design, as well as general-purpose
devices like personal computers and mobile devices like
smartphones.

Mirunalini.P (SSNCE) Unix October 18, 2023 11 / 60


cat to create a File
There is only one input this time, however, the input comes from
the keyboard. Because we want to save the file, we redirect
standard output to a file rather than to the monitor.
The keyboard command for end of file is the ctrl+d keys, usually
abbreviated as ˆd .
the end of file token ˆd cannot be seen as soon as cat detects end
of file it quits and returns control to unix

$ c a t > go od S tu de n t s
Now i s t h e time
For a l l good s t u d e n t s
To come t o t h e a i d
of their college .

#t o append t h e f i l e
$ c a t >> go o d S t u d e nt s
Mirunalini.P (SSNCE) Unix October 18, 2023 12 / 60
cat Options
There are six options available with cat. Four categories:
Visual characters
Buffered output
Missing files
Numbered lines
Visual Characters: During output display unprintable character
such as ASCII control characters cannot be seen because they have no
visual graphic.
The visual option -v allows us to see control characters, with the
exception of the tab, new line, and form feed characters.
For -ve a dollar sign is printed at the end of the each line.
For -vt, the tabs appear as
ˆI
For -vet nonprintable characters are prefixed with a caret ˆ.
Mirunalini.P (SSNCE) Unix October 18, 2023 13 / 60
cat Options- Example

To display the content using -vet option


cat >f2.txt
t h e r e a r e tab s p a c e s
1 2 3 4
There a r e s p a c e s a t t h e end o f t h e next l i n e
one two t h r e e f o u r
cat f2.txt -vet
t h e r e a r e tab s p a c e s $
1ˆ I 2 ˆ I 3 ˆ I 4 ˆ I $
There a r e s p a c e s a t t h e end o f t h e next l i n e $
one two t h r e e f o u r $
$
ˆI $

Mirunalini.P (SSNCE) Unix October 18, 2023 14 / 60


cat Options

Buffered Output:
When output is buffered, it is kept in the computer until the system
has time to write it to a file.
Normally cat output is buffered.
We can force the output to be written to the file immediately by
specifying the option -u unbuffered.
This will slows down the system.

Mirunalini.P (SSNCE) Unix October 18, 2023 15 / 60


cat Options
Missing Files:
When you concatenate several files together, if one of them is
missing, the system displays a message
Use shell redirection to send the error message to /dev/null to
effectively suppress the error message to concatenate files and
ignore any missing files.
cat f1.txt f3.txt
1
2
3
4
c a t : f 3 . t x t : No such f i l e o r d i r e c t o r y

c a t f 1 . t x t f 3 . t x t 2>/dev / n u l l
1
2
3
4
Mirunalini.P (SSNCE) Unix October 18, 2023 16 / 60
cat Options
Numbered lines:
The numbered lines option (-n) numbers each line in each file as
the line is written to standard output.
If more than one file is being written, line numbers will continue
sequentially from the first line of the first file to the last line of the
last file.
Example :
$ c a t −n go odS tu d e n ts catExample
1 : Now i s t h e time
2 : For a l l good s t u d e n t s
3 : To come t o t h e a i d
4 : of their college .
5 : There i s a tab between t h e numbers
on t h e next l i n e
6 : 1 2 3 4 5

Mirunalini.P (SSNCE) Unix October 18, 2023 17 / 60


Head Command

Head Command : Copies a specified number of lines from the


beginning of one or more files to the standard output stream.
The basic syntax : head options inputfiles
If no files specified it gets lines from standard input specifying
head -3 −
Number of lines is omitted head assumes 10 lines.
if number specified larger than total number of lines in the files
total file is used.

$ head −2 g o od St ud e nt s
Now i s t h e time
For a l l good s t u d e n t s

Mirunalini.P (SSNCE) Unix October 18, 2023 18 / 60


Head Command
When multiple files are included in one head command, head displays
the name of file before its output.
$ head −2 g ood S t u de n t s TheRaven

= => g o od St u d e nt s <=

Now i s t h e time
For a l l good s t u d e n t s

= => TheRaven <= =

Once up on a midnight dreary , w h i l e I pondered ,


weak and weary .
Over many a q u a i n t and c u r i o u s
volume o f f o r g o t t e n l o r e

Mirunalini.P (SSNCE) Unix October 18, 2023 19 / 60


Tail Command

The tail command also outputs data from the end of the file.
The basic syntax: tail options inputfile
Although only file can be referenced it has several options as
shown in the table below:

Option Code Description


Count from beginning +N Skips N-1 lines ;
copies rest to end of file
Count from end -N Copies last N lines
Count by lines -1 Count by line (default)
Count by characters -c Count by character
Count by blocks -b Count by disk blocks
Table 3: Tail - Options

Mirunalini.P (SSNCE) Unix October 18, 2023 20 / 60


Head and Tail

head −5 f 3 . t x t
head −5 f 3 . t x t | t a i l −3
1 3
2 4
3 5
4
5

Mirunalini.P (SSNCE) Unix October 18, 2023 21 / 60


Cut and Paste

Used to extract one or more columns of data from one or more


files.
Extraction can be done based on character position or by a field
number.
cut removes columns of data from a file and paste combines
columns of data
A column specification can be one column or a range of columns in
the format N-M, where N is the start column and M is the end
column, inclusively. Multiple columns are separated by commas.
Character position work well when data are aligned in fixed
columns
cut -c1-14, 19-25 sam.dat
To cut the columns from a extended list command pipe was used
with overlapping cut simply combines them into one column
ls -l | cut -c 56-70, 36-42
Mirunalini.P (SSNCE) Unix October 18, 2023 22 / 60
Cut command using field specifier
Column specification works well when the data are organized as
fixed columns.
Fields separated from each other by a terminating character
known as a delimiter.
Multiple fields are separated by commas with nospace and
consecutive fields may be specified as a range.
Eg: cut -f1 sam1.dat
Eg: cut -f1, 3-5 sam1.dat

Table 4: Options of cut Command


Option Code Results
Character -c Extracts fixed columns specified by
column number
Field -f Extracts delimited columns
Delimiter -d Specifies delimiter if not tab (default)
Suppress -s Specifies output if no delimiter in line
Mirunalini.P (SSNCE) Unix October 18, 2023 23 / 60
Paste Command
The paste command combines lines together.
It gets the input from two or more files. Eg: paste options file list
If paste contains more than two files the corresponding lines from
each file.
Between columns separated by tabs and the end of last column
writes a new line character.
The cat and paste command are similar.
The cat command combines files vertically (by lines) and the paste
command combines files horizontally (by columns).
paste odd.txt even.txt

1 one 2 4 6
2 two 8 10 12
3 three 14 16 18
4 four
Mirunalini.P (SSNCE) Unix October 18, 2023 24 / 60
Paste Command

if length of the line is uneven writes the extra data from the first
file a separation delimiter such as the tab and newline until all
data have been output. paste odd.txt even.txt
1 one 2 4 6
2 two 8 10 12
3 three 14 16 18
4 four
5 five

p a s t e −d ”#” even . t x t odd1 . t x t


2 4 6#1 one
8 10 12#2 two
14 16 18#3 three

The delimiters gets rotated if more number of files are specified

Mirunalini.P (SSNCE) Unix October 18, 2023 25 / 60


Sorting

When data is large, need to organize them for analysis and


efficient processing.
Simplest and most powerful organizing technique is sorting
Sorting: Arranging data in sequence either in ascending order or
descending order.
Sort utility in Unix helps to sort data
Sort utility uses options, field specifiers and input files.
Eg: sort options field specifiers inputfiles

Mirunalini.P (SSNCE) Unix October 18, 2023 26 / 60


Sorting
The easiest sort arranges the data by lines,
Sorting undergoes character-by-character comparison
Comparison continues until either all character in both lines have
compared equal or until two unequal characters are found.
If lines are not equal, comparison stops and sort determines which
line should be first based on the two unequal characters.
In comparing characters, sort uses the ASCII values of each
character.
Unsorted Data Sorted Data
forouzan gilberg
gilberg :
8 27
27 8
: Paula
˜ Paulo
Paulo forouzan
Paula ˜
Mirunalini.P (SSNCE) Unix October 18, 2023 27 / 60
Sorting - Field Specifiers

In field sort need to define which field or fields are used for the
sort.
Field specifiers are a set of two numbers that together identify the
first and last field in a sort key.
+number1 -number2
number1 specifies the number of fields to be skipped to get to the
beginning of the sort field
number2 specifies the number of fields to be skipped, relative to
the beginning of the line to get to the end of the sort key.
+0 -1 => 0 fields are skipped and the end is one field away
+2 -4 => sorted based on 3rd and 4th field
+3 => 4th to end fields
Sort should be delimited by one space or tab if more spaces then
null field is considered in between the fields

Mirunalini.P (SSNCE) Unix October 18, 2023 28 / 60


Sorting - Field Specifiers

$cat > censusspace


Chennai TN 893457 589765 896713
B a n g a l o r e KA 795623 895671 568712
Faridabad HR 589657 895756 785123
Imphal MN 785965 586972 698713
U j j a i n MP 528958 896257 268985

s o r t +0 −1 r 2 . dat − S o r t based a s t h e 1 s t f i e l d
B a n g a l o r e KA 795623 895671 568712
Chennai TN 893457 589765 896713
Faridabad HR 589657 895756 785123
Imphal MN 785965 586972 698713
U j j a i n MP 528958 896257 268985

Mirunalini.P (SSNCE) Unix October 18, 2023 29 / 60


Delimiter Option

D e l i m i t e r o p t i o n (− t ) : S p e c i f i e s an a l t e r n a t e \\ d e l i m i
$ c a t > amp
Chennai &TN
B a n g a l o r e &KA
Faridabad &HR
Imphal &MN

$ s o r t −t & amp
B a n g a l o r e &KA
Chennai &TN
Faridabad &HR
Imphal &MN
Mirunalini .

Mirunalini.P (SSNCE) Unix October 18, 2023 30 / 60


Sorting and Merging

Merge F i l e s : Combines m u l t i p l e o r d e r e d
f i l e s i n t o one f i l e
$ c a t > male $ c a t > f e m a l e
Arun Brindha
Sai Seetha

s o r t −m male f e m a l e
Arun
Brindha
Sai
Seetha
If input files are not sorted the merge sort to its maximum
and produce merge files

Mirunalini.P (SSNCE) Unix October 18, 2023 31 / 60


Unique sort Fields

Unique S o r t F i e l d s : E l i m i n a t e s a l l k e e p i n g
one l i n e when t h e s o r t f i e l d s
are i d e n t i c a l

$ cat > state . txt


c h e n n a i TN SI 856
B a n g l a o r e KA SI 568
U t t a r p r a d e s h MP NI 900
Himachalpradesh HP NI 500
Bihar B EI 400

s o r t +2 −3 −u s t a t e . t x t
Bihar B EI 400
U t t a r p r a d e s h MP NI 900
c h e n n a i TN SI 856

Mirunalini.P (SSNCE) Unix October 18, 2023 32 / 60


Sorting Numeric Values

Numeric Values To s o r t t h e numeric data c o r r e c t l y , u s e


comparison −n
s o r t −n +4 c e n s u s s p a c e

U j j a i n MP 528958 896257 268985


B a n g a l o r e KA 795623 895671 568712
Imphal MN 785965 586972 698713
Faridabad HR 589657 895756 785123
Chennai TN 893457 589765 896713
Note: If -n option was not used then numeric values are sorted based
on ASCII values only.
Reverse Order: To order the data from largest to smallest
Both numeric and reverse can be combined
sort –nr +2 -3 censusspace
sort +2nr -3 censusspace
Mirunalini.P (SSNCE) Unix October 18, 2023 33 / 60
Dictionary Sorting

Dictionary Sorting: Normal sorting make the special characters


to be intermixed with digits and alphabetic but dictionary sorting
moves all the characters to the top.
To sort the index correctly, dictionary option –d is used.
Fold Lowercase: The sorting ignores the difference between
uppercase and lowercase where the uppercase can be folded into
lowercase all sort the same.
sort -df symbol.txt

Mirunalini.P (SSNCE) Unix October 18, 2023 34 / 60


Frame Title

Figure 1: Caption
Mirunalini.P (SSNCE) Unix October 18, 2023 35 / 60
Multi Pass Sorting
Defines two different fields for sorting.
List the fields one after another
Phoenix AZ 899
Sanfrancisco CA 890
Sandiego CA 85796
Dallas TX 89576
Sanantonio TX 7859
Table 6: City Table

sort +1 -2 +2nr -3 mul


Phoenix AZ 899
Sandiego CA 85796
Sanfrancisco CA 890
Dallas TX 89576
Sanantonio TX 7859
Table 7: City Table - SORTED
Mirunalini.P (SSNCE) Unix October 18, 2023 36 / 60
Translating Characters

Translating characters from one set to another.


Eg: convert lowercase characters to uppercase, or vice versa.
UNIX provides translate utility making conversions from one set
to another.
The tr command replaces each character in a user-specified set of
characters with a corresponding character in a second specified set.
Each set is specified as a string and specified using quotes.
The tr command is represented as follows:
tr options string1 string2

Mirunalini.P (SSNCE) Unix October 18, 2023 37 / 60


Translating Characters

Translate receives its input from standard input(keyboard) and


writes its output to standard output.
The text is matched against the string1 set, and any matching
characters are replaced with the corresponding characters in the
string2 set.
Unmatched characters are unchanged.
Unix translates one line at a time changing specified characters
until it finds end of file. tr “aeiou” “AEIOU”
It is very easy to use TRANSLATE
It Is vEry EAsy tO UsE TRANSLATE

Mirunalini.P (SSNCE) Unix October 18, 2023 38 / 60


Non-Matching Translating Characters

When the translate strings are of different length, the result


depends on which string is shorter.
If string2 is shorter, the unmatched characters will all be changed
to the last character in string2.
tr “aeiou” “AE?”
case 1: string2 is shorter than string1
It is very easy to use TRANSLATE.
It ?s vEry EAsy t? ?sE trAnslAtE.
if string1 is shorter, the extra characters in string2 are ignored.
case2: String1 is shorter than string1 tr “aei” “AEIOU”
It Is vEry EAsy to usE trAnslAtE.

Mirunalini.P (SSNCE) Unix October 18, 2023 39 / 60


Delete and Squeeze Characters

Delete Characters:
To delete matching characters in the translation we use the delete
option (-d).
Note that the delete option does not use string2.
tr –d “aeiouAEIOU”
It is very easy to use TRANSLATE
t s vry sy t s TRNSLT
Squeeze Output:
The squeeze option deletes consecutive occurrences of the same
character in the output.
After translation if the output contains a string of ”d’s” all but one
would be deleted.
tr –s “ie” “dd”
The fiend did dastardly deeds
Thd fdnd d dastardly ds

Mirunalini.P (SSNCE) Unix October 18, 2023 40 / 60


Complement
The complement option reverses the meaning of the first string.
Rather than specifying what characters are to be changed, it says
what characters are not to be changed.
tr –c “aeiou” “*”
It is very easy to use TRANSLATE.
***i***e***ea****o*u*e***********
Cat > sample . t x t
The d a s t a r d l y f i e n d d i d 101 d a s t a r d l y d e e d s .
He was t r u l y d a s t a r d l y .
tr -cs ”A-Za-z” ”newline” < dastardly.txt
The
dastardly
fiend
did
dastardly
Mirunalini.P (SSNCE) Unix October 18, 2023 41 / 60
Files with Duplicate Lines

uniq Command: The uniq command deletes duplicate lines,


keeping the first and deleting the others.
To be deleted, the lines must be adjacent.
Duplicate lines that are not adjacent are not deleted.
To delete nonadjacent lines the file must be sorted.
Unless otherwise specified the whole line can be used for
comparison.
Options provide for the compare to start with a specified field or
character.
It is not possible to compare one filed in the middle of the line.
The unique command is shown as follows: uniq options inputfile

Mirunalini.P (SSNCE) Unix October 18, 2023 42 / 60


Files with Duplicate Lines

Input s a m t e x t . t x t
5 completely duplicate l i n e s
5 completely duplicate l i n e s
Not a d u p l i c a t e − − next d u p l i c a t e s f i r s t 5
5 completely duplicate l i n e s
Last 3 f i e l d s d u p l i c a t e : one two t h r e e
Last 3 f i e l d s d u p l i c a t e : one two t h r e e
The next 3 l i n e s a r e d u p l i c a t e a f t e r c h a r 5
uniq sam text.txt
Output :
5 completely duplicate l i n e s
Not a d u p l i c a t e − − next d u p l i c a t e s f i r s t 5
5 completely duplicate l i n e s
Last 3 f i e l d s d u p l i c a t e : one two t h r e e
The next 3 l i n e s a r e d u p l i c a t e a f t e r c h a r 5
Mirunalini.P (SSNCE) Unix October 18, 2023 43 / 60
Files with Duplicate Lines
cat unique1.txt
5 completely duplicate l i n e s
5 completely duplicate l i n e s
Not a d u p l i c a t e − − next d u p l i c a t e s f i r s t 5
5 completely duplicate l i n e s
Last 3 f i e l d s d u p l i c a t e : one two t h r e e
Last 3 f i e l d s d u p l i c a t e : one two t h r e e
The next 3 l i n e s a r e d u p l i c a t e a f t e r c h a r 5
abcde d u p l i c a t e t o end
f g h i j d u p l i c a t e t o end
uniq -u unique1.txt
Not a d u p l i c a t e − − next d u p l i c a t e s f i r s t 5
5 completely duplicate l i n e s
The next 3 l i n e s a r e d u p l i c a t e a f t e r c h a r 5
abcde d u p l i c a t e t o end
f g h i j d u p l i c a t e t o end
Mirunalini.P (SSNCE) Unix October 18, 2023 44 / 60
Files with Duplicate Lines

-d:Displays only duplicated lines


uniq -d unique1.txt
5 completely duplicate lines
Last 3 fields duplicate: one two three
uniq -c unique1.txt
Displays count of duplicated lines

2 5 completely duplicate l i n e s
1 Not a d u p l i c a t e − − next d u p l i c a t e s f i r s t 5
1 5 completely duplicate l i n e s
2 Last 3 f i e l d s d u p l i c a t e : one two t h r e e
1 The next 3 l i n e s a r e d u p l i c a t e a f t e r c h a r 5
1 abcde d u p l i c a t e t o end
1 f g h i j d u p l i c a t e t o end

Mirunalini.P (SSNCE) Unix October 18, 2023 45 / 60


Unique
Skip Leading Fields: While the default compares the whole line
to determine if two lines are duplicate, we can also specify where
the compare is to begin.
The skip duplicate fields option (-f) skips the number of fields
specified starting at the beginning of the line and any spaces
between them.
uniq -d -f 4 sam text.txt
5 completely duplicate lines
Last 3 fields duplicate: one two three
abcde duplicate to end
Skip Leading Characters: Specifies number of characters to be
skipped before starting the comparsion.
uniq -d -s 5 sam text.txt
5 completely duplicate lines
Last 3 fields duplicate: one two three
abcde duplicate to end
Mirunalini.P (SSNCE) Unix October 18, 2023 46 / 60
Count of characters, Words or LInes

wc Command:The wc command counts the number of characters,


words and lines in one or more documents.
The character count includes newlines (/n).

wc TheRaven
116 994 5782 TheRaven
l i n e s words c h a r
options :
wc -c : No of Characters
wc -l: No of Lines
wc -w : No of words

Mirunalini.P (SSNCE) Unix October 18, 2023 47 / 60


Comparing Files

There are three UNIX commands that can be used to compare the
contents of two files:
compare (cmp), difference (diff ), and common (comm)
The cmp command examines two files byte by byte.
Its operation is shown below:
cmp options file1 file2
cmp without Options
When the cmp command is executed without any options, it stops at
the first byte that is different.
The byte number of the first difference is reported.

Mirunalini.P (SSNCE) Unix October 18, 2023 48 / 60


Comparing Files

F i r s t with two i d e n t i c a l f i l e s and then with d i f f e r e n t


$ c a t cmpFile1 $ c a t c m p f i l e 1 . cpy
123456 123456
7890 7890
cmp c m p f i l e 1 c m p f i l e 1 . cpy

c a t cmp f i l e 2
123456
as9u

cmp c m p f i l e 1 c m p f i l e 2
cmpfile1 cmpfile2 d i f f e r : char 8 l i n e 2

Mirunalini.P (SSNCE) Unix October 18, 2023 49 / 60


Comparing Files
cmp -l cmpfile1 cmpfile2
8 67 141
9 70 163
11 60 165
The list option displays all the differences line by line in bytes and
also the octal value of two characters.
cmp with suppress list option (-s) The suppress list option is
similar to the default except that no output is displayed.
When no output displayed the results determined using exit status.
if the exit status is ”0” the files are identical, if it is ”1” that is
different
cmp −s c m p f i l e 1 c m p f i l e 1 . cpy
echo ? 0
cmp −s cmpfile1 cmpfile2
echo ? 1
Mirunalini.P (SSNCE) Unix October 18, 2023 50 / 60
Diff Command
The diff command shows line-by-line differences between two files.
The first file is compared with the second file.
The arguments can be two files, a file and directory, or two
directories.
When one file and one directory are specified, the utility looks for
a file with the same name in the specified directory.
If two directories are provided, all files with matching names in
each directory are used. Each difference is displayed using the
following format:
range1 a c t i o n range2
< t e x t from f i l e 1
− − −
> t e x t from f i l e 2
The first line defines what should be done at range1 in file1 to
make it match the lines at range2 in file2
Mirunalini.P (SSNCE) Unix October 18, 2023 51 / 60
Diff Command
The action can be change (c), append (a), or delete (d).
Change (c): indicates what action should be taken to make file1
the same as file2. Note that a change is a delete and replace. The
line(s) in range1 are replaced by the line(s) in range2.
change(c): 6c6 <hello — >greeting
Replace line 6 in file1 with line 6 in file2
Append (a): Indicates what lines need to be added to file1 to
make it the same as file2. Appends can take place only at the end
of the file1; they occur only when file1 is shorter than file2.
append(a):25a 26,27 >bye>good bye
Append: At the end of file1(after line 25), insert lines 26 and 27
from file2.
Delete (d): indicates what lines must be deleted from file1 to make
it the same as file2. Deletes can occur only if file1 is longer than
file2.
delete(d) 78, 79 d 77 <line 78 text < line 79
Delete: the extra lines at the end of file1 should be deleted
Mirunalini.P (SSNCE) Unix October 18, 2023 52 / 60
diff command

cat>d i f f 1
one same
two same
x and y
same
x
˜ $ cat>d i f f 2
one same
two same
y and x
same
not x
extra l1

Mirunalini.P (SSNCE) Unix October 18, 2023 53 / 60


diff command

˜$ d i f f diff1 diff2
3 c3
< x and y
−−−
> y and x
5 c5 , 6
< x
−−−
> not x
> extra l1

Mirunalini.P (SSNCE) Unix October 18, 2023 54 / 60


diff - example

cat diff1 cat diff2


one same one same
two same two same
x and y y and x
same same
same same
x not x
y not y
same same
extra l1
extra l2
Table 8: Input Data

Mirunalini.P (SSNCE) Unix October 18, 2023 55 / 60


Diff- example

diff diff2 diff1


3 c3
< y and x
−−−
> x and y
6 , 7 c6 , 7
< not x
< not y
−−−
> x
> y
9 , 1 0 d8
< extra l1
< extra l2

Mirunalini.P (SSNCE) Unix October 18, 2023 56 / 60


comm Command

The comm command finds lines that are identical in two files.
It compares the files line by line and displays the results in three
columns
The left column contains unique lines from file1
The center column contains unique lines from file2
The right column contains lines found in both files

Mirunalini.P (SSNCE) Unix October 18, 2023 57 / 60


comm command
comm comm1 comm2

one same
two same
d i f f e r e n c e comm2
d i f f e r e n t comm1
same a t l i n e 4
same a t l i n e 5
comm : f i l e 1 i s not i n s o r t e d o r d e r
comm : f i l e 2 i s not i n s o r t e d o r d e r

not i n comm2
same a t l i n e 7
same a t l i n e 8
not i n comm1

Mirunalini.P (SSNCE) Unix October 18, 2023 58 / 60


Studied about Filter commands
cat, Head and tails,cut and paste
sorting , translating characters,
Removing duplicate lines
Counting characters, Words and lines
Compare , diff and common commnads

Mirunalini.P (SSNCE) Unix October 18, 2023 59 / 60


References

Behrouz Forouzan, Richard Gilberg “UNIX and Shell


Programming: A Textbook”, Thomson Learning, 2005

Mirunalini.P (SSNCE) Unix October 18, 2023 60 / 60

You might also like