String processing with regular expressions

String Processing with regular expression

* Regular Expression . Regular expression is a pattern matching engine. . It is used by different tools like vi, grep, sed, awk etc.

* Modifiers . It determines the no. of previous character.

Modifier Meaning

* – Matches zero or more of the previous char.

\+ – ,, one ,, ,, ,, ,, ,, ,,

\? – ,, zero or one of the previous char.

\{i\} – ,, exactly ‘i’ of the ,, ,,

\{i,\} – ,, ‘i’ or more ,, ,, ,,, ,,

\{i,j\} – ,, between ‘i’ & ‘j’ ,, ,,, ,,

* Anchors .

It determines the begining or end of a line or a word.

Anchors Meaning

^ – Line begins with

$ – Line ends ,,

\< – word begins with

/\ apply above command is test file

Grep (genela regular expression processor) .

It seraches for tjhe specified pattern in a file & displays all these lines that have at least one machting pattern.

syntax: grep [pattern] [option] [argument]

-v – do not display the lines that have matching pattern

-c – display only the total count of lines that name matching pa ttern

-n – proceede the lines having matching pattern with line no.

-l – Lists all the fuiles in the specified dir that have at leas r one matching pattern.

Examples

#grep bash /etc/passwd

#grep -v bash /etc/passwd

#grep -c bash /etc/passwd

#grep -n bash /etc/passwd

#grep -l bash /etc/passwd

Sed command ———-—-

. It is used to perform search & replace

expression

#sed ‘s/old text/new tet /g’ source_file>output file

#sed ‘s/boy/girl/g’ test >sed_text

#sed ‘1,20s/boy/girl/g’ test>outfile

#sed -e ‘s/boy/girl/g’ -e ‘s/cat/dog/g’ test>output file

-e for to perform mutiple search & replace operation in the same file script

examples

#vi sd s/boy/girl/g s/cat/dog/g

#sed -f