这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# learnings
# Learnings

Contains a collection of READMEs for the various areas of my learning. Some of the exercises are from various paid sources, so please support the author of those exercises if possible! 🍺

## Table of Contents

- [GNU `grep` and `ripgrep`](gnu-grep/)
66 changes: 66 additions & 0 deletions gnu-grep/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# GNU grep

Learnings from [GNU GREP and RIPGREP](https://learnbyexample.gumroad.com/l/gnugrep_ripgrep?layout=profile) by [Sundeep Agarwal](https://learnbyexample.gumroad.com/)'

> Ensure that the version of `grep` you're using is 3.7 and above (the default that comes with MacOS is 2.6):
>
> ```bash
> grep --version
>
> grep (GNU grep) 3.8
> Packaged by Homebrew
> Copyright (C) 2022 Free Software Foundation, Inc.
> License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
> This is free software: you are free to change and redistribute it.
> There is NO WARRANTY, to the extent permitted by law.
>
> Written by Mike Haertel and others; see
> <https://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.
> ```
>
> You can install this (as well as others using [Homebrew](gnu-essentials-mac.md))
## Exercises

- [Chapter 2](exercises/chapter_2/README.md)

## Simple commands

> Example file: [`programming_quotes.txt`](https://github.com/learnbyexample/learn_gnugrep_ripgrep/blob/master/example_files/freq_options/programming_quotes.txt)

| Criteria | `grep` Command | Manual Reference |
|----------------------------------------------------|:--------------------------------------------------------:|:-----------------------------------------------------------------------:|
| Find a word from a text file | `grep '[keyword]' [file]` | [Link](https://www.gnu.org/software/grep/manual/grep.html) |
| Regex pattern matching | `grep -F '[regex_pattern]' [file]` | [Link](https://www.gnu.org/software/grep/manual/grep.html#index-_002dF) |
| Case-insensitive searching | `grep -i '[keyword]' [file]` | [Link](https://www.gnu.org/software/grep/manual/grep.html#index-_002di) |
| Inverse matching (ignore lines) | `grep -v '[keyword]' [file]` | [Link](https://www.gnu.org/software/grep/manual/grep.html#index-_002dv) |
| Line number | `grep -n '[keyword]' [file]` | [Link](https://www.gnu.org/software/grep/manual/grep.html#index-_002dn) |
| Line count | `grep -c '[keyword]' [file]` | [Link](https://www.gnu.org/software/grep/manual/grep.html#index-_002dc) |
| Limit output lines | `grep -m[lines] '[keyword]' [file]` | [Link](https://www.gnu.org/software/grep/manual/grep.html#index-_002dm) |
| Search multiple strings | `grep -e '[first_keyword]' -e '[second_keyword]' [file]` | [Link](https://www.gnu.org/software/grep/manual/grep.html#index-_002de) |
| Search multiple strings (keywords from a file) | `grep -f [keyword_file] [file]` | [Link](https://www.gnu.org/software/grep/manual/grep.html#index-_002df) |
| List files matching the pattern | `grep -l '[keyword]' [file1] [file2]` | [Link](https://www.gnu.org/software/grep/manual/grep.html#index-_002dl) |
| List files NOT matching the pattern | `grep -L '[keyword]' [file1] [file2]` | [Link](https://www.gnu.org/software/grep/manual/grep.html#index-_002dL) |
| Suppress filename output (default for single file) | `grep -h '[keyword]' [file1] [file2]` | [Link](https://www.gnu.org/software/grep/manual/grep.html#index-_002dh) |
| Show filename output (default for multiple files) | `grep -H '[keyword]' [file]` | [Link](https://www.gnu.org/software/grep/manual/grep.html#index-_002dH) |
| Match whole word only | `grep -w '[keyword]' [file]` | [Link](https://www.gnu.org/software/grep/manual/grep.html#index-_002dw) |
| Output exact match | `grep -x '[keyword]' [file]` | [Link](https://www.gnu.org/software/grep/manual/grep.html#index-_002dx) |
| Common lines between two files | `grep -Fxf [file1] [file2]` | |
| Extract only matching pattern | `grep -o '[keyword]' [file]` | [Link](https://www.gnu.org/software/grep/manual/grep.html#index-_002do) |

## Basic Regular Expression (BRE) / Extended Regular Expression (ERE) Regular Expressions

> Example files: [`word_anchors.txt` and `words.txt`](https://github.com/learnbyexample/learn_gnugrep_ripgrep/tree/master/example_files/bre_ere)

Typical commands:

| Options | Description | Manual Reference |
|:-------:|------------------------------------------------------------------------------------------------------------------------------------------------------|:-----------------------------------------------------------------------:|
| `-G` | can be used to specify explicitly that BRE is needed | [Link](https://www.gnu.org/software/grep/manual/grep.html#index-_002dG) |
| `-E` | will enable Extended Regular Expression (ERE). In GNU `grep`, BRE and ERE only differ in how metacharacters are specified, no difference in features | [Link](https://www.gnu.org/software/grep/manual/grep.html#index-_002dE) |
| `-F` | will cause the search patterns to be treated literally | [Link](https://www.gnu.org/software/grep/manual/grep.html#index-_002dF) |
| `-P` | will enable Perl Compatible Regular Expression (PCRE) (if available) | [Link](https://www.gnu.org/software/grep/manual/grep.html#index-_002dP) |

### Line Anchors

- `^` — start of line
- `$` — end of line
127 changes: 127 additions & 0 deletions gnu-grep/exercises/chapter_2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
Format for the exercise is:

- use the [`dracula.txt`](dracula.txt) file as input
- output is shown for every question within code blocks
- answer is in code blocks

1. Display all lines containing `ablaze`:

```text
the room, his face all ablaze with excitement. He rushed up to me and
```

```bash
grep 'ablaze' dracula.txt
```

1. Display all lines containing `abandon` as a whole word:

```text
inheritors, being remote, would not be likely to abandon their just
```

```bash
grep -w 'abandon' dracula.txt
```

1. Display all lines that satisfies both of these conditions:

- `professor` matched irrespective of case
- either `quip` or `sleep` matched case sensitively

```text
equipment of a professor of the healing craft. When we were shown in,
its potency; and she fell into a deep sleep. When the Professor was
sleeping, and the Professor seemingly had not moved from his seat at her
to sleep, and something weaker when she woke from it. The Professor and
```

```bash
grep -i 'professor' dracula.txt | grep -e 'quip' -e 'sleep'
```

1. Display first three lines containing `Count`:

```text
town named by Count Dracula, is a fairly well-known place. I shall enter
must ask the Count all about them.)
Count Dracula had directed me to go to the Golden Krone Hotel, which I
```

```bash
grep -m3 'Count' dracula.txt
```

1. Display first six lines containing `Harker` but not either of `Journal` or `Letter`:

```text
said, "The Herr Englishman?" "Yes," I said, "Jonathan Harker." She
"I am Dracula; and I bid you welcome, Mr. Harker, to my house. Come in;
I shall be all alone, and my friend Harker Jonathan--nay, pardon me, I
Jonathan Harker will not be by my side to correct and aid me. He will be
"I write by desire of Mr. Jonathan Harker, who is himself not strong
junior partner of the important firm Hawkins & Harker; and so, as you
```

```bash
grep -v -e 'Journal' -e 'Letter' dracula.txt | grep -m6 'Harker'
```

1. Display lines containing `Zooelogical Gardens` along with line number prefix:

```text
5597: _Interview with the Keeper in the Zooelogical Gardens._
5601:the keeper of the section of the Zooelogical Gardens in which the wolf
8042:the Zooelogical Gardens a young one may have got loose, or one be bred
```

```bash
grep -n 'Zooelogical Gardens' dracula.txt
```

1. Find total count of whole word `the` (irrespective of case):

```text
8090
```

```bash
grep -iow 'the' dracula.txt | wc -l
```

1. The below code snippet tries to get number of empty lines, but apparently shows wrong result, why?

```bash
$ grep -cx '' dracula.txt
0
```

`dracula.txt` is not a Unix-style file format:

```bash
file dracula.txt

dracula.txt: ASCII text, with CRLF line terminators
```

This can be further reviewed using the `od` command ([manual](https://man7.org/linux/man-pages/man1/od.1.html)):

```bash
od -c dracula.txt | head -n5

0000000 T h e P r o j e c t G u t e
0000020 n b e r g E B o o k o f D
0000040 r a c u l a , b y B r a m
0000060 S t o k e r \r \n \r \n T h i s e
0000100 B o o k i s f o r t h e
```

This normally means there aren't any empty lines as there are (at least) a `\r` character in every other line other than the new line.

In order to resolve this, add the `$'\r'` to the `grep` command:

```bash
grep -cx $'\r' dracula.txt

2559
```
Loading