bash – text processing utilities erick, joan © sekolah tinggi teknik surabaya 1

18
Sistem Operasi BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1

Upload: lorena-austin

Post on 20-Jan-2016

216 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1

Sistem Operasi

BASH – Text Processing UtilitiesErick, Joan

© Sekolah Tinggi Teknik Surabaya

1

Page 2: BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1

grep© Sekolah Tinggi Teknik Surabaya

2

Page 3: BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1

grep» globally regular expression print» grep looks inside file(s) and returns any

line that contains the string or expression

» prints lines matching a pattern to STDOUT

3

© Sekolah Tinggi Teknik Surabaya

Page 4: BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1

grep Examples» grep Pattern» grep Pattern filename˃ Ex. grep ‘permission’ thisFile

» grep pattern file1 file2˃ Ex. grep ‘permission’ file1 file2˃ file1: ˃ file2:

» If grep cannot find a line in any of the specified files that contain the requested pattern, no output is produced.

4

© Sekolah Tinggi Teknik Surabaya

Page 5: BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1

grep Structure» The general format:˃grep [options] PATTERN [FILE…]

» Reading from files˃ grep re *

» Reading from STDIN˃ grep ‘pattern’

˃ cat file | grep ‘pattern’˃ ls –l | grep ‘Jan’

5

© Sekolah Tinggi Teknik Surabaya

Page 6: BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1

More ExamplesA=‘This is it’echo $A | grep ‘This’

A=`grep ‘tborrelli’ /etc/passwd`if [[ -z $A ]] ; thenecho “tborrelli Not found”

elseecho “tborrelli found”

fi

6

© Sekolah Tinggi Teknik Surabaya

Page 7: BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1

grep Options» -i: ignore case˃grep –i unix filename

» -n: list line numbers along with the matching line˃ [File:]lineNumber:theLine

» -v: invert match ˃grep –v ‘#’ /etc/hosts

+ Produces a list of all the lines in /etc/hosts that do not contain the # character. 7

© Sekolah Tinggi Teknik Surabaya

Page 8: BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1

grep Options» -l: only listing the filenames˃ grep -l delete projects/*

+ Pqops.c+ Pqops.h+ Scheduler.c

» -w: matches the whole word˃ grep –w wordonly FILE˃ grep –w north datafile

» -c: Print the number of lines where the pattern was found˃ grep –c ‘^no[a-z]*’ datafile

» -A [B] num: Print num of lines after [before] match lines˃ grep –A 1 ‘^south[a-z]’ datafile 8

© Sekolah Tinggi Teknik Surabaya

Page 9: BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1

The grep Family» grep˃ grep ‘^[0-9][0-9]*$’˃ Try grep ‘^[0-9]+$’˃ Try grep ‘^[0-9]\+$’

» egrep (grep –E)˃ Extended/Enhanced grep with more RE

metacharacters˃ egrep ‘^[0-9]+$’

» fgrep (grep –F)˃ Fixed/fast grep, treats all characters as

literals˃ fgrep ‘^[0-9]+$’ 9

© Sekolah Tinggi Teknik Surabaya

Page 10: BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1

tr© Sekolah Tinggi Teknik Surabaya

10

Page 11: BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1

tr» UNIX utility for translating, or deleting,

or squeezing repeated characters. It will read from STDIN and write to STDOUT.

» The syntax of tr command is:˃ tr [OPTION] SET1 [SET2]

11

© Sekolah Tinggi Teknik Surabaya

Page 12: BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1

Convert Lower Case to Upper Case

12

© Sekolah Tinggi Teknik Surabaya

Page 13: BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1

Translate Braces to Parenthesis

tr '{}' '()' < inputfile > outputfile

13

© Sekolah Tinggi Teknik Surabaya

Page 14: BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1

Translate White-space to Tabs

tr [:space:] '\t'

14

© Sekolah Tinggi Teknik Surabaya

Page 15: BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1

Squeeze Repetition

tr -s [:space:] '\t'

15

© Sekolah Tinggi Teknik Surabaya

Page 16: BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1

Delete Specified Characters

tr -d 't‘tr -d [:digit:]

16

© Sekolah Tinggi Teknik Surabaya

Page 17: BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1

Complement The Sets

tr -cd [:digit:]

17

© Sekolah Tinggi Teknik Surabaya

Page 18: BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1

Join All The Lines

tr -s '\n' ' ' < file.txt

18

© Sekolah Tinggi Teknik Surabaya