BASIC CONCEPTS AND COMMANDS - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

BASIC CONCEPTS AND COMMANDS

Description:

chars specifies what characters you want to extract from each line of file. cut ... finds all the lines in the filename that don't contain the characters UNIX. ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 27
Provided by: Kami47
Category:

less

Transcript and Presenter's Notes

Title: BASIC CONCEPTS AND COMMANDS


1
BASIC CONCEPTS AND COMMANDS
  • Filters
  • cut
  • grep
  • sort

2
Filters
  • Filter refers to any program that can take input
    from standard input, perform some operations on
    that input, and write the results to standard
    output.
  • In other words, a filter is any program that can
    be used between two other programs in a pipeline.
  • Eg. ls wc -l wc is a filter

3
Filters
  • Are head, tail, and sort filters?
  • How about who, date, cd, pwd, echo, rm mv, cp?

4
cut
  • It is used to extract ( cut out ) various fields
    of data from a data file or the output a command.
  • General format
    cut -cchars
    filename
  • chars specifies what characters you want to
    extract from each line of file.

5
cut
  • Single character
  • cut -c5 filename
  • list of characters
  • cut -c1, 4, 15, 40
    filename
  • Range of characters
  • cut -c10-30 filename
  • from a character to the end of the line
  • cut -c5- filename

6
cut
  • Cut can also read its input from std output.
  • who cut -c1-8
  • who cut -c10-16
  • who cut -c1-8, 18-

7
cut
  • The -d and -f option are used with cut when you
    have data that is delimited by a particular
    character.
  • Cut -ddchar -ffields filename
  • dchar is the character that delimits each field
  • fields specifies the fields to be extracted from

8
cut
  • Examples
  • cut -d -f1 /etc/passwd
  • cut -d -f1, 6 /etc/passwd
  • If -d option is not supplied, the cut uses the
    tab character as the default field delimiter.
  • To know in advance if fields are delimited by
    blanks or tabs, type the command
  • sed -n 1 filename
  • If the fields are separated by a tab, then a
    greater sign (gt) will be displayed.

9
grep
  • grep allows you to search one or more files for
    particular character patterns.
  • grep pattern files
  • every line of each file that contains pattern is
    displayed at the terminal.

10
grep
  • grep shell myfile
  • grep John mylist
  • grep shell

11
grep
  • Enclosing your grep pattern inside a pair of
    single quotes to protect it is a good idea.
  • For example, to find all the lines containing
    asterisks inside the file stars, then typing
  • grep stars
  • does not work. Because shell substitutes the
    names of all files in your current directory.
  • But try grep stars

12
grep
  • grep can also take its input from std input if no
    file name is specified.
  • who grep ce144d04

13
Task 1
  • Using your regular expression knowledge, explain
    what each of the following grep command may
    display?
  • grep tT filename
  • grep A-Z filename
  • grep 0-9 filename
  • grep \.pic filename

14
Task2
  • Having a file with the following contents
  • big
  • bad
  • bug
  • bag
  • bigger
  • boogy
  • What does grep b.g filename display?

15
grep and options
  • grep -i the filename
  • ignores the case, display the, The, tHE, etc
  • grep -v UNIX filename
  • finds all the lines in the filename that dont
    contain the characters UNIX.
  • Grep -l history .c
  • displays list of file names that contain the
    pattern (not their actual lines).

16
grep and options
  • What do the following commands do?
  • grep -l history .c wc -l
  • grep history .c wc -l

17
grep and options
  • grep -n history filename.c
  • places the line number before the lines that
    has been matched to contain that pattern.

18
Sort
  • Sort takes each line of the specified input file
    and sorts it into ascending order based on their
    ASCII value (Asciibetical order).
  • Example sort names
  • Alice
  • Fred
  • Fred
  • Ralph

19
Sort Options
  • sort -u
  • -u option tells sort to eliminate duplicate lines
    from the output
  • sort -u names
  • Alice
  • Fred
  • Ralph

20
sort -r
  • Use the -r option to reverse the order of the
    sort
  • sort -r names
  • Ralph
  • Fred
  • Fred
  • Alice

21
sort -o
  • By default, sort writes the sorted data to
    standard output. To have it go into a file, you
    can use output redirection.
  • sort names gt sorted_names
  • Alternatively, you can use the -o option to
    specify the output file.
  • sort names -o sorted_names

22
sort -n
  • The -n option to sort specifies that the first
    field to be considered a number, and the data is
    to be sorted arithmetically.
  • Sort data sort -n data
  • 14 2
  • 2 3
  • 23 14
  • 3 23

23
sort n (skipping fields)
  • To sort your data file by the y value i.e., the
    second number on each line, use sort 1n to skip
    past the first number on the line. Compare
  • sort -n data sort 1n data
  • 2 11 14 6
  • 3 33 2 11
  • 14 27 23 27
  • 23 6 3 33
  • Note! 5n would mean to skip the first 5 fields.

24
sort -t
  • Generally sort assumes that the fields being
    skipped are delimited by space or tab characters.
    The -t option says otherwise.
  • The character that follows the -t is taken as the
    delimiter character. Example
  • sort /etc/passwd sorts the file by the first
    field (user name)
  • sort 2n -t /etc/passwd sorts the file by
    the third colon-delimited field (user id).

25
Filters
  • Can you now add some other commands to your list
    of filters?

26
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com