ExpNo5 Updated
ExpNo5 Updated
AIM
Demonstrate the usage of pipes and filters using Linux commands - wc, cut, paste, comm, uniq, more,
less, cmp, diff, sort, sleep, grep.
Commands
1 ) wc - print newline count, word count and byte count for each file along with the file name.wc stands
for word count is a command in Unix and Unix-like operating systems. It is mainly used for counting
purpose.
-c, --bytes
print the byte counts
-m, --chars
print the character counts
-l, --lines
print the newline counts
-w, --words
print the word counts
Passing only one file name in the argument and count number of lines, words and characters.also
allows to pass more than one file name in the argument.
2) grep
t is a pattern or expression matching command. It searches for a pattern or regular expression that
matches in files or directories and then prints found matches. Syntax - $grep[options] "pattern to be
matched" filename
Example:
Input : $grep 'hello' ist_file.txt
Output : searches hello in the ist_file.txt and outputs/returns the lines containing 'hello'.
Example
user@chedept-u:~/SAexps$ grep -l 'cpu' *
file.txt
3) sort Command: It is a data manipulation command that sorts or merges lines in a file by specified
fields. In other words it sorts lines of text alphabetically or numerically, default sorting is
alphabetical.
Syntax:
$sort[options] filename
4) more - It is used to customize the displaying contents of file. It displays the text file contents on the
terminal with paging controls. Following key controls are used:
To display next line, press the enter key
To bring up next screen, press spacebar
To move to the next file, press n
To quit, press q.
5) Paste - Paste command is one of the useful commands in Unix or Linux operating system. It is used
to join files horizontally (parallel merging) by outputting lines consisting of lines from each file
specified, separated by tab as delimiter, to the standard output.
Syntax:
paste [OPTION]... [FILES]...
Let us consider three files having name state, capital and number. state and capital file contains 3
names of the Indian states and capitals respectively. number file contains 3 numbers.
Without any option paste merges the files in parallel. The paste command writes corresponding lines
from the files with tab as a deliminator on the terminal.
6) uniq- uniq is the tool that helps to detect the adjacent duplicate lines and also deletes the duplicate
lines.uniq filters out the adjacent matching lines from the input file(that is required as an argument) and
writes the filtered data to the output file.
Syntax :
//...syntax of uniq...//
$uniq [OPTION] [INPUT[OUTPUT]]
INPUT refers to the input file in which repeated lines need to be filtered out and if INPUT isn’t
specified then uniq reads from the standard input.OUTPUT refers to the output file in which you can
store the filtered output generated by uniq command and as in the case of INPUT if OUTPUT isn’t
specified then uniq writes to the standard output.
7) less
Less command is a Linux utility that can be used to read the contents of a text file one page (one
screen) at a time. It has faster access because if a file is large, it doesn’t access the complete file, but
accesses it page by page.
For example, if it’s a large file and you are reading it using any text editor, then the complete file will
be loaded to the main memory. The less command doesn’t load the entire file but loads it part by part
which makes it faster.
8) sleep
The sleep command in Linux is used to introduce a delay for a specified amount of time, typically in
seconds. It's often used in scripts to pause execution for a certain duration.
Here's the basic syntax of the sleep command
sleep [Time]
where TIME is the duration in seconds for which the command should pause.
Eg: $sleep 5
This will pause the execution of the script or command for 5 seconds.
9) cut
The cut command in Linux is used to extract sections from each line of a file or from input passed
through standard input (stdin). It can be useful when you need to select specific columns, characters, or
fields from text data.
Syntax:
cut [options] [file]
Common Options:
a. -b (bytes)
Selects specific bytes from the line.
eg:cut -b 1-5 file.txt - This selects bytes 1 through 5 from each line in file.txt.
b. -c (characters)
Selects specific characters from the line.
Eg:cut -c 1-3 file.txt - This selects characters 1 through 3 from each line in file.txt.
10)comm
The comm command in Linux is used to compare two sorted files line by line. It displays the
differences between the two files in three columns: lines unique to the first file, lines unique to the
second file, and lines common to both files.
Syntax
comm [OPTION]... FILE1 FILE2
Requirements:
• The input files must be sorted before using comm. If the files are not sorted, you can use the
sort command beforehand:
Output Format:
The comm command compares lines from two files and outputs them in three columns:
Output:
• If the files are identical, cmp produces no output but returns an exit status of 0.
• If the files are different, it outputs the byte and line number of the first difference and returns an
exit status of 1.
12) diff
The diff command in Linux is used to compare two files line by line and displays the differences
between them. Unlike cmp, which works at the byte level, diff shows the differences between text
files in a more readable format, typically indicating what changes need to be made to one file to make it
identical to the other.
Syntax:
diff [OPTION]... FILE1 FILE2
Output:
The default output format of diff shows changes in a symbolic way:
• Lines prefixed with < are present in FILE1 but not in FILE2.
• Lines prefixed with > are present in FILE2 but not in FILE1.
RESULT
Sucessfully executed the usage of pipes and filters using Linux commands