0% found this document useful (0 votes)
55 views

PRACTICAL 2 (Linux OS)

The document provides examples of using pipes to concatenate Linux commands. Pipes allow the output of one command to act as input for the next command. Some examples shown are sorting a file with cat and sort, searching for a pattern with grep, displaying the first few lines with head, last few lines with tail, and line count with wc. Tee is used to store output in a file while also displaying it. The conclusion states pipes are used to concatenate general purpose Linux commands to perform shell scripting tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

PRACTICAL 2 (Linux OS)

The document provides examples of using pipes to concatenate Linux commands. Pipes allow the output of one command to act as input for the next command. Some examples shown are sorting a file with cat and sort, searching for a pattern with grep, displaying the first few lines with head, last few lines with tail, and line count with wc. Tee is used to store output in a file while also displaying it. The conclusion states pipes are used to concatenate general purpose Linux commands to perform shell scripting tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Practical No.

2
Aim : Use pipe to concatenate the general purpose linux command.
Theory:
Pipes help combine two or more commands and are used as input/output concepts in a command.
In the Linux operating system, we use more than one pipe in command so that the output of one
command before a pipe acts as input for the other command after the pipe.
Syntax:
Command 1 | command 2 | command 3 | ……

Examples:
sort :The data present in the file is unordered. So, This will sort the given file.
Command : cat Branch.txt | sort
Output:

Grep:
The grep filter
searches a file for
a particular
pattern of
characters, and displays all lines that contain that pattern.
Command: $ cat Branch.txt | grep Computer
Output :

head :
Command: $ cat Branch.txt | head -4
Output :

tail :
Command : $ cat Branch.txt |tail -4
Output :

wc :
Command :$ cat Branch.txt | wc
Output :

tee:
Use cat, grep, tee and wc command to read the particular entry from user and store in a file and print line
count. 
 
$ cat result.txt | grep "Rajat Dua" | tee file2.txt | wc -l

This command select Rajat Dua and store them in file2.txt and print total number of lines matching Rajat Dua 
Output : 

$ Cat file2.txt|sort|uniq

The output shows that the elements are organized and arranged alphabetically. At the same time, all the words that
were duplicated are removed. The above command will only display the output, but we will use the below-cited
command to save them.

$ Cat file3.txt | grep h

The result shows that the fetched data is according to the search by the ‘h’ command. Moving towards the following
example. Here we want to fetch the items of the file having ‘s’ in it, but we have applied a condition of case
sensitivity. Both upper and lower case alphabets will be fetched.

$ cat file1.txt grep “a\+t”

$ ls –al | more

$ cat sample2.txt | head -7 | tail -5

cat Filename | pg
cat Filename | more

cat sample | grep -v a | sort - r

Conclusion:
As a result, we have performed Shell script by using pipe to concatenate general purpose linux
command.
Practical No.3

To search for a particular character string in a file, use the grep command. The basic syntax of
the grep command is:

$ grep string file

The grep filter searches a file for a particular pattern of characters, and displays all lines that
contain that pattern. The pattern that is searched in the file is referred to as the regular
expression (grep stands for global search for regular expression and print out). 
Syntax: 
 
grep [options] pattern [files]
 
Options Description
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line,
with each such part on a separate output line.

1. Case insensitive search : The -i option enables to search for a string case insensitively in the
given file. It matches the words like “UNIX”, “Unix”, “unix”. 
 
$grep -i "UNix" geekfile.txt

2. Displaying the count of number of matches : We can find the number of lines that matches
the given string/pattern 
 
$grep -c "unix" geekfile.txt

5. Displaying only the matched pattern : By default, grep displays the entire line which has
the matched string. We can make the grep to display only the matched string by using the -o
option. 
 
$ grep -o "unix" geekfile.txt

3. Display the file names that matches the pattern : We can just display the files that contains
the given string/pattern. 
 
$grep -l "unix" *

or

$grep -l "unix" f1.txt f2.txt f3.xt f4.txt

4. Checking for the whole words in a file : By default, grep matches the given string/pattern
even if it is found as a substring in a file. The -w option to grep makes it match only the whole
words. 
 
$ grep -w "unix" geekfile.txt

6. Show line number while displaying the output using grep -n : To show the line number of
file with the line matched. 
 
$ grep -n "unix" geekfile.txt
Note
https://www.geeksforgeeks.org/grep-command-in-unixlinux/

You might also like