You are on page 1of 11

Experiment - 5

Student Name: Raghav kumar Duggal UID: 19BCA1469


Branch: BCA Section/Group: B/2
Semester: 4th
Subject: Linux Administration Lab Subject Code:CAP-257

• Aim/Overview of the practical: To work and execute commands of


Filters and Regular Expressions in Linux

Task to be done:

• Enlist all files that start with f and have i or R as second character and
end with a number from range 0-6. Write a command to search the
word Good or good and have any integer from 0-9 at the end of that
word. Exemplify egrep and fgrep for searching above pattern. List all
files starting with word File and having any digit as fifth character and
nothing else. List all files starting with a letter and ending in a number.

• Create a file using vi editor named filters.txt. Enter the content in that
file as follows:

“Anyone who has never made a mistake has never tried anything
new.”

Albert Einstein

After creating a file, count the characters only of a file. Sort the
contents and check if there is any duplicate content exists. After that,
display the line numbers using cat command. Check the occurrences
of word “has” using quantifiers. Search a word “never” which is
preceded by a word “made” and replace it with a word “NEVER”.
• Concept used:

These are some commands and concepts that were being discussed during
the

Lecture time.

sort file1 [ to sort file 1]

uniq -c file1 [ count the occurrences]

sort file1 | uniq -c

cat file1 | tr [a-z] [A-Z]

or

cat file1 | tr [: lower] [: upper]

echo "welcome" | tr -d 'e' [to delete 'e' ]

echo 'welcomeeeee' | tr s 'e'

// sed (Stream Editor)

s- [search]

p- [print]

d- [delete]

g- [global declaration]
-i [ignore the case]

-n [no. of lines]

-e [to specify multiple command in single row].

** search a word and replace the word **

sed 's/word1/word2' filename [for one time]

sed 's/word1/word2/g' filename [for all time]

sed 's/word1/word2/nth' filename [nth occurrences]

sed 'nth s/word1/word2/' filename [ s&r for specific no. of lines]

sed '$d' filename [delete last line]

sed '3,6d' filename [ 3 to 6 will not print]

sed 'nth d' filename [ nth line will not print]

sed -n 's/word1/word2/p' filename [print only replace line]

sed -e 's/word1/word2/' -e 's/word3/word4/' filename

[ change multiple words]

// wildcards and quantifiers //

wildcards-

? [for exactly one occurance] [ex - C?T]

$ [ end with this word] [$r] [

^ [starts with the word] [^a] [ find out the file in dir = ls ^s]
+ [one or more occurance] [a+le]

* [0 or more occurance]

. [matching the character]

( ) [ use for regular expression

[ ] [range of occurances]

// Regular expression //

grep

egrep

fgrep

-n [Show line number while displaying the output]

-c [Show count displaying the output]

-i [ignore the cases]

-v [count lines which doesn’t display the content]

-o [Show only the matched string]

-w [search a word not subpart]

-b [show the position where it matches the pattern in the file]


• Steps/Commands involved to perform practical:

Question (1):

Step 1: Open your Linux terminal

Step 2: To enlist all files that start with f and have i or R as second
character and end

with a number from range 0-6 use command

ls f[iR]*[0-6] (ls to list the files)

(using * because the length of word


is not

mentioned in the question)

(using [ ] for range of values)

Step 3: To search the word Good or good that have any integer from
0-9 at the end of

that word use command of egrep-


egrep ‘good[0-9]|Good[0-9]’ file77 (using [ ] for
range of values)

Step 4: Now to search with fgrep command use –

fgrep -h “good” file77

OR (using -h to the matched


content)

fgrep -h “Good” file77

Step 5: To list all files starting with word ‘file’ and having any digit
as fifth

character and nothing else use command-

ls file[0-9] (using [ ] for range of values)

Step 6: To List all files starting with a letter and ending in a number
use-

ls [a-z]*[0-9] (using * because the length of


file name is not

mentioned i.e., more than one


occurrence)
Question (2)

Step 1: Open your Linux terminal

Step 2: To create a file named ‘filters.txt’ using vi editor use


command-

vi filters.txt

Step 3: Now press ‘i’ to enter insert mode and then add the content

Step 4: To count the characters in the file ‘filters.txt’ use command-

wc -c filters.txt

Step 5: To sort file and check for duplicates use command –

sort filters.txt | uniq -c

Step 6: To display line numbers use –

cat -n filters.txt ( using -n for counting the lines in


a file)

Step 7: To search for word ‘has’ use command –

egrep ‘has’ filters.txt


Step 8: To search a word “never” which is preceded by a word
“made” and

replace it with a word “NEVER” use command –

egrep 'never ? made' filters.txt | sed 's/never/NEVER/'


filters.txt

(using ‘?’ for establishing consecutiveness of words ‘never


made’ because

there is only a space between two words)

(using sed command to change the words and pipe to


concatenate two

commands)

• Result/Output/Writing Summary:

Question (1) Output:

Fig.1(Enlisting file that starts with f) Fig.3 (using fgrep


command)
Fig.2(searching word ‘good’) Fig.4(listing file that
ends with integer)

Fig.5(listing file that starts with alphabet and ends with integers)

Question (2) Output:


Fig.6(sorting, counting, searching in a file)

Fig.7(searching word and replacing word)

Learning outcomes (What I have learnt):

1. How to use Regular expressions

2. Using Wildcards and quantifiers with Regular expressions

3. Using Stream editor(sed) and sorting and uniq commands


Evaluation Grid:

Sr. Parameters Marks Obtained Maximum Marks


No.
1. Worksheet 10
2. Demonstration/Performance 5
/Pre Lab Quiz
3. Post Lab Quiz 5

You might also like