You are on page 1of 16

Filters

Head

 The head command in unix or linux system is used to print the


first N lines from the file to the terminal.
 By default, the head command prints the first 10 lines from a file.
head example.txt
 Use the -n option to print the first n lines from a file. The following
example prints the first 2 lines from the file:
 head –n 2 example.txt
Tail

 The tail command in unix or linux system is used to print the last
N lines from the file to the terminal.
 By default, the head command prints the last10 lines from a file.
tail example.txt
 Use the -n option to print the last n lines from a file. The following
example prints the last 2 lines from the file:
tail –n 2 example.txt
Cut
 Cut command is used to select sections of text from each line of
files.
 cut command is used to select fields or columns from a line by
specifying a delimiter or by specifying the range or characters.
 The cut command can be used to print characters in a line by
specifying the position of the characters. To print the characters
in a line, use the -c option in cut command
cut –c 4 file.txt
 The above cut command prints the fourth character in each line
of the file.
cut –c 4,6 file.txt
 The above cut command prints the fourth and sixth character in
each line of the file.
cut -c4-7 file.txt
 The above cut command prints the characters from fourth
position to the seventh position in each line.
Cut
cut –c -6 file.txt
 This is used to print the first six characters in a line, from the start
position.
 To print the characters from tenth position to the end, specify
only the start position and omit the end position.
cut –c 10- file.txt
 cut command is used to extract the fields in a file using a
delimiter. The -d option in cut command can be used to specify
the delimiter and -f option is used to specify the field position.
cut –d ' ' –f 2 file.txt
 print a range of fields by specifying the start and end position.
cut –d ' ' –f 1-3 file.txt
Paste
 The paste command merges the lines from multiple files. The
paste command sequentially writes the corresponding lines from
each file separated by a TAB delimiter on the unix terminal.
paste file1 file2
 the paste command merges the files in parallel.
 change the delimiter to any other character by using the -d option
paste –d "|" file1 file2
Sort command

 Sort command is used to order the elements or text.


 The default sort command uses alphabetical order (ASCII order)
to sort the file. It treats each line as a string and then sorts the
lines.
sort order.txt
 Sorting based on the field positions.
 The sort command uses the space or tab as the default delimiter.
To sort based on the data in the second field use
sort –k 2 order.txt
 Numeric sorting: sort –n 3 numfile
Sort

 sort –t ' ' –k 2 file.txt


 This commands takes blankspace as delimiter and sorts on basis
of field 2.
Uniq command

 Uniq command is used to suppress the duplicate lines from a file.


 The default behavior of the uniq command is to suppress the
duplicate line. Note that, you have to pass sorted input to the uniq,
as it compares only successive lines.
uniq example.txt or sort example.txt | uniq
 Display only duplicate lines using –d option .
uniq -d example.txt
Tr command

 Tr command is used for translating characters.


 It takes input only from standard input and not from filename as
argument.
 tr a z < file1
 In this command a is replaced with z.
 the following would replace all instances of the letters e, f and g in
a file named file5 with the letters x, y and z, respectively, and write
the output to file6:
cat file5 | tr '[efg]' '[xyz]' > file6
 Examples:
cat file5 | tr '[e-g]' '[x-z]' > file6
cat file7 | tr '[A-Z]' '[a-z]' > file8
tr -s ' ' < file9 > file10 // Used for squeezing whitespaces
Grep command options
 Case insensitive search
The -i option enables to search for a string case insensitively in the
give file. It matches the words like "UNIX", "Unix", "unix".
grep -i "UNix" file.txt
 Displaying the line numbers.
We can make the grep command to display the position of the line
which contains the matched string in a file using the -n option
grep -n "string" file.txt
 Displaying the count of number of matches.
We can find the number of lines that matches the given string/pattern
grep -c "string" file.txt
 Display the file names that matches the pattern.
We can just display the files that contains the given string/pattern.
grep -l "string" *
 Searching for a string in multiple files.
grep "string" file1 file2
Grep regular expression

^ match expression at the start of a line, as in ^A.


$ match expression at the end of a line, as in A$.
[] match any one of the enclosed characters, as in [aeiou]. Use
Hyphen "-" for a range, as in [0-9].
[^ ] match any one character except those enclosed in [ ], as in
[^0-9].
. match a single character of any value, except end of line.
*= match zero or more of the preceding character or
expression.
Grep regular expression

grep '^smug' files : Searches 'smug' at the start of a line


grep 'smug$' files Searches 'smug' at the end of a line
grep '^smug$' files Searches lines containing only 'smug'
grep '[Ss]mug' files Searches search for 'Smug' or 'smug'
grep 'B[iI][gG]' files search for BIG, Big, BIg or BiG
grep '^$' files search for blank lines
grep '[0-9][0-9]' file search for pairs of numeric digits
Grep and Extended Regular Expressions

 Grep can be used with more extensive regular expression


language by using the "-E" flag or by calling the "egrep" command
instead of grep.
 Extended regular expressions include all of the basic meta-
characters, along with additional meta-characters to express more
complex matches.
 The following will find either "GPL" or "General Public License" in
the text:
grep -E "(GPL|General Public License)" f1
 To match a character zero or one times, you can use the "?"
character.
The following matches "copyright" and "right" by putting "copy" in an
optional group:
grep -E "(copy)?right“ f1
Grep and Extended Regular Expressions

 The "+" character matches an expression one or more times. This


is almost like the "*" meta-character, but with the "+" character, the
expression must match at least once.
grep –E a+ file1
Types of shells
 sh
The Bourne shell, called "sh," is one of the original shells, developed
for Unix computers by Stephen Bourne at AT&T's Bell Labs in 1977.
It offers features such as input and output redirection, shell scripting
with string and integer variables, and condition testing and looping.
 bash
"bash" -- the "Bourne-again Shell," based on sh -- has become the
new default standard. One attractive feature of bash is its ability to
run sh shell scripts unchanged.
 csh
Using C syntax as a model, Bill Joy at Berkeley University developed
the "C-shell," csh, in 1978. Ken Greer, took csh concepts a step
forward with a new shell, tcsh, which Linux systems now offer. Tcsh
added command completion, in which the shell makes educated
"guesses" as you type, based on your system's directory structure
and files.
 ksh
David Korn developed the Korn shell, or ksh. Ksh is compatible with
sh and bash. Ksh improves on the Bourne shell by adding floating-

You might also like