Download presentation
Presentation is loading. Please wait.
Published byLorena Austin Modified over 9 years ago
1
BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1
2
2
3
» 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
4
» 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
5
» 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
6
A=‘This is it’ echo $A | grep ‘This’ A=`grep ‘tborrelli’ /etc/passwd` if [[ -z $A ]] ; then echo “tborrelli Not found” else echo “tborrelli found” fi 6 © Sekolah Tinggi Teknik Surabaya
7
» -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
8
» -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
9
» 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
10
10
11
» 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
12
12 © Sekolah Tinggi Teknik Surabaya
13
tr '{}' '()' outputfile 13 © Sekolah Tinggi Teknik Surabaya
14
tr [:space:] '\t' 14 © Sekolah Tinggi Teknik Surabaya
15
tr -s [:space:] '\t' 15 © Sekolah Tinggi Teknik Surabaya
16
tr -d 't‘ tr -d [:digit:] 16 © Sekolah Tinggi Teknik Surabaya
17
tr -cd [:digit:] 17 © Sekolah Tinggi Teknik Surabaya
18
tr -s '\n' ' ' < file.txt 18 © Sekolah Tinggi Teknik Surabaya
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.