grep 'Stately' files/examples/ulysses.txt
Stately, plump Buck Mulligan came from the stairhead, bearing a bowl of
Modern Plain Text Social Science: Week 3
September 6, 2024
Eventually you will accumulate a history of shell commands you have typed, many of which you may want to reuse from time to time.
↑
^R
^R
will also work for history search at the RStudio console and in many other places.Symbol | Key | Unicode | Symbol | Key | Unicode |
---|---|---|---|---|---|
⎋ | Escape | U+238B |
⌫ | Backspace | U+232B |
⇥ | Tab | U+21E5 |
⌦ | Delete | U+2326 |
⇪ | Caps Lock | U+21EA |
⇱ | Home | U+21F1 |
⇧ | Shift | U+21E7 |
⇲ | End | U+21F2 |
⌃ | Control | U+2303 |
⇞ | Page Up | U+21DE |
⌥ | Option/Alt | U+2325 |
⇟ | Page Down | U+21DF |
⌘ | Command | U+2318 |
⌤ | Enter | U+2324 |
⏎ | Return | U+23CE |
␣ | Space | U+2423 |
grep
find
searches file and folder names only. To search inside files we use grep
. Or rather we will use a flavor of grep called egrep
.
Stately, plump Buck Mulligan came from the stairhead, bearing a bowl of
Search more than one file:
files/examples/jabberwocky.txt:“Beware the Jabberwock, my son!
files/examples/jabberwocky.txt: The Jabberwock, with eyes of flame,
files/examples/jabberwocky.txt:“And hast thou slain the Jabberwock?
files/examples/ulysses.txt:jabber on their girdles: roguewords, tough nuggets patter in their
grep
familygrep and its derivatives are very powerful thanks to their ability to use regular expressions. We will learn about those momentarily. There are also more recent command-line search tools like ripgrep, or rg
, a modern version of grep
that is very fast, automatically searches subfolders, and has other nice additional features. For more details see the ripgrep project page.
rg
and fzf
fzf
is a command-line fuzzy-finder. It makes ^R
really powerful and convenient. For details see the fzf project page.
Or,
Waiter, there appears to be a language inside my language
Regexps are their own world of text processing
☜ This book is a thing of beauty.
Here’s a file:
Search basics.txt
for apple
:
@
symbol”, or “Dates formatted as YYYY/MM/DD
” or “Any word that’s repeated”, and so on..
apple, banana, pear
Apple, Banana, Pear,
apple pie, apple, apple cake
apple
Apple
Apple. Banana. Pear.
Banana
Guava
Alabama
pear
peach, apple, plum
grey
gray
griy
groy
A period at the end.
Everything in the file matches the .
pattern.
.
” matches any character, how do you specifically match the character “.
”?\n
for “New Line”).\.
”Now the only match is the period (highlighted in red).
… how do you match a literal \
then?
… how do you match a literal \
then?
Well that’s ugly
This is the price we pay for having to express searches for patterns using a language containing these same characters, which we may also want to search for.
I promise this will pay off, though.
^
to match the start of a string.$
to match the end of a string.^
to match the start of a string.$
to match the end of a string.To force a regular expression to only match a complete string, anchor it with both ^
and $
apple, banana, pear
apple pie, apple, apple cake
apple
peach, apple, plum
\d
matches any digit. \s
matches any whitespace (e.g. space, tab, newline). [abc]
matches a, b, or c. [^abc]
matches anything except a, b, or c.
Use parentheses to make the precedence of |
clear:
?
is 0 or 1+
is 1 or more*
is 0 or more?
is 0 or 1+
is 1 or more*
is 0 or more?
is 0 or 1+
is 1 or more*
is 0 or more?
is 0 or 1+
is 1 or more*
is 0 or more{n}
is exactly n{n,}
is n or more{,m}
is at most m{n,m}
is between n and m{n}
is exactly n{n,}
is n or more{,m}
is at most m{n,m}
is between n and m{n}
is exactly n{n,}
is n or more{,m}
is at most m{n,m}
is between n and m{n}
is exactly n{n,}
is n or more{,m}
is at most m{n,m}
is between n and m{n}
is exactly n{n,}
is n or more{,m}
is at most m{n,m}
is between n and mBy default these are greedy matches. You can make them “lazy”, matching the shortest string possible by putting a ?
after them. This is often very useful!
{n}
is exactly n{n,}
is n or more{,m}
is at most m{n,m}
is between n and mBy default these are greedy matches. You can make them “lazy”, matching the shortest string possible by putting a ?
after them. This is often very useful!
apple
apricot
avocado
banana
bell pepper
bilberry
blackberry
blackcurrant
blood orange
blueberry
boysenberry
breadfruit
canary melon
cantaloupe
cherimoya
cherry
chili pepper
clementine
cloudberry
coconut
cranberry
cucumber
currant
damson
date
dragonfruit
durian
eggplant
elderberry
feijoa
fig
goji berry
gooseberry
grape
grapefruit
guava
honeydew
huckleberry
jackfruit
jambul
jujube
kiwi fruit
kumquat
lemon
lime
loquat
lychee
mandarine
mango
mulberry
nectarine
nut
olive
orange
pamelo
papaya
passionfruit
peach
pear
persimmon
physalis
pineapple
plum
pomegranate
pomelo
purple mangosteen
quince
raisin
rambutan
raspberry
redcurrant
rock melon
salal berry
satsuma
star fruit
strawberry
tamarillo
tangerine
ugli fruit
watermelon
Find all fruits that have a repeated pair of letters:
Backreferences and grouping are very useful for string replacements.
stringr
is Perl- or PCRE-like.grep
in the shellgrep
, sed
, and awk
.grep
in the shell-i
option create backup files of everything it touches:# Find every quarto file beneath the current directory
# Then edit each one in place to replace every instance of
# `percent_format` with `label_percent`
find . -name "*.qmd" | xargs perl -p -i.orig -e "s/percent_format/label_percent/g"
-i.orig
flag will back up e.g. analysis.qmd
to analysis.qmd.orig
before changing analysis.qmd
.One view of things