OSA3
OSA3
a) owner access 7 1 11
RWX
b) group access 6 11 0
RWX
Column 1 gives the names which are present in first file but not in second file
Column 2 gives the names which are present in second file but not present in first file
Column 3 gives the names which are present in both the Files
diff: Display file differences. Diff displays which lines in one file have to be changed to
make the file identical to other.
Syntax: diff file1 file2
E.g.:
Contents of file f1: apple, mango, orange
Contents of file f2: grape, mango, musk melon
diff f1 f2
Understanding Output
c – change d – delete a – append
1c1
<apple…
grape>
1c1: 1st line of 1st file to be changed to match 1st line of 2nd file
“<” means 1st file
“>” means 2nd file
3c3: likewise can be understood.
Filters / Text Processing Commands
Filters are commands which accept data from standard input manipulate it and write the
results to standard
Output.
head: command displays the top “n” lines of the file. When used without any option it will
display first 10 lines of the file. (Note: Create a file sample.txt using text editor and type
at least 15 lines)
Syntax: head filename
Example: head sample1.txt
/*display first 10 lines*/
tail: command displays the end of the file. By default it will display last 10 lines of the
file.
Syntax: tail filename
Example: head sample1.txt
/*display last 10 lines*/
(Note: Create a file sample.txt using text editor and type at least 15 lines)
tail or head with -n followed by a number will display that many number of lines from last
and from first respectively.
head -n 20 sample1.txt
/* will display first 20 lines*/
tail -n 15 sample1.txt
/* will display last 15 lines */
cut: Cut command in linux is used to select sections of text from each line of files. The
cut command can be used to select fields or columns from a line by specifying a delimiter
or to select a portion of text by specifying the range or characters. Basically the cut
command slices a line and extracts the text.
Example: Create a file using text editor “file.txt” with following data
unix or linux os
is unix good os
is linux good os
cut -c4 file.txt
x
u
l
xo
ui
ln
The above cut command prints the characters from fourth position to the seventh position
in each line.
x or
unix
linu
The cut command can be used to extract the fields in a file using a delimiter. The -d option
in cut command can be used to specify the delimiter and -f option is used to specify the
field position.
cut -d' ' -f2 file.txt
or
unix
linux
This command prints the second field in each line by treating the space as delimiter.
More than one field can be printed by specifying the position of the fields in a comma
delimited list.
or linux
unix good
linux good
The above command prints the second and third field in each line.
User can print a range of fields by specifying the start and end position.
cut -d' ' -f1-3 file.txt
The above command prints the first, second and third fields.
To print the first three fields, you can ignore the start position and specify only the end
position.
cut -d' ' -f-3 file.txt
paste:
Paste command will paste the contents of the files side by side
create “file1” with following contents using text editor and contents must be separated by
TAB
sourav 40 male
sachin 45 male
pooja 16 female
suchithra 17 female
create “file2” with following contents using text editor and contents must be separated by
TAB
IV CS NRAMP
II EC KARKALA
VI CE MANIPAL
II EE UDUPI