*Monday, 22nd June 2026 - 14:23 UTC+0100*
## Refresher
- `grep` or `egrep` filters input based on regex pattern matching
- `cat` concatenates file contents line by line
- `tail` gives only the last `-n` lines, whereas `head` gives the first `-n` lines
- `wc` does a word count, or line count (w/ flags `-w`, `-l`)
- `sed` does pattern-matched string replacement.
Regex is a vital skill for bash scripting as it will often be needed to filter files, data within files, match arguments, and a variety of other uses, so it is worth revising.
```bash
user:linux:~$ grep a fruits.txt
banana
apple
carrot
```
Square parentheses are a matching set, but adding a `^` inverses it.
```bash
user@linux:~$ grep [pc] fruits.txt
apple
carrot
```
## Extracting Scores with Shell
`cat footy_scores.csv | grep 1959` outputs the line for 1959.
Though if I were to use it on a different file, and if I wanted to count the number of lines in `two_cities.txt` that contained either the character *Sydney Carton* or *Charles Darnay*, I would use the following command instead:
```bash
cat two_cities.txt | egrep 'Sydney Carton|Charles Darnay' | wc -l
```
> [!Note]
> `egrep` is the same as `grep -E`
## A Simple Bash Script
Concatenating files in Bash is one of the most basic scripts that can be done:
```bash
user@linux:~$ cat file.ext
laaaaaaaaa
```
## Shell pipelines to Bash scripts
But there are sime situations in which you may be given a `.csv` file and you'll need to do a shell piped command that will aggregate and show how many times `X` has done `Y`. In this example, how many times each team has won.
```bash
cat footy_scores.csv | cut -d , -f 2 | tail -n +2 | sort | uniq -c
```
## Extract and Edit using Bash Scripts
Sometimes, you need to make multiple edits, and you need an easy way to do it.
```bash
cat footy_scores.csv | sed 's/Real/Real Madrid Club de Fútbol/g' | sed 's/Atleti/Club Atlético de Madrid/g' > footy_scores_edited.csv
```
## Arguments in Bash Scripts
There are three streams in Bash scripting:
- `STDIN` (standard input) - a stream of data into the program
- `STDOUT` (standard output) - a stream of data out of the program
- `STDERR` (standard error) - errors in the program
Furthermore, `2> /dev/null` in script calls redirects `STDERR` to be deleted.
Bash scripts can take arguments to be used inside by adding a space after the script execution call.
- ARGV is the array of all the arguments given to the program
- Each argument can be accessed via the `
notation. The first as `$1`, the second as `$2`, the third as `$3`, ad infinitum.
- `$@` and `$*` give all the arguments in ARGV
- `$#` gives the length (number) of arguments.
An example of how it works, with `args.sh`:
```bash
#!/usr/bash
echo $1
echo $2
echo $@
echo "There are " $# "arguments"
```
`bash args.sh one two three four five` outputs:
```
one
two
one two three four five
There are 5 arguments
```
---
[[Upskilling|← Back to Upskilling]]