UNIX For BI-ClassBook-Lesson03
UNIX For BI-ClassBook-Lesson03
UNIX For BI-ClassBook-Lesson03
Lesson 03:
Capgemini Internal
July 31, 2020 Proprietary and Confidential -1-
Lesson Objectives
What is a Filter?
➢ The head command, by default, will display the first 10 lines of a file.
– Example 1: To display fist 10 lines from file employee:
$head employee
– Example 2: To display first 5 lines from file employee:
$head –5 employee
➢ Single command can be used to display lines from more than one file.
➢ The tail command is useful to display last few lines or characters of the file.
– Example 1: To display last ten lines from employee:
$tail employee
$tail –7 employee
– Example 3: To display lines from the 10th line till end of the file:
– Options :
• -c : selects columns specified by list
• -f : selects fields specified by list
• -d : field delimiter (default is tab)
– Example 2: To display characters from 1st to 4th and 31st to 35th from file
bookDetails.lst :
$paste <file1><file2><Enter>
– Options : -d (Field delimiter)
$sort <filename>
– Options are:
• -r : Reverse order
• -n : Numeric sort
• -f : Omit the difference between Upper and lower case alphabets
• -t : Specify delimiter
• -k : to specify fields as primary or secondary key
– Example:
➢ The uniq command fetches only one copy of redundant records and writes the
same to standard output.
– –u option: It selects only non-repeated lines.
– –d option: It selects only one copy of repeated line.
– -c option: It gives a count of occurrences.
➢ To find unique values, the file has to be sorted on that field.
– Example: To find unique values from file duplist.lst
$ uniq duplist.lst
Output file
find Command
– Example 2: To locate the file named myfile starting at the root directory in the
system
– Example: The following example will search for the string UNIX in the file books.lst.
The lines which match the pattern will be displayed.
➢ Options of grep:
– c : It displays count of lines which match the pattern.
– n : It displays lines with the number of the line in the text file which match the
pattern.
– v : It displays all lines which do not match pattern.
– i : It ignores case while matching pattern.
– -w : It forces grep to select only those lines containing matches that form whole
words
➢ Regular Expression:
Expression Description
^ (Caret) match expression at the start of a line, as in ^A.
$ (Question) match expression at the end of a line, as in A$.
\ (Back Slash) turn off the special meaning of the next character, as in \^.
match any one of the enclosed characters, as in [aeiou]. Use Hyphen "-" for a
[ ] (Brackets)
range, as in [0-9].
[^ ] match any one character except those enclosed in [ ], as in [^0-9].
. (Period) match a single character of any value, except end of line.
* (Asterisk) match zero or more of the preceding character or expression.
\{x,y\} match x to y occurrences of the preceding.
\{x\} match exactly x occurrences of the preceding.
\{x,\} match x or more occurrences of the preceding.
Example Description
grep “smile“ files search files for lines with ‘smile’
grep '^smile' files 'smile' at the start of a line
grep 'smile$' files 'smile' at the end of a line
grep '^smile$' files lines containing only 'smile'
grep '\^s' files lines starting with '^s', "\" escapes the ^
grep '[Ss]mile' files search for ‘Smile' or 'smile'
grep 'B[oO][bB]' files search for BOB, Bob, BOb or BoB
grep '^$' files search for blank lines
grep '[0-9][0-9]' file search for pairs of numeric digits
➢ The fgrep command is useful to search files for one or more patterns, which
cannot be combined together.
➢ It does not use regular expressions. Instead, it does direct string comparison to
find matching lines of text in the input.
➢ The egrep command works in a similar way. However, it uses extended regular
expression matching.
– Syntax: