4.
Working of Comparison and Searching Commands
A .Work out the following redirection operators
1.Display all the input that are typed in the console immediately using cat command
[eeeb122@sel-48 ~]$ cat > s1
abcde
123
eee
345
cse
^Z
[8]+ Stopped cat > s1
[eeeb122@sel-48 ~]$ cat < s1
abcde
123
eee
345
cse
2.Create a file "College" and "Country" using output direction operator
OUTPUT:
[eeeb122@sel-48 ~]$ cat > college
SSN college of engg.
^Z
[9]+ Stopped cat > college
[eeeb122@sel-48 ~]$ cat > country
India
^Z
[10]+ Stopped cat > country
3. Write the output of ls in a file and count the number of lines in that file
OUTPUT:
[eeeb122@sel-48 ~]$ ls
college Downloads f1 fn2 Music r2 Templates
country Ex 2 output.pdf fags fn3 Pictures run1 Videos
Desktop ex 3. fd fn4 Public ssn
Documents ex 3.pdf fn1 gafs r1 SSN
[eeeb122@sel-48 ~]$ ls < s2 | wc -l
26
4. Concatenate College and country files information into a single file called
"colleges_and_country"
OUTPUT:
[eeeb122@sel-48~]$cat college country>> colleges_and_country
[eeeb122@sel-48 ~]$ cat < colleges_and_country
SSN college of engg.
India
5.Append some more college information in college file and display the contents of
college file using input direction operator
OUTPUT:
[eeeb122@sel-48 ~]$ cat >> college
St.Joseph
^Z
[11]+ Stopped cat >> college
[eeeb122@sel-48 ~]$ cat < college
SSN college of engg.
St.Joseph
B. Work out the following Pipe Command ( | )
1. Count the number of lines available in listing your directory
OUTPUT:
[eeeb122@sel-48 ~]$ ls | wc -l
27
[eeeb122@sel-48 ~]$ ls
college Documents ex 3.pdf fn1 gafs r1 SSN
colleges_and_country Downloads f1 fn2 Music r2 Templates country Ex 2
output.pdf fags fn3 Pictures run1
Videos Desktop ex 3. fd fn4 Public ssn
2. Count the number of users logged in
OUTPUT:
[eeeb122@sel-48 ~]$ who -q
eeeb122 eeeb122
# users=2
3. Show the name of users who were logged in
OUTPUT:
[eeeb122@sel-48 ~]$ who -m
eeeb122 pts/0
4. Show the name and time of login users
OUTPUT:
[eeeb122@sel-48 ~]$ who
eeeb122 tty7 2017-02-04 04:04 (:0)
eeeb122 pts/0 2017-02-04 04:08 (:0.0)
5. Create a file named "big_file" which contains more than 50 lines. Show the
difference between the commands "cat big_file" and "cat big_file | more"
OUTPUT:
[eeeb122@sel-48 ~]$ cat > big_file
a
g
h
d
f
s
d
f
9(more than 50)
^Z
[12]+ Stopped cat > big_file
[eeeb122@sel-48 ~]$ cat big_file
a
g
h
d
f
s
d
f
9(more than 50)
[eeeb122@sel-48 ~]$ cat big_file | more
a
g
h
d
f
s
d
f
9(more than 50)
--More--
C. Work out the following Comparison & Searching Commands
1. Create Three files file1, file2, file3 as needed for each command
OUTPUT:
[eeeb122@sel-48 ~]$ cat > a1
ABCD
12345
EEE
run
^Z
[14]+ Stopped cat > a1
[eeeb122@sel-48 ~]$ cat > a2
abcd
12345
eee
run
^Z
[15]+ Stopped cat > a2
[eeeb122@sel-48 ~]$ cat > a3
abcde
12345
eee
run
^Z
[16]+ Stopped cat > a3
2. Show that the given two files are differed by giving the message " Files are differ"
OUTPUT:
[eeeb122@sel-48 ~]$ diff -q a1 a2
Files a1 and a2 differ
3. Show that the given two files are same in case insensitive manner
OUTPUT:
[eeeb122@sel-48 ~]$ diff -i a1 a2
[eeeb122@sel-48 ~]$
4. Compare the given two files and show they are differ or not
OUTPUT:
[eeeb122@sel-48 ~]$ diff a1 a2
1c1
< ABCD
---
> abcd
3c3,4
< EEE
---
> eee
>
5. What is the difference between diff and cmp command
OUTPUT:
diff checks line by line
cmp checks byte by byte (i.e character by character)
[eeeb122@sel-48 ~]$ diff a2 a3
1c1
< abcd
---
> abcde
[eeeb122@sel-48 ~]$ cmp a2 a3
a2 a3 differ: byte 5, line 1
6. Compare and show that the first 4 bytes of two given files are same. But
the whole file is different
OUTPUT:
[eeeb122@sel-48 ~]$ cmp a2 a3
a2 a3 differ: byte 5, line 1
[eeeb122@sel-48 ~]$ cmp -n4 a2 a3
[eeeb122@sel-48 ~]$
7. Compare and show how each byte in the given two files differ among
themselves
OUTPUT:
[eeeb122@sel-48 ~]$ cmp -l a2 a3
5 12 145
6 61 12
7 62 61
8 63 62
9 64 63
10 65 64
11 12 65
12 145 12
15 12 145
18 162 12
19 165 162
20 156 165
21 12 156
cmp: EOF on a2
8. Search whether the given pattern is available in file1 and file3
OUTPUT:
[eeeb122@sel-48 ~]$ grep bcd a2 a3
a2:abcd
a3:abcde
9. Search the file for the given pattern and print its count
OUTPUT:
[eeeb122@sel-48 ~]$ grep bcd a2 | wc -l
1
10.Search the file and print the non pattern matching lines
OUTPUT:
[eeeb122@sel-48 ~]$ grep -v bcd a2
12345
eee
11. Search the file and count the number of non pattern matching lines
OUTPUT:
[eeeb122@sel-48 ~]$ grep -v bcd a2 | wc -l
12.Search the given pattern in the file by case insensitive manner
OUTPUT:
[eeeb122@sel-48 ~]$ grep -i BCD a2
abcd
13.Create a file which contains the patterns to be searched. Search the pattern
file with file1
OUTPUT:
[eeeb122@sel-48 ~]$ cat > pattern
abc
eee
^Z
[16]+ Stopped cat > pattern
[eeeb122@sel-48 ~]$ grep f pattern file1
eee
14.What is the expansion for grep?
Global Regular Expression Print
15.What do you mean by regular expression?
Matching the strings or texts such as character ,word, pattern.