mds08011 / shell-scripts

GNU General Public License v3.0
0 stars 0 forks source link

Replace #7

Open mds08011 opened 2 years ago

mds08011 commented 2 years ago

https://news.ycombinator.com/item?id=29318751 https://will-keleher.com/posts/5-Useful-Bash-Patterns.html

Use for to iterate over simple lists I definitely use this all the time. Also, generating the list of things over which to iterate using the output of a command:

for thing in $(cat file_with_one_thing_per_line) ; do ...

Find and replace a pattern in a codebase with capture groups git grep -l pattern | xargs gsed -ri 's|pat(tern)|\1s are birds|g'

git grep -l: make sure we're only looking for files in our codebase (ag -l is another good option) xargs: allow running this with gsed -i gsed -i: edit files (default mac sed is bad, so gnu-sed is essential) gsed -r: use regular expression for the pattern to allow capture groups s|: the first character after the s is used as the delimiter. / is pretty consistently annoying because it's used in file paths & urls. gsed '...': the single quotes are important to avoid escapes 's|pat(tern)|\1s are birds|g': being able to easily use capture groups in a find and replace is amazing |g: replace this multiple times in a line