Filter PDF (en)

  • Upload
    pedy

  • View
    218

  • Download
    0

Embed Size (px)

Citation preview

  • 8/9/2019 Filter PDF (en)

    1/25

    Redirection, pipeline and filter

    Standard input, output and error

    Redirection

    Pipeline

    Filter utilities

    Version 1.0 linuxslides.blogspot.com

  • 8/9/2019 Filter PDF (en)

    2/25

    Standard input, output, error

    processstandard input

    (stdin)standard output

    (stdout)

    process is a runninglinux command linestdin is an input fora process which istyped from keyboard

    stdout is an outputthat shows up onthe screen

    standard error

    (stderr)

    stderr is an errormessage that showsup on the screen

    Version 1.0 linuxslides.blogspot.com

  • 8/9/2019 Filter PDF (en)

    3/25

    Standar output (stdout)

    Let's say we executed a command:

    $ ls

    Maildir public_html data.txt

    $ whoami

    joni

    $ cat /etc/passwdroot:x:0:0:root:/root:/bin/bashdaemon:x:1:1:daemon:/usr/sbin:/bin/shbin:x:2:2:bin:/bin:/bin/shsys:x:3:3:sys:/dev:/bin/sh

    ...

    process

    stdout

    proses

    stdout

    process

    stdout

    Version 1.0 linuxslides.blogspot.com

  • 8/9/2019 Filter PDF (en)

    4/25

    Standar error (stderr)

    Let's say we executed another command:

    $ hello

    bash: hello: command not found

    $ pure-ftpd

    The program 'pure-ftpd' is currently notinstalled. You can install it by typing:

    sudo apt-get install pure-ftpdbash: pure-ftpd: command not found

    process

    stderr

    process

    stderr

    Version 1.0 linuxslides.blogspot.com

  • 8/9/2019 Filter PDF (en)

    5/25

    Standar input (stdin)

    Let's say we executed another command:

    $ cat

    Hallo world...

    process

    stdin (input from keyboard)

    Hallo world... stdout (output shows up on the screen)

    Ctrl+D EOF (end of file)

    Version 1.0 linuxslides.blogspot.com

  • 8/9/2019 Filter PDF (en)

    6/25

    Redirection

    normal redirection symbol

    from keyboard from file or 1>

    to screen to file 2>

    stdin

    stdout

    stderr

    Version 1.0 linuxslides.blogspot.com

  • 8/9/2019 Filter PDF (en)

    7/25

    Redirection stdin

    Without redirection:$ cat

    Hallo world...

    process

    stdin (input from keyboard)

    Hallo world... stdout (output shows up on the screen)

    With redirection stdin (input from file, not from keyboard):

    $ cat < /etc/passwd

    process

    root:x:0:0:root:/root:/bin/bashdaemon:x:1:1:daemon:/usr/sbin:/bin/shbin:x:2:2:bin:/bin:/bin/shsys:x:3:3:sys:/dev:/bin/sh...

    stdout

    redirection stdin

    The command above is the same with: $ cat /etc/passwdVersion 1.0 linuxslides.blogspot.com

  • 8/9/2019 Filter PDF (en)

    8/25

    Redirection stdout

    $ cat < /etc/passwd

    process

    root:x:0:0:root:/root:/bin/bashdaemon:x:1:1:daemon:/usr/sbin:/bin/sh

    bin:x:2:2:bin:/bin:/bin/sh stdout

    redirection stdin

    Redirection stdin (input from file, not from keyboard):

    $ cat > ~/hallo.txt

    process

    Hallo world...How're you? Bye.. :)

    stdin

    redirection stdout

    Redirection stdout (output to file, not to screen):

    Ctrl+D EOF (end of file)

    To display the content of file, type: $ cat halo.txt

    QUIZ

    Try following command:$ cat >> ~/hallo.txt(type some lines and press Ctrl+D)Display the content. What happen?

    Version 1.0 linuxslides.blogspot.com

  • 8/9/2019 Filter PDF (en)

    9/25

    Redirection stdout usage

    Redirection stdout generally used for capturing anddocumenting command's results:

    $ ls -l > result-ls.txt

    $ cat result-ls.txt

    $ ifconfig > ip-address.txt

    $ cat ip-address.txt

    QUIZIs the command below valid? What it uses for?$ cat < hallo.txt > hallo2.txt

    Version 1.0 linuxslides.blogspot.com

  • 8/9/2019 Filter PDF (en)

    10/25

    Homework

    Using redirection technique, assemble commands that willcreate a file which is recursively increasing its own sizeinfinitely. It won't stop until terminated by user, or the hard disk

    is full.

    Version 1.0 linuxslides.blogspot.com

  • 8/9/2019 Filter PDF (en)

    11/25

    Redirection stderr

    Without redirection stderr:

    $ hallo

    bash: hallo: command not found

    process

    stderr

    $ hallo 2> error.txt

    With redirection stderr:

    redirection stderr

    $ cat error.txt

    bash: hallo: command not found

    Notice that the error message didn't show up on the screen,but redirected and saved in file named error.txt. To see:

    Version 1.0 linuxslides.blogspot.com

  • 8/9/2019 Filter PDF (en)

    12/25

    Redirection stdout and stderr

    $ find / -name xyz > result.txt 2>&1

    The meaning of command above is:

    Search results will be redirected in file named result.txt If any error messages show up, it will also redirected to the same

    file result.txt

    Version 1.0 linuxslides.blogspot.com

  • 8/9/2019 Filter PDF (en)

    13/25

    Pipeline

    Pipeline is a technique for flowing a command's output asan input of another command.

    process1 output1 = input2 process2 output2

    In command line:

    $ process1 | process2 | process3 | ...

    Pipeline example:

    $ cat /var/log/messages | more

    $ ls /etc | grep passwd

    $ dpkg -l | grep apache

    ...

    Command that able to accept an input andproduce an output at the same time called filter

    Version 1.0 linuxslides.blogspot.com

  • 8/9/2019 Filter PDF (en)

    14/25

    Filter utilities

    more

    less

    grep

    head

    tail

    cut

    paste

    split

    sort

    tr

    wc

    pr

    Version 1.0 linuxslides.blogspot.com

  • 8/9/2019 Filter PDF (en)

    15/25

    more

    more is a filter for paging through text one screenful at a time.For navigations use enter to scroll per line, or space to scrollper screen.

    $ cat /var/log/messages$ more /var/log/messages

    $ cat /var/log/messages | more

    Version 1.0 linuxslides.blogspot.com

  • 8/9/2019 Filter PDF (en)

    16/25

    less

    less is like more, but besides standard enter and spacenavigations, less is also have additional navigations like uparrow, down arrow, Page Up, and Page Down.

    $ cat /var/log/messages$ less /var/log/messages

    $ cat /var/log/messages | less

    Version 1.0 linuxslides.blogspot.com

  • 8/9/2019 Filter PDF (en)

    17/25

    grep

    grep searches lines containing a match to the given pattern. Forexample if we want to search all lines containing root:

    $ grep root /etc/passwd

    $ cat /etc/passwd | grep root

    To search all lines, NOT containing root:

    $ grep -v root /etc/passwd

    To count how many lines matched:

    $ grep -c root /etc/passwd

    Search with case sensitive:

    $ grep -i ROOT /etc/passwd

    Version 1.0 linuxslides.blogspot.com

  • 8/9/2019 Filter PDF (en)

    18/25

    head and tail

    head: Print the first 10 lines of each FILE to standard output:$ head -5 /etc/passwd

    $ cat /etc/passwd | head -2

    $ cat -n /etc/passwd | head -3

    tail: Print the last 10 lines of each FILE to standard output:

    $ tail -5 /etc/passwd

    $ cat /etc/passwd | head -2

    $ cat -n /etc/passwd | head -3

    QUIZUsing head, tail and pipeline,

    assemble commands forprinting only the third row.

    Version 1.0 linuxslides.blogspot.com

  • 8/9/2019 Filter PDF (en)

    19/25

    cut

    cut is an utility to print selected column with a specific delimiter.For example /etc/passwd filled with columns separated by : .How many columns are in /etc/passwd?

    $ cat /etc/passwd

    To print the first column:$ cut -d: -f1 /etc/passwd

    To print the first and the fifth column:

    $ cut -d: -f1,5 /etc/passwd

    To print the first to the fifth column:

    $ cut -d: -f1-5 /etc/passwd

    Version 1.0 linuxslides.blogspot.com

  • 8/9/2019 Filter PDF (en)

    20/25

    paste

    paste is an utility for merging columns and print it to standardoutput.

    Take the first column and save it:

    $ cut -d: -f1 /etc/passwd > col1.txt

    Take the second column and save it:

    $ cut -d: -f2 /etc/passwd > col2.txt

    Merge the two files above with paste:$ paste col1.txt col2.txt

    Merge with delimiter :

    $ paste -d: col1.txt col2.txt Version 1.0 linuxslides.blogspot.com

  • 8/9/2019 Filter PDF (en)

    21/25

    splitsplit is an utility for splitting a file into pieces with certain size.

    Split /etc/passwd into files each contain 10 rows:

    $ cat -n /etc/passwd | split -10

    $ split -10 /etc/passwd

    The results are files with prefix xa: xaa, xab, xac:

    $ cat xaa

    If we want different prefix like xxx:

    $ split -10 /etc/passwd xxx

    To merge again back to original:

    $ cat xxx* > passwd.txt

    Version 1.0 linuxslides.blogspot.com

  • 8/9/2019 Filter PDF (en)

    22/25

    sortsort is a utility for sorting text.

    Take field user name from /etc/passwd:

    $ cat /etc/passwd | cut -d: -f1

    Then sort ascending:$ cat /etc/passwd | cut -d: -f1 | sort

    Then sort descending:

    $ cat /etc/passwd | cut -d: -f1 | sort -r

    To sort numerically (third field: uid):

    $ cat /etc/passwd | cut -d: -f3 | sort -n

    Version 1.0 linuxslides.blogspot.com

  • 8/9/2019 Filter PDF (en)

    23/25

    trtr (translate) is an utility for translate or delete characters.

    Translate character a into o:

    $ tr a o

    Hallo

    HolloCtrl+D

    Translate file's contents into capital:

    $ cat /etc/passwd | tr a-z A-Z

    Version 1.0 linuxslides.blogspot.com

  • 8/9/2019 Filter PDF (en)

    24/25

    wcwc (word count) is an utility for printing newline, word, and byte

    counts for each file.

    To count lines from /etc/passwd:

    $ cat /etc/passwd | wc -l

    To count words from /etc/passwd:

    $ cat /etc/passwd | wc -w

    To count characters from /etc/passwd:

    $ cat /etc/passwd | wc -c

    To count files in your home directory:

    $ ls ~ | wc -l

    Version 1.0 linuxslides.blogspot.com

  • 8/9/2019 Filter PDF (en)

    25/25

    prpr is a utility for converting text files for printing.

    To format /etc/passwd:

    $ cat /etc/passwd | pr

    To format /etc/passwd with header:$ cat /etc/passwd | pr -h File /etc/passwd

    QUIZ: Try to format /var/log/messagesfor printing and

    redirect it to~/messages.txt

    Version 1.0 linuxslides.blogspot.com