From ea316d7043489a34296d984cb81156d90d1f3da9 Mon Sep 17 00:00:00 2001
From: Jacques Dainat <jacques.dainat@ird.fr>
Date: Wed, 5 Mar 2025 22:14:14 +0100
Subject: [PATCH] last exercise

---
 docs/pages/bash_manip/bash_manip-7-sed.md | 62 ++++++++++++++++++++---
 1 file changed, 54 insertions(+), 8 deletions(-)

diff --git a/docs/pages/bash_manip/bash_manip-7-sed.md b/docs/pages/bash_manip/bash_manip-7-sed.md
index 7162b16..d3b7582 100644
--- a/docs/pages/bash_manip/bash_manip-7-sed.md
+++ b/docs/pages/bash_manip/bash_manip-7-sed.md
@@ -69,18 +69,24 @@ sed 'FLAG/<pattern>/<string>/FLAG'
 sed -n 'line p' file
 ```
 
-| Command | Description |
-|----------|----------|
+| Command | Description | Comment |
+|----------|----------| ----------|
 | `sed -n '8p' file` | Print line 8 |
 | `sed -n '8p; 16p' file` | Print lines 8 and 16 |
 | `sed -n '8p; 16p' file` | Print lines 8 and 16 |
 | `sed -n -e '8p' -e '16p' file` | Print lines 8 and 16 |
 | `sed -n '8,16 p' file` | Print lines from 8 to 16 |
 | `sed '8,$ p' file` | Print lines from line 8 to the end of the file |
-| `sed -n '1~8 p' file` | Print from line 1, every 8 lines |
+| `sed -n '1~8 p' file` | Print from line 1, every 8 lines | `~` not supported by BSD sed (MacOS)
 
 ### Exercice
 
+!!! question "Print the header and line 686 529 until the end."
+
+??? example "Click to show the solution"  
+    ```bash
+    sed -n '1p; 686529,$p' nat2021.csv
+    ```
 
 
 ## Line deletion 
@@ -91,18 +97,23 @@ sed -n 'line p' file
 sed 'line d' file
 ```
 
-| Command | Description |
-|----------|----------|
-| `sed '8d' file` | Delete line 8 |
+| Command | Description | Comment |
+|----------|----------| ----------|
+| `sed '8d' file` | Delete line 8 | 
 | `sed '8d; 16d' file` | Delete lines 8 and 16 |
 | `sed -e '8d' -e '16d' file` | Delete lines 8 and 16 |
 | `sed '8,16 d' file` | Delete lines from 8 to 16 |
 | `sed '8,$ d' file` | Delete lines from line 8 to the end of the file |
-| `sed '1~8d' file` | Delete from line 1, every 8 lines |
+| `sed '1~8d' file` | Delete from line 1, every 8 lines | `~` not supported by BSD sed (MacOS)
 
 ### Exercice
 
+!!! question "Delete everything from line 10 to 686 529."
 
+??? example "Click to show the solution"  
+    ```bash
+    sed '10,686529d' nat2021.csv
+    ```
 
 ## Use of Regular Expression 
 
@@ -115,11 +126,33 @@ sed 'RegEx' file
 | Command | Description |
 |----------|----------|
 | `sed '/^#/d' file` | Delete lines starting by # |
-| `sed -n '/\tmRNA\t/p' file` | Print lines matchin mRNA surrounded by tabulation |
+| `sed -n '/[0-9][0-9][0-9][0-9]/p' file` | Print lines matching any number with 4 digits |
+| `sed -E -n '/[0-9]{4}/p' file` | Print lines matching any number with 4 digits using extended regular expressions |
+
+??? Note "Summary of sed Regex Operators"
+    | Operator | Description |
+    |----------|----------|
+    . | Matches any character except newline | sed 's/a.b/c/g'
+    ^ | Matches the start of a line | sed '/^apple/d'
+    $ | Matches the end of a line | sed '/end$/d'
+    * | Matches 0 or more occurrences of the preceding character | sed 's/a*b/c/g'
+    [] | Matches any one character in the class | sed 's/[aeiou]/X/g'
+    [^] | Matches any character not in the class | sed 's/[^a-z]/X/g'
+    () | Groups characters (Extended Regex) | `sed -E ’s/(apple
+    ` | ` | OR operator (Extended Regex)
+    + | Matches 1 or more occurrences (Extended Regex) | sed -E 's/a+b/c/g'
+    ? | Matches 0 or 1 occurrence (Extended Regex) | sed -E 's/a?b/c/g'
+    {n,m} | Matches between n and m occurrences (Extended Regex) | sed -E 's/a{2,4}/c/g'
+    \ | Escapes special characters | sed 's/\./X/g'
 
 ### Exercice
 
+!!! question "Select all line that match PIERRE in the 2000s."
 
+??? example "Click to show the solution"  
+    ```bash
+    sed -E -n '/;PIERRE;2[0-9]{3}/p' nat2021.csv
+    ```
 
 ## Subsitution
 
@@ -139,7 +172,20 @@ sed 's/pattern/replacement/' file
 
 ### Exercice
 
+!!! question "Replace all numbers from last colum by XX"
+
+??? example "Click to show the solution"  
+    ```bash
+    sed -E 's/[0-9]+.?$/XX/' nat2021.csv
+    # /!\ s/[0-9]+$/XX/ does not work because an unprintable character exist at the end of line (\r). Using .? allow to match this unprintable character.
+    ```
+
+!!! question "Replace all numbers by X"
 
+??? example "Click to show the solution"  
+    ```bash
+    sed -E 's/[0-9]/X/g' nat2021.csv
+    ```
 
 ## Capturing
 
-- 
GitLab