Grep & RegExp Flashcards

Learn all about grep command and regular expressions

1
Q

To find lines in test.txt that have word “seven”

So if you have lines
line 1: five seventeen two
line 2: five seven two

only “five seven two “ will be found

A

grep -w ‘seven’ test.txt

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

To find lines in test.txt where we have words that start with “seven”

A

grep ‘\

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

To find in file test.txt lines where we have words that ends with seven, like ‘seven’ not ‘seventeen’

A

grep ‘seven>’ test.txt

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

To find in test.txt file lines which start with seven. So

will be found: ‘seven five two’
but
won’t be found: ‘five seven two’

A

grep ‘^seven’ test.txt

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

To find in test.txt file lines which end with seven. So

five two seven - will be found
but
five seven two - won’t be found

A

grep ‘seven$’ test.txt

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

For file test.txt

  1. To show one line before and one row after the grep pattern in the file test.txt like
    #line UP
    twentyseven
    #line down
  2. To show one row before grep pattern
    #line UP
    twentyseven
  3. To show one row after grep pattern
    twentyseven
    #line down
A
  1. before and after: grep -C 1 twentyseven test.txt
  2. after match: grep -B 1 twentyseven test.txt
  3. before match: grep -A 1 twentyseven test.txt
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

To show lines where twenty1, twenty2, twenty3, twenty4 exist

for

twenty1
twenty3
twenty5
twenty7

A

grep “twenty[1-4]” test.txt

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

To exclude lines where twenty1, twenty2, twenty3, twenty4 exist

for

twenty1
twenty3
twenty5
twenty7

A

grep “twenty[^1-4]” test.txt

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

nameserver 127.0.0.1

For file test.txt

nameserver 8.8.8.8
nameserver 77.88.8.8
nameserver 8.8.4.4

show lines with ip and exlude lines which are comments - have # at the begining

A

grep -E ‘\b[0-9]{1,3}(.[0-9]{1,3}){3}\b’ /etc/resolv.conf | grep -v ‘#’

will show
nameserver 8.8.8.8
nameserver 77.88.8.8
nameserver 8.8.4.4

and wont show 
#nameserver 127.0.0.1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
For file test.txt
#nameserver 127.0.0.1
nameserver 8.8.8.8
nameserver 77.88.8.8
nameserver 8.8.4.4
to show only ip excluding line with a comment (line starts with #)
Will be shown
8.8.8.8
77.88.8.8
8.8.4.4

not
127.0.0.1

A

grep -v ‘#’ /etc/resolv.conf | grep ‘\b[0-9]{1,3}(.[0-9]{1,3}){3}\b’

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

Case insensitive search of grep_pattern using grep in file test.txt

A

grep -i grep_pattern file_pattern test.txt

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

A regular expression matches any single character

A

the sign “.”

e. g.
text. *text
text. text

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

Specify the color parameter in a grep-specific environment variable

A

$ export GREP_OPTIONS=’–color=auto’ GREP_COLOR=’100;8’

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

Searching in all files recursively using grep. When you want to search in all the files under the current directory and its sub directory

A

grep -r “ramesh” *

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

Invert match using grep (exclude pattern from the search)

A

grep -v “go” demo_text

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

To count that how many lines matches the given pattern/string

A

grep -c “pattern” filename

17
Q

Show only the matched string in demo_file that contains is line

like

is line is the 1st lower case line
is line
is is the last line

A

grep -o “is.*line” demo_file

18
Q

A regular expression matches any single character - that is optional and will be matched at most once

A

‘?’

19
Q

A regular expression matches any single character - that will be matched zero or more times.

A

‘*’

20
Q

A regular expression matches any single character - that item will be matched one or more times

A

’+’

21
Q

A regular expression matches any single character the preceding item is matched exactly n times

A

‘{n}’

22
Q

A regular expression matches any single character is matched n or more times

A

‘{n,}’

23
Q

A regular expression matches item is matched at most m times. This is a GNU extension

A

‘{,m}’

24
Q

A regular expression matches any single character is matched at least n times, but not more than m times

A

‘{n,m}’

25
Q

To show the line number of file with the line matched like

A

grep -n “go” demo_text

26
Q

With this option grep won’t show strings with that result but reports file names where this pattern was found.

E.g. for word Alice in current folder

A

grep -l ‘Alice’ ./*

27
Q

Сообщает имена тех файлов, где не встретился ОБРАЗЕЦ:

A

grep -L ‘Алиса’ example/*

28
Q

What is the difference in using

a*
a+
a?

A

a* -> 0 or more
a+ -> 1 or more
a? ->0 or 1

29
Q

What is the difference in using

a{5}
a{2,}
a{1,3}

A

a{5} ->exactly five,
a{2,} ->two or more
a{1,3} ->between one & three

30
Q

What does ab|cd mean

A

Match ab or cd

31
Q

What do mean

```
abc
\1
(?:abc)
(?=abc)
(?!abc)
~~~

A
(abc)	capture group
\1	        backreference to group #1
(?:abc)	non-capturing group
(?=abc)	positive lookahead
(?!abc)	negative lookahead
32
Q
\w
\d 
\s	 
\W 
\D 
\S
A
\w  word
\d   digit
\s    whitespace
\W  not word
\D   digit
\S   whitespace
33
Q

To find 4 letter words in

drink beer, it’s very nice!

so “beer”, “very”, “nice” are found

A

\b\w{4}\b