Skip to content
Open
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
78 changes: 78 additions & 0 deletions tutorials/learnshell.org/en/Regular Expressions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
Tutorial
--------
Regular Expressions (Regex) are powerful patterns used to search and manipulate text. In Shell, the most common tool for regex is `grep`. This tutorial covers basic syntax.

In general, `grep` has the following syntax:

grep [options] 'pattern' [file...]

### Some grep options

* `-E` - Extended regex syntax.
* `-i` - Ignore case distinctions.
* `-v` - Invert match (select non-matching lines).
* `-w` - Match whole words only.

### Basic Regex symbols

* `.` - Matches any single character.
* `[012]` - Matches `0`, `1`, or `2`.
* `[0-9]` - Matches any character from `0` to `9` inclusive (any digit).
* `[^0-9]` - Matches any character EXCEPT `0` to `9` (negation).
* `a|b` - Matches `a` or `b` (requires `grep -E`).
* `^` - Matches the start of a line.
* `$` - Matches the end of a line.
* `*` - Matches ZERO or more of the preceding element.
* `+` - Matches ONE or more of the preceding element (requires `grep -E`).
* `{n}` or `{n,m}` - Matches the preceding element exactly `n` times, or between `n` and `m` times (requires `grep -E`).

### Escaping and Extended Regex (`-E`)
In standard `grep`, meta-characters like `+`, `?`, `|`, `(`, `)`, `{`, `}` are treated as literal text. To use their special powers, you must escape them with a backslash (e.g., `\+`, `\|`). Using `grep -E` allows you to use these symbols directly without escaping. You only need to escape literal special characters (like a real period `\.`).

### Examples

# Specific word search
echo "Find the error here" | grep -w "error"

# Simplified IPv4 search (matching 1 to 3 digits separated by literal dots)
echo "Server IP: 192.168.1.10" | grep -E "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"

# Using options: filter out lines with "debug" (case-insensitive)
echo -e "Info\nDEBUG\nError" | grep -iv "debug"

# Simplified example: find 'success' or 'failure' followed by a message
echo -e "success: operation finished\nfailure: disk full\ninfo: ok" | grep -E "(success|failure): [a-z ]+"

Exercise
--------
You are given a server log snippet. Use `grep` to find all `ERROR` entries (even if the word is misspelled with multiple `R`s) that occurred exactly during the 18:xx hour. Make sure your regex only matches valid minutes.

Tutorial Code
-------------
#!/bin/bash
log_text="17:45 INFO Server started
18:05 ERRRRRROR Failed to connect to DB
18:22 INFO User login
18:59 EROR Timeout exception
18:99 ERRROR Invalid time log
19:01 ERROR Disk full"

# write your code here

Expected Output
---------------
18:05 ERRRRRROR Failed to connect to DB
18:59 EROR Timeout exception

Solution
--------
#!/bin/bash
log_text="17:45 INFO Server started
18:05 ERRRRRROR Failed to connect to DB
18:22 INFO User login
18:59 EROR Timeout exception
18:99 ERRROR Invalid time log
19:01 ERROR Disk full"

# write your code here
echo "$log_text" | grep -E "18:[0-5][0-9] ER+OR"
5 changes: 5 additions & 0 deletions tutorials/learnshell.org/en/Shell Functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ Like other programming languages, the shell may have functions. A function is a
command...
}

# alternative syntax is also possible
function_name() {
command...
}

Functions are called simply by writing their names. A function call is equivalent to a command. Parameters may be passed to a function, by specifying them after the function name. The first parameter is referred to in the function as $1, the second as $2 etc.

function function_B {
Expand Down
180 changes: 180 additions & 0 deletions tutorials/learnshell.org/en/Special Commands sed,awk,grep,sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
Tutorial
--------

Unix-like systems encourage a pipeline-based workflow: use small, specialized tools and connect them with `|` so the output of one command becomes the input of the next.

command1 | command2 | command3

This tutorial covers the most common text-processing tools used in shell pipelines.

### 1. `grep` — search and filter text

This utility was described in the previous chapter.

### 2. `sort` — order lines

`sort` arranges lines in a specific order.

Useful options:

* `-n` — Numeric sort.
* `-r` — Reverse order.
* `-u` — Remove duplicate lines.

Examples:

printf "10\n2\n7\n" | sort
10
2
7

printf "10\n2\n7\n" | sort -n
2
7
10

printf "a\na\nb\n" | sort -u
a
b

### 3. `awk` — extract and transform columns

`awk` is a small text-processing programming language. It treats each input line as a record and splits it into fields, usually separated by spaces or tabs. Because it is a full language, it supports variables, conditions, loops, arithmetic, and pattern-based actions.

Common fields:

* `$1` — first column
* `$2` — second column
* `$0` — whole line

Examples:

echo "Alice 24" | awk '{print $1}'
Alice

echo "Alice 24" | awk '{print $2, $1}'
24 Alice

echo "Alice 24" | awk '{print $1 ":" $2}'
Alice:24

echo "Alice 24" | awk '$2 >= 18 {print $1, "is an adult"}'
Alice is an adult

echo -e "Alice 24\nBob 16\nCarol 31" | awk '{sum += $2} END {print sum / NR}'
23.6667

### 4. `sed` — stream editor

`sed` is commonly used for search-and-replace, deletion, insertion, and line-based text transformations.

General syntax:

sed [options] 'script' [file...]

A common substitute command looks like this:

sed 's/old/new/g'

Examples:

echo "cat cat" | sed 's/cat/dog/'
dog cat

echo "apple apple" | sed 's/apple/orange/g'
orange orange

echo -e "keep\nremovethis\nkeep" | sed '/removethis/d'
keep
keep

echo -e "one\ntwo\nthree" | sed -n '2p'
two

echo "abcdc" | sed 'y/abc/xyz/'
xyzdz

### 5. `head` and `tail` — limit output

These tools let you inspect only part of a file or stream.

* `head -n 5` — first 5 lines
* `tail -n 3` — last 3 lines

Examples:

seq 1 10 | head -n 3
1
2
3

seq 1 10 | tail -n 3
8
9
10

### 6. `tr` — translate or delete characters

`tr` works on characters rather than lines.

Examples:

echo "Hello" | tr '[:lower:]' '[:upper:]'
HELLO

echo "a b c" | tr -d ' '
abc

### Example pipeline

This pipeline filters apple rows, extracts the numeric column, and sorts it numerically:

echo "green-apple 5
red-apple 3
banana 2
fuji-apple 8" | grep "apple" | awk '{print $2}' | sort -n

Result:

3
5
8

Exercise
--------

You are given a product list. Use a pipeline to:

1. keep only lines containing `berry`,
2. extract the price column,
3. sort the prices from low to high,
4. print only the most expensive berry price.

Tutorial Code
-------------
#!/bin/bash
data="blueberry 5
strawberry 3
banana 2
raspberry 8
gooseberry 6
apple 10"

# write your code here

Expected Output
---------------
8

Solution
--------
#!/bin/bash
data="blueberry 5
strawberry 3
banana 2
raspberry 8
gooseberry 6
apple 10"

# write your code here
echo "$data" | grep "berry" | awk '{print $2}' | sort -n | tail -n 1
37 changes: 37 additions & 0 deletions tutorials/learnshell.org/es/Welcome.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Bienvenido

Bienvenido al tutorial interactivo de Programación Shell de learnshell.org.

Tanto si eres un programador experimentado como si no, este sitio web está pensado para cualquier persona que quiera aprender programación con intérpretes de shell Unix/Linux.

Te invitamos a unirte a nuestro grupo en Facebook para preguntas, discusiones y novedades.

Solo haz clic en el capítulo por el que quieras comenzar y sigue las instrucciones. ¡Buena suerte!

### Aprende lo básico

- [[Hello, World!]]
- [[Variables]]
- [[Passing Arguments to the Script]]
- [[Arrays]]
- [[Basic Operators]]
- [[Basic String Operations]]
- [[Decision Making]]
- [[Loops]]
- [[Array-Comparison]]
- [[Shell Functions]]

### Tutoriales avanzados

- [[Special Variables]]
- [[Bash trap command]]
- [[File Testing]]
- [[Input Parameter Parsing]]
- [[Pipelines]]
- [[Process Substitution]]
- [[Regular Expressions]]
- [[Special Commands sed,awk,grep,sort]]

### Tutoriales para contribuir

Lee más aquí: [[Contributing Tutorials]]