Skip to content

Instantly share code, notes, and snippets.

seq 20 | xargs -I{} echo "Hi there {}"
@naganowl
naganowl / conditional-sed.sh
Created September 13, 2019 23:33
Replace Coffee w/ JS in all lines except if it contains `..`
grep -lr \.coffee app/assets/ | xargs sed -i '' -e '/\.\./!s/coffee/js/g'
@naganowl
naganowl / average-scss-loc.sh
Last active May 31, 2019 19:23
Average length of stylesheet
# Get list of all files | Print out each line count | Remove final line total | Calculate average
find . -name *.scss | xargs wc -l | sed -e '$ d' | awk '{ total += $1; count++ } END { print total/count }'
@naganowl
naganowl / dynamic-prepend.sh
Last active November 6, 2018 00:37
Add text after first line of file
# Inspired by Stack Overflow Question 19151954
LINE_NO = `grep -n require $1 | tail -1 | cut -d: -f1`
sed ''"${LINE_NO}"' a \
text_to_add
' $1
@naganowl
naganowl / replace-newlines.sh
Created September 12, 2018 01:05
Replace blank lines with just a single one
# Inspired by https://unix.stackexchange.com/questions/72739/how-to-remove-multiple-blank-lines-from-a-file
for i in `grep -rl classSelector app/assets/stylesheets/` ; do cat $i | awk '!NF {if (++n <= 1) print; next}; {n=0;print}' | tee $i; done
@naganowl
naganowl / shell.js
Created August 8, 2018 18:29
Different ways to shell out in node
const childProcess = require('child_process');
function runShell(cmd, args, options) {
const output = childProcess.spawnSync(cmd, args, options);
return output.stdout.toString();
};
// 1. With just the base command
const findCmd = `find app/assets -type f -name *.js -not \( -path *spec* \) -o -name *.coffee -not \( -path *spec* \)`;
@naganowl
naganowl / subdirectories.sh
Created August 7, 2018 19:20
List directories that contain SCSS files
find app/assets -type f -name '*.scss' | sed -E 's|/[^/]+$||' | sort -u
@naganowl
naganowl / double-xargs.sh
Created August 7, 2018 01:42
Double loop over files for better performance
# Assumes two files present (`sel` and `filez`)
# OSX needs an empty zero length argument, which is quite difficult to escape if using the same type of quotations
# Inner file (`filez`) is used over a `find ... -exec` to cut down on repetitive operations
cat sel | xargs -I '{}' sh -c 'cat filez | xargs sed -i "" "s/{}//"'
@naganowl
naganowl / find-replace.sh
Last active August 6, 2018 18:26
Find/replace except in a directory
# https://stackoverflow.com/questions/18046456/excluding-a-directory-from-find-and-sed-rename
# https://stackoverflow.com/questions/9704020/recursive-search-and-replace-in-text-files-on-mac-and-linux
find app -type f -name '*.erb' -not \( -path 'app/views/helpers/*' \) -exec sed -i '' s/this/that/ {} +
# Given a file `sel` of strings, remove all instances of them in all other directories
for i in `cat sel` ; do echo $i | cut -c2- | find app -type f -name '*.erb' -not \( -path 'app/views/helpers/*' \) -exec sed -i '' "s/$i//" {} +; done
@naganowl
naganowl / tags.md
Created May 25, 2018 22:26
Differences b/w Git vs. npm tags

There is a difference b/w a prerelease tag associated to a package version (https://docs.npmjs.com/misc/semver#prerelease-tags) and a package's distribution tag upon publish (https://docs.npmjs.com/cli/dist-tag#purpose).

Thus, given the URL schemas dictated by https://docs.npmjs.com/files/package.json#git-urls-as-dependencies it be possible to publish an npm package with both a prerelease tag on a version and a distribution tag to give two options for targetting within a library

The RFC behind the new semver notation specifies just a prerelease tag. It be worthwhile to publish some sort of distribbution tag to avoid clobbering the default latest tag that npm uses