-
Notifications
You must be signed in to change notification settings - Fork 5
/
04-pipes.qmd
334 lines (224 loc) · 7.32 KB
/
04-pipes.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
---
title: "Pipes and Filters"
format:
revealjs:
theme: _extensions/metropolis-theme/metropolis.scss
chalkboard: true
logo: /images/ScPo-logo.png
footer: "[SciencesPo Intro To Programming 2024](https://floswald.github.io/ScPoProgramming/)"
incremental: false
code-line-numbers: false
highlight-style: github
author: Florian Oswald and The Software Carpentry
subtitle: "[SciencesPo Intro To Programming 2024](https://floswald.github.io/ScPoProgramming/)"
date: today
date-format: "D MMMM, YYYY"
---
## Combining Commands
* We are now ready to combine some of the commands we learned.
* You will see that here is where the real power lies.
* Let's navigate into our exercise data folder first.
```bash
$ cd ~/shell-lesson-data/exercise-data/proteins
$ ls
cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb
```
* Those are *protein data bank* files.
---
## Capturing Output
* Introducing the `wc` word count command.
```bash
wc cubane.pdb
20 156 1158 cubane.pdb
```
29 lines, 156 words, 1158 characters.
* Let's **redirect** the output of `wc` to a file instead with `>`:
```bash
wc -l *.pdb > lengths.txt
```
* no ouput on screen, you see? but now there is a new file: `lengths.txt`.
* Let's *concatenate* its content (i.e. join together) and print to screen:
```bash
$ cat lengths.txt
20 cubane.pdb
12 ethane.pdb
9 methane.pdb
30 octane.pdb
21 pentane.pdb
15 propane.pdb
107 total
```
---
## Reading Text Files
* `cat` prints the entire thing to screen.
* `tail` only the end
* `head` only the beginning
* `less` lets you scroll and read (arrows up/down or `j` (up) and `k` (down), `q` exits.)
```bash
$ head -n 3 ../animal-counts/animals.csv
$ tail -n 2 ../animal-counts/animals.csv
$ less ../../north-pacific-gyre/NENE01729A.txt
```
---
## Printing Text with `echo`
* The `echo` function prints text - by default to screen:
```bash
$ echo hi
hi
```
* But you can redirect it to a file as well:
```bash
$ echo I said hi! > echofile1.txt
```
::: {.callout-important}
# Challenge
Do 2 times in a row:
```bash
$ echo I said hi! > echofile1.txt
```
Now do twice (notice `>>`!)
```bash
$ echo I said hi! >> echofile2.txt
```
* What's happening?
:::
---
## Appending to Files
::: {.callout-important}
# Challenge
Consider the file `shell-lesson-data/exercise-data/animal-counts/animals.csv`. What is result of this:
```bash
$ head -n 3 animals.csv > animals-subset.csv
$ tail -n 2 animals.csv >> animals-subset.csv
```
1. The first three lines of animals.csv?
2. The last two lines of animals.csv?
3. The first three lines and the last two lines of animals.csv?
4. The second and third lines of animals.csv?
:::
<details>
<summary>Show Solution</summary>
::: {.callout-note}
# Solution
Option 3 is correct.
:::
</details>
---
## Filtering Files with `sort`
* `sort` reads a file and *sorts* it's content to screen
* it does not change the file.
```bash
$ sort -n lengths.txt
```
```
9 methane.pdb
12 ethane.pdb
15 propane.pdb
20 cubane.pdb
21 pentane.pdb
30 octane.pdb
107 total
```
---
## Filtering Files and using the result
* Cool 😎 but now we want to use this list.
* Could save it to a new file?
```bash
$ sort -n lengths.txt > sorted_lengths.txt
$ head -n 2 sorted_lengths.txt
```
```
9 methane.pdb
12 ethane.pdb
```
## Filtering Files and **the pipe**
* We call `|` the pipe. It takes output from a command and gives it to another command.
* Modern languages use their own version of this (R has a package and now also a native pipe, julia has of course a pipe etc. Stata not sure 😜)
* The **pipe** allows us to do this *without* storing intermediate results.
```bash
$ sort -n lengths.txt | head -n 1
```
```
9 methane.pdb
```
* But, wait 🤔. Then we don't even need `lengths.txt`:
```bash
$ wc -l *.pdb | sort -n | head -n 1
```
```
9 methane.pdb
```
* That's a *pipeline*. 🤯
---
## Piping Away
* Make sure we are still in `~/shell-lesson-data/exercise-data/proteins`
:::{.callout-important}
# Pipe Dreams
Which of the following commands shows us the 3 files with the least number of lines in the current directory? Build the pipeline up from left to right to check!
1. `wc -l * > sort -n > head -n 3`
2. `wc -l * | sort -n | head -n 1-3`
3. `wc -l * | sort -n | tail -n 4 | head -n 3`
4. `wc -l * | sort -n | head -n 3`
:::
---
## Piping Away
* Make sure we are still in `~/shell-lesson-data/exercise-data/proteins`
<details>
<summary>Show Solution</summary>
::: {.callout-note}
# Solution
Option 4 is correct. Option 3 finds the ones with *most* lines.
:::
</details>
---
## Cutting and Piping
* We have a `.csv` file here: `shell-lesson-data/exercise-data/animal-counts`
* Let's use the `cut` command to get parts of it.
```bash
$ cd ~/shell-lesson-data/exercise-data/animal-counts
$ cut -d , -f 2 animals.csv
```
::: {.callout-important}
# Building a Pipe
* `uniq` filters **adjacent** matching lines in a file.
* Can you extend the above command with `uniq` (and another command?) such that we get the list of unique animal names?
* Add the `-c` flag to `uniq` to get a contingency table.
:::
---
## Building a Pipe
<details>
<summary>Show Solution</summary>
::: {.callout-note}
# Solution
1. `cut -d , -f 2 animals.csv | sort | uniq`
1. `cut -d , -f 2 animals.csv | sort | uniq -c`
:::
</details>
---
## House Prices in France
The below dataset contains information on house sales (price, location, type of house etc). We call one record a _housing transaction_.
Using the shell:
1. Use `wget` to download data to [from here](https://static.data.gouv.fr/resources/demandes-de-valeurs-foncieres/20240408-125738/valeursfoncieres-2023.txt) to your downloads folder as `carburants.csv`: `wget https://static.data.gouv.fr/resources/demandes-de-valeurs-foncieres/20240408-125738/valeursfoncieres-2023.txt`
2. use `wc -l` to count how many rows (*lines*) there are
3. use `head -n 2` to see the first two rows (the *header*)
4. Use the above solution to build a contingency table that tells us the number of housing transactions per *commune*. Show the 10 cities with most housing transactions.
5. Compute the average of variable `Valeur fonciere`. You should use the `awk` command like this : `awk 'BEGIN{s=0;}{s+=$1;}END{print s/(NR);} your_file.txt'`
---
## House Prices in France
The below dataset contains information on house sales (price, location, type of house etc). We call one record a _housing transaction_.
Using the shell:
5. Compute the average of variable `Valeur fonciere`. You should use the `awk` command like this : `awk 'BEGIN{s=0;}{s+=$1;}END{print s/(NR);} your_file.txt'`
---
## Real Data
<details>
<summary>Show Solution</summary>
1. `wget https://static.data.gouv.fr/resources/demandes-de-valeurs-foncieres/20240408-125738/valeursfoncieres-2023.txt`
2. `wc -l valeursfoncieres-2023.txt`
3. `head -n 2 valeursfoncieres-2023.txt`
4. `cut -d '|' -f 18 valeursfoncieres-2023.txt | sort | uniq -c | sort -r | head -n 10 `
5. `cut -d '|' -f 11 valeursfoncieres-2023.txt | cut -d , -f 1 | awk 'BEGIN{s=0;}{s+=$1;}END{print s/(NR);}' `
6. If you have R installed you can check whether this is the same as reading this column into it (it's not! odd.)
```bash
Rscript -e 'x = data.table::fread(cmd = "cut -d \'|\' -f 11 valeursfoncieres-2023.txt | cut -d , -f 1"); x[, mean(`Valeur fonciere`,na.rm = TRUE)]'
```
</details>