Skip to content
Snippets Groups Projects
bash_manip-5-grep2.md 1.97 KiB
Newer Older
# GREP (part2)

## setup

??? quote "First click and follow the instructions below only if you start the course at this stage! Otherwise skip this step!"
    {%
    include-markdown "pages/bash_manip/bash_manip-0-setup.md"
    %}


## Searching patterns (grep)

!!! question "Select all line related of the year 2001 in `nat2021.csv` file"

??? example "Click to show the solution"  
    ```bash
    grep ";2021;" nat2021.csv
    ```

!!! question "How many names have been provided in 2021?"

??? example "Click to show the solution"  
    ```bash
    grep ";2021;" nat2021.csv | wc -l
    # result: 13501
    ```

!!! question "Is there more diversity in male or female names in 2021"?

??? example "Click to show the solution"  
    ```bash
    # female
    grep ";2021;" nat2021.csv | grep "^2" | wc -l
    # result: 7112
    # male
    grep ";2021;" nat2021.csv | grep "^1" | wc -l
    # result: 6389
    ```

!!! question "How many person are called PARIS in 2021"?

??? example "Click to show the solution"  
    ```bash
    # female
    grep "PARIS;2021;" nat2021.csv
    # result 16 (5 male and 11 female)
    ```

The rare name ([see here for documentation](https://www.insee.fr/fr/statistiques/2540004?sommaire=4767262#documentation)) are set as `_PRENOMS_RARES`.

!!! question "Could you find all rare name ? Do you see any pattern?"

??? example "Click to show the solution"  
    ```bash
    grep ";_PRENOMS_RARES;" nat2021.csv
    ```
    People tends to provide more and more rare names.


!!! question "What year was the most prolific fot the name ZINEDINE?"

??? example "Click to show the solution"  
    ```bash
    # command
    grep ";ZINEDINE;" nat2021.csv | sort -n -t ';' -k4
    # result: 1998
    ```


You can redirect a result and store it in a file thanks to the `>` redirection:  
`command > filename`

!!! question "Select all the names from 2005 in a dedicated file?"

??? example "Click to show the solution"  
    ```bash
    # command
    grep ";2005;" nat2021.csv 
    ```