grep Flashcards

1
Q

What does the -i option do in grep?

A

The -i option makes the search case-insensitive. For example, ‘grep -i error file.txt’ matches ‘error’, ‘Error’, and ‘ERROR’.

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

What does the -v option do in grep?

A

The -v option inverts the match, displaying lines that do NOT match the pattern. For example, ‘grep -v DEBUG file.txt’ excludes lines containing ‘DEBUG’.

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

What does the -c option do in grep?

A

The -c option counts the number of matching lines instead of displaying them. For example, ‘grep -c keyword file.txt’ outputs the number of lines containing ‘keyword’.

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

What does the -n option do in grep?

A

The -n option displays line numbers alongside matching lines. For example, ‘grep -n "pattern" file.txt’ shows matching lines with their line numbers.

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

What does the -l option do in grep?

A

The -l option lists only the names of files containing matches. For example, ‘grep -l "error" *.log’ lists log files containing ‘error’.

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

What does the -L option do in grep?

A

The -L option lists only the names of files that do NOT contain any matches. For example, ‘grep -L "keyword" *.txt’ lists files that do not contain ‘keyword’.

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

What does the -r option do in grep?

A

The -r option enables recursive search through directories. For example, ‘grep -r "keyword" /project’ searches all files in the /project directory and subdirectories.

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

What does the -w option do in grep?

A

The -w option matches whole words only. For example, ‘grep -w "cat" file.txt’ matches ‘cat’ as a whole word but not ‘category’.

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

What does the -A option do in grep?

A

The -A option displays ‘n’ lines after each match. For example, ‘grep -A 3 "error" file.log’ shows the matching lines and 3 lines after each match.

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

What does the -B option do in grep?

A

The -B option displays ‘n’ lines before each match. For example, ‘grep -B 2 "404" file.log’ shows 2 lines before each matching line.

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

What does the -C option do in grep?

A

The -C option displays ‘n’ lines of context before and after each match. For example, ‘grep -C 1 "failed" file.log’ shows 1 line before and 1 line after each match.

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

What does the -e option do in grep?

A

The -e option allows specifying multiple patterns. For example, ‘grep -e "pattern1" -e "pattern2" file.txt’ matches lines containing ‘pattern1’ or ‘pattern2’.

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

What does the -f option do in grep?

A

The -f option allows reading patterns from a file. For example, ‘grep -f patterns.txt file1.txt’ matches lines containing patterns listed in patterns.txt.

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

What does the –color option do in grep?

A

The –color option highlights matching text in the output. For example, ‘grep –color "keyword" file.txt’ highlights ‘keyword’ in the results.

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

What does the -q option do in grep?

A

The -q option runs grep quietly, suppressing all output. For example, ‘grep -q "ERROR" file.txt && echo "Error found"’ outputs ‘Error found’ if ‘ERROR’ exists.

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

What does the grep command ‘grep -i "error" /var/log/syslog’ do?

A

It performs a case-insensitive search for the word ‘error’ in the /var/log/syslog file and displays all matching lines.

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

What does the grep command ‘grep -v "debug" /var/log/messages’ do?

A

It displays all lines from /var/log/messages that do not contain the word ‘debug’.

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

What does the grep command ‘grep -c "timeout" application.log’ do?

A

It counts the number of lines in application.log that contain the word ‘timeout’.

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

What does the grep command ‘grep -n "keyword" notes.txt’ do?

A

It searches for the word ‘keyword’ in notes.txt and displays the matching lines along with their line numbers.

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

What does the grep command ‘grep -l "failure" /var/log/*.log’ do?

A

It lists the names of log files in /var/log that contain the word ‘failure’.

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

What does the grep command ‘grep -L "success" results.txt’ do?

A

It lists the names of files that do not contain the word ‘success’ in results.txt.

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

What does the grep command ‘grep -r -i "critical" /etc’ do?

A

It recursively searches for the word ‘critical’ in all files under the /etc directory, case-insensitively.

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

What does the grep command ‘grep -w "error" /var/log/syslog’ do?

A

It searches for whole-word occurrences of ‘error’ in /var/log/syslog and excludes matches where ‘error’ is part of another word.

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

What does the grep command ‘grep -A 2 "failed" logs.txt’ do?

A

It searches for lines containing ‘failed’ in logs.txt and displays each matching line along with the 2 lines after it.

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

What does the grep command ‘grep -B 3 "denied" auth.log’ do?

A

It searches for lines containing ‘denied’ in auth.log and displays each matching line along with the 3 lines before it.

26
Q

What does the grep command ‘grep -C 3 "password" server.log’ do?

A

It searches for lines containing ‘password’ in server.log and displays each matching line along with 3 lines of context before and after the match.

27
Q

What does the grep command ‘grep -f patterns.txt -i /var/log/syslog’ do?

A

It reads patterns from patterns.txt and performs a case-insensitive search for those patterns in the /var/log/syslog file.

28
Q

What does the grep command ‘grep –color -n "login" access.log’ do?

A

It searches for the word ‘login’ in access.log, displays matching lines with their line numbers, and highlights the matches in color.

29
Q

What does the grep command ‘grep -q "error" application.log && echo "Error found"’ do?

A

It performs a quiet search for ‘error’ in application.log. If a match is found, it outputs ‘Error found’.

30
Q

What does the grep command ‘grep -r -l "connection lost" /var/log/’ do?

A

It recursively searches for ‘connection lost’ in all files under /var/log and lists the names of the files containing the match.

31
Q

What does the -d option do in the cut command?

A

The -d option specifies the delimiter used to separate fields. For example, ‘cut -d , -f 1 file.csv’ extracts the first field from a CSV file.

32
Q

What does the -f option do in the cut command?

A

The -f option selects which fields to display from a delimited file. For example, ‘cut -f 2 file.txt’ displays the second field from a tab-delimited file.

33
Q

What does the -c option do in the cut command?

A

The -c option selects specific characters from a line. For example, ‘cut -c 1-5 file.txt’ displays the first 5 characters of each line in file.txt.

34
Q

What does the -b option do in the cut command?

A

The -b option selects specific bytes from a line. For example, ‘cut -b 1-4 file.txt’ displays the first 4 bytes of each line in file.txt.

35
Q

What does the -d option do in the sort command?

A

The -d option sorts input data by considering only alphanumeric characters, ignoring non-alphanumeric characters. For example, ‘sort -d file.txt’ sorts the file with alphanumeric sorting.

36
Q

What does the -n option do in the sort command?

A

The -n option sorts the input numerically. For example, ‘sort -n numbers.txt’ sorts the lines of numbers.txt in ascending numerical order.

37
Q

What does the -r option do in the sort command?

A

The -r option reverses the order of the sort. For example, ‘sort -r file.txt’ sorts the contents of file.txt in descending order.

38
Q

What does the -u option do in the sort command?

A

The -u option removes duplicate lines from the sorted output. For example, ‘sort -u file.txt’ sorts file.txt and removes any duplicate lines.

39
Q

What does the -k option do in the sort command?

A

The -k option allows sorting by specific fields. For example, ‘sort -k 2 file.txt’ sorts file.txt based on the second field of each line.

40
Q

What does the -t option do in the sort command?

A

The -t option specifies the delimiter for sorting fields. For example, ‘sort -t , -k 2 file.csv’ sorts the file by the second field in CSV format.

41
Q

What does the –version option do in the sort command?

A

The –version option displays the version of the sort command. For example, ‘sort –version’ outputs the version information for the sort utility.

42
Q

What does the –check option do in the sort command?

A

The –check option checks if the input is already sorted without sorting it. For example, ‘sort –check file.txt’ returns an error if the file is not sorted.

43
Q

What does the -f option do in the sort command?

A

The -f option ignores case when sorting. For example, ‘sort -f file.txt’ sorts file.txt in a case-insensitive manner.

44
Q

What does the -b option do in the sort command?

A

The -b option ignores leading spaces when sorting. For example, ‘sort -b file.txt’ will sort the lines of file.txt while ignoring any leading whitespace.

45
Q

What does the -M option do in the sort command?

A

The -M option sorts by month names. For example, ‘sort -M months.txt’ sorts months.txt by the month names (e.g., Jan, Feb, Mar).

46
Q

What does the -z option do in the sort command?

A

The -z option sets the input and output to be delimited by null characters instead of newlines. For example, ‘sort -z file.txt’ treats nulls as delimiters in file.txt.

47
Q

cut -d , -f 1 employees.csv

A

This command extracts the first column from a comma-separated CSV file named employees.csv.

48
Q

cut -d : -f 2 password_file.txt

A

This command extracts the second field (usually the password) from a colon-separated file such as a password file.

49
Q

cut -f 1-3 data.txt

A

This command extracts the first three fields from a tab-delimited text file named data.txt.

50
Q

cut -c 1-5 log.txt

A

This command extracts the first five characters from each line in log.txt.

51
Q

cut -b 1-4 report.txt

A

This command extracts the first 4 bytes from each line of report.txt.

52
Q

sort -d names.txt

A

This command sorts the lines of names.txt alphabetically, considering only alphanumeric characters.

53
Q

sort -n numbers.txt

A

This command sorts the contents of numbers.txt numerically, from lowest to highest.

54
Q

sort -r reverse.txt

A

This command sorts the contents of reverse.txt in reverse (descending) order.

55
Q

sort -u list.txt

A

This command sorts list.txt and removes any duplicate lines.

56
Q

sort -k 2 people.txt

A

This command sorts people.txt based on the second field (e.g., last names).

57
Q

sort -t , -k 3 data.csv

A

This command sorts data.csv by the third field using a comma as the delimiter.

58
Q

sort -f names.txt

A

This command sorts names.txt in a case-insensitive manner.

59
Q

sort -b input.txt

A

This command sorts input.txt while ignoring leading spaces.

60
Q

sort -M months.txt

A

This command sorts the months.txt file containing month names in chronological order.

61
Q

sort -z file.txt

A

This command sorts file.txt where records are separated by null characters instead of newlines.