bash – text processing utilities erick, joan © sekolah tinggi teknik surabaya 1
TRANSCRIPT
![Page 1: BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bf9a1a28abf838c92398/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bf9a1a28abf838c92398/html5/thumbnails/2.jpg)
grep© Sekolah Tinggi Teknik Surabaya
2
![Page 3: BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bf9a1a28abf838c92398/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bf9a1a28abf838c92398/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bf9a1a28abf838c92398/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bf9a1a28abf838c92398/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bf9a1a28abf838c92398/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bf9a1a28abf838c92398/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bf9a1a28abf838c92398/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bf9a1a28abf838c92398/html5/thumbnails/10.jpg)
tr© Sekolah Tinggi Teknik Surabaya
10
![Page 11: BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bf9a1a28abf838c92398/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bf9a1a28abf838c92398/html5/thumbnails/12.jpg)
Convert Lower Case to Upper Case
12
© Sekolah Tinggi Teknik Surabaya
![Page 13: BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bf9a1a28abf838c92398/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bf9a1a28abf838c92398/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bf9a1a28abf838c92398/html5/thumbnails/15.jpg)
Squeeze Repetition
tr -s [:space:] '\t'
15
© Sekolah Tinggi Teknik Surabaya
![Page 16: BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bf9a1a28abf838c92398/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bf9a1a28abf838c92398/html5/thumbnails/17.jpg)
Complement The Sets
tr -cd [:digit:]
17
© Sekolah Tinggi Teknik Surabaya
![Page 18: BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bf9a1a28abf838c92398/html5/thumbnails/18.jpg)
Join All The Lines
tr -s '\n' ' ' < file.txt
18
© Sekolah Tinggi Teknik Surabaya