Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Using Filters and Regular Expressions

Student Name: Vishu Raman UID: 21MCA2234


Branch: MCA Section/Group: MCA-2/A
Semester: 1st Date of Performance:10/11/21
Subject Name: Linux Administration Lab Subject Code: 21CAP-608

1. Aim/Overview of the practical:

A filter is a program that reads standard input, performs an operation upon it and writes the
results to standard output. For filtering the data, we can use Linux Filter Commands.

There are various commands that are known as filter commands such as

 Head
 Tail
 Uniq
 Sed
 Wc
 sort

2. 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”.
3. Concept used:

Filter Commands:

Wc : Using this command, we can count word in file along with word count,character
count and line count

Syntax: wc filename

working with wc command

 -c: To count the no of characters in a file


 -l : To count the no of lines in a file
 -w : To count the no of words in a file

nl: Using this command, we can count no. of lines in a file

Syntax: nl filename

head: head is used to display the first parts of a file, it outputs the first 10 lines by default.

Syntax: head filename

working with head command:

 -n: use the –n to display the desired number of lines


 -c: use the –c to display the desired number of characters

tail: tail outputs the last parts (10 lines by default) of a file.

Syntax: tail filename

working with tail command:

 -n: use the –n to display the desired number of lines

 -c: use the –c to display the desired number of characters


sort: The sort filter (alphabetical sort) is used to sort lines of text files.

Syntax: sort filename

working with sort command:

 -d: use -d to sort the content in dictionary order


 -r: use –r to sort the content in reverse order
 -f: use –f to ignore the cases
 -o: use –o to sort the content and give its output to another file
 -n: use -n to sort the numeric content

cat: The cat (short for “concatenate“) command is one of the most frequently used
command in Linux/Unix like operating systems. cat command allows us to create single or
multiple files, view contain of file, concatenate files and redirect output in terminal or files.

Syntax: cat filename

working with cat command:

 -n: use –n to display the content of file along with line numbers
 -e: use –e to display the content of file along with end of line

Tac : tac command in Linux is used to concatenate and print files in reverse

Syntax: tac filename

uniq: uniq command is used to report or omit repeated lines, it filters lines from standard input
and writes the outcome to standard output.

Syntax: uniq option filename

working with uniq command:

 -u: use -u to display unique data


 -c: use -c to count the repeated content
 -i: use –i to ignore the cases
 -D: use -D to display and count the duplicate content
 -d: use -d to display duplicate content

pipe: pipe is used to combine two or more commands

Syntax: command1 | command2

tr:tr is used to translate the content of file

Syntax: tr option filename

tee: tee reads the standard input and write it to both standard output and on one or more files

Syntax: tee –a filename

cut: cut command is used to cut out selected fields (columns) of each line of a file, depending
on a delimiter or a count of bytes.

Syntax: cut option value filename

sed:Sed is a stream editor. A stream editor is used to perform basic text transformations on an
input stream (a file or input from a pipeline).

Syntax: sed options value filename

working with sed command:

 -s: use s to search and replace


 -p: use p to print the data
 -d: use delete to delete the data
 -g: g is used for global declaration
 -i: use -i to ignore the cases
 -n: use –n to perform operation in lines
Steps/Commands involved to perform practical:

 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.
Steps/Commands involved to perform Question1 is:-

1. Create files are file1, from, file1234 and file26 and add content in file26 is good
good234, Good456.
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 by using command ls f[i,r]*[0-6] .
3. To search the word Good or good and have any integer from 0-9 at the end of that
word by using command egrep “[G,g]ood[0-9]” file26
4. List all files starting with word File and having any digit as fifth character and nothing
else by using command ls file[0-9]
5. List all files starting with a letter and ending in a number by using command ls [a-
z]*[0-9].

Steps/Commands involved to perform Question2 is:-

1. To create a file using vi editor named filters.txt. by using command vi filters.txt.


2. Enter the content in file filters.txt “Anyone who has never made a mistake has
never tried anything new.”
Albert Einstein
,To enter the content press I then enter content and then esc and :wq to save the
content in file filters.txt.
3. After creating a file, count the characters only of a fileby using command wc –c
filters.txt
4. Sort the contents and check if there is any duplicate content exists by using
command sort filters.txt | uniq –d filters.txt
5. After that, display the line numbers using cat command is cat –n filters.txt
6. Check the occurrences of word “has” using quantifiers by using command egrep
“has{1,}” filters.txt
7. Search a word “never” which is preceded by a word “made” and replace it with a
word “NEVER” by using command sed ‘s/never\+ made/NEVER made/g’ filters.txt

Output:
Output of 1:

Output 2:
Learning outcomes (What I have learnt):

1. I have learnt about Filter Commands.

2. I have learnt about Regular Expressions.

3. In Regular Expressions I have learnt about Wildcard, Quantifiers, grep ,egrep,fgrep.

Evaluation Grid:

Sr. No. Parameters Marks Obtained Maximum Marks


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

You might also like