Week 4 Flashcards

1
Q

What is POSIX Standard?

A

standard which defines regular expression into two types 1. Basic Regular Expression (BRE) 2. Extended Regular Expression (ERE)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is regex?

A

Regex is a pattern template to filter text

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How is grep command used?

A
  1. grep ‘pattern’ filename
  2. command| grep ‘pattern’
  3. egrep ‘pattern’ filename
    grep -E ‘pattern’ filename
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what are special characters used in grep pattern?

A

. - any single char
* - zero or more of preceding char/expr

[] - Any of the enclosed character; hypen (-) indicates char range

^ Anchor for beginning of line or negation of enclosed characters

$ - Anchor for end of line

\ Escape special characters.

{n,m} - range of occurances of preceding pattern at least n and utmost m times

() - grouping of regular expressions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are some of the special character (ERE)?

A

{n,m} - Range of occurances of precing pattern at least n and utmost m times.
() grouping of regular expressions

+ one or more of preceding character/ expression

? Zero or one of preceding character / expression

logical OR over the patterns

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are character classes?

A

[[:print:}} - Printable
[[:alnum:}} - alpha numeric

[[:alpha:}} - Alphabetic

[[:lower:}}- Lower case

[[:upper:}} - Upper case

[[:digit:}} - Decimal digits

[[:blank:}} - Space/ Tab

[[:space:}} - Whitespace

[[:punct:}} - Punctuation

[[:xdigit:}} - Hexadecimal

[[:graph:}} - Non - Space

[[:cntrl:}} - Control Characters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are Backreferences?

A

There are 9 backreferences
\1 through \9

\n matches whatever was matched by nth earlier paranthesized sub expression

A line with two occurances of hello will be matched using
(hello). *\1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the BRE / ERE operator precedence?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

how to search for a string

A

grep <pattern> <filename></filename></pattern>

cat <filename> | grep <pattern></pattern></filename>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how to use ‘.’ in grep command?

A

cat names.txt | grep ‘S.n’ - implies pattern ‘S*n’ here ‘.’ means any character available.

Suppose we use cat names.txt | grep ‘.am$’ - this pattern will look for pattern “*am” at the end of the line.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to check for a string with ‘.’

A

cat ‘names.txt’| grep ‘.’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to use anchors in grep?

A

cat ‘names.txt’ | grep ‘^M’ - Lines which begin with M.

cat names.txt |grep ‘^e’ does not return string which start with capital E. we need to use
cat names.txt | grep -i ‘^e’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

how to use word boundaries ?

A

cat names.txt | grep ‘am\b’ - end of the word boundary

cat names.txt | grep ‘am$’ - end of the line boundary

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to use [] in grep?

A

[] are used to give options.

cat names.txt | grep ‘M[ME]’ - matches MM or ME

cat names.txt | grep ‘[aeiou][aeiou]’ - matches names which has 2 vowels side by side

cat names.txt| grep ‘B90[1-4]’ - matches B901 to B904

cat names.txt | grep ‘B90[^5-7] - matches everything except B905 till B907. Anchor inside square brackets act as negation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

how to check frequency of occurance?

A

cat names.txt | grep ‘M{2} - matched MM

‘M{1,2} - either one or twice

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to group patterns ?

A

cat names.txt | grep ‘{ma)’

’(ma).*\1’ - matches pattern ‘ma’ followed by any no of characters and then ends with ‘ma’

’{a.}{3}’ - matches presense of 3 a’s in a name followed by some other char.

17
Q

how to use egrep?

A

cat names.txt | egrep ‘M+’ - patterns containing M’

‘^M+’ - patterns which start with letter M

‘[ED][ME]’ - either ED or ME should occur.

18
Q

how to filter through files using egrep?

A

dpkg-query -w -f’${Section} ${Binary:Package}\n | egrep ‘^math’

lists all files which start with math

19
Q

How to use the character classes in practice?

A

cat chartypes.txt | grep ‘[[:alnum:]]’

20
Q

How to exclude a particular output using grep

A

cat chartypes.txt | grep -v ‘[[:cntrl:]]’

selects all lines which do not have a control character

21
Q

How to select all the non empty lines using egrep?

A

cat chartypes.txt | egrep -v ‘^$’

22
Q

How to select a pincode (which has exactly 6 digits using egrep command?

A

egrep ‘\b[[:digit:]]{6}\n patterns.txt

23
Q

how to select email addresses using egrep?

A

egrep ‘\b[[:alnum:]]+.[[:alnum:]]+\b’ patterns.txt

24
Q

What does cut command do?

A

It does horizontal trimming of sections.

cut -c 1-4 fields.txt

displays first 4 characters of each line of the file fields.txt

cat fields.txt | cut -d “ “ -f 1

displays text delimited by space and displays first column or field

25
Q

how is composition of cut operation used?

A

cat fields.txt | cut -d “;” -f 1 |cut -d “,” -f 1

26
Q
A