Wednesday 24 November 2010

SED : Tutorial 2

Deleting Everything after first H
echo "Fawad 123 Hello How are you" | sed s/H.*//
Output: Fawad 123

Double Substitution
$ echo "Fawad is a friend of Nazir" | sed 's/Fawad/Midhat/g;s/Nazir/Fawad/g'
Output: Midhat is a friend of Fawad

Matching Exact Patterns using [ and ].
$ echo "Aaminah is a name" | sed s/[aA][aA]/BB/g
Output: BBminah is a name

REF: http://www.grymoire.com/Unix/Regular.html

Regular Expression Matches
[] The characters "[]"
[0] The character "0"
[0-9] Any number
[^0-9] Any character other than a number
[-0-9] Any number or a "-"
[0-9-] Any number or a "-"
[^-0-9] Any character except a number or a "-"
[]0-9] Any number or a "]"
[0-9]] Any number followed by a "]"
[0-9-z] Any number,
or any character between "9" and "z".
[0-9\-a\]] Any number, or
a "-", a "a", or a "]"

Matching a specific number of sets with \{ and \}
Regular Expression Matches
_
* Any line with an asterisk
\* Any line with an asterisk
\\ Any line with a backslash
^* Any line starting with an asterisk
^A* Any line
^A\* Any line starting with an "A*"
^AA* Any line if it starts with one "A"
^AA*B Any line with one or more "A"'s followed by a "B"
^A\{4,8\}B Any line starting with 4, 5, 6, 7 or 8 "A"'s
followed by a "B"
^A\{4,\}B Any line starting with 4 or more "A"'s
followed by a "B"
^A\{4\}B Any line starting with "AAAAB"
\{4,8\} Any line with "{4,8}"
A{4,8} Any line with "A{4,8}"

Matching words with \< and \>

Searching for a word isn't quite as simple as it at first appears. The string "the" will match the word "other". You can put spaces before and after the letters and use this regular expression: " the ". However, this does not match words at the beginning or end of the line. And it does not match the case where there is a punctuation mark after the word.
There is an easy solution. The characters "\<" and "\>" are similar to the "^" and "$" anchors, as they don't occupy a position of a character. They do "anchor" the expression between to only match if it is on a word boundary. The pattern to search for the word "the" would be "\<[tT]he\>". The character before the "t" must be either a new line character, or anything except a letter, number, or underscore. The character after the "e" must also be a character other than a number, letter, or underscore or it could be the end of line character.

Backreferences - Remembering patterns with \(, \) and \1

Another pattern that requires a special mechanism is searching for repeated words. The expression "[a-z][a-z]" will match any two lower case letters. If you wanted to search for lines that had two adjoining identical letters, the above pattern wouldn't help. You need a way of remembering what you found, and seeing if the same pattern occurred again. You can mark part of a pattern using "\(" and "\)". You can recall the remembered pattern with "\" followed by a single digit. Therefore, to search for two identical letters, use "\([a-z]\)\1". You can have 9 different remembered patterns. Each occurrence of "\(" starts a new pattern. The regular expression that would match a 5 letter palindrome, (e.g. "radar"), would be
\([a-z]\)\([a-z]\)[a-z]\2\1

Examples:
1. Remember a pattern
$ echo "Aaminah Aaminah is a name" | sed 's/\(A[a-z]*\) \1/\1/g'
Output: Aaminah is a name

2. Eliminate the repeating characters.
$ echo "mmmmy nnname iis ffawad." | sed 's/\([a-z]\)\{1,3\}\1/\1/g'
Output: my name is fawad.

3. Keep first word and delete the rest.

$ echo "mmmmy nnname iis ffawad." | sed 's/\([a-z]*\).*/\1/'
Output: mmmmy

3. Printing the Last name & replacing the first name with last.
$ echo "Mr. Fawad Nazir" | sed 's/\([Ff]awad\) \([Nn]azir\)/\2 \1/'
Output: Mr. Nazir Fawad
$ echo "Mr. Fawad Nazir" | sed 's/\([Ff]awad\) \([Nn]azir\)/\2/'
Output: Mr. Nazir

3. Subtituting exact match words:
$ echo "ad Fawad ad Nazad ad" | sed 's/ ad/ xx /g'
Output: ad Fawad xx Nazad xx
$ echo "ad Fawad ad Nazad ad" | sed 's/ ad / xx /g'
Output: ad Fawad xx Nazad ad
$ echo "ad Fawad ad Nazad ad" | sed 's/ad/ xx /g'
Output: xx Faw xx xx Naz xx xx
$ echo "ad Fawad ad Nazad ad" | sed 's// xx /g'
Output: ad Fawad ad Nazad ad
$ echo "ad Fawad ad Nazad ad" | sed 's/\/ xx /g'
Output: xx Fawad xx Nazad xx
$ echo "ad Fawad ad Nazad ad" | sed 's/\/xx/g'
Output: xx Fawad xx Nazad xx


END of Tutorial 2.

No comments:

Post a Comment

Azure OpenAI Architecture Patterns & Deployment Patterns

Sharing some useful links that will help customers architect Azure OpenAI solution using the best practices: (1) Azure OpenAI Landing Zone r...