Skip to content

Instantly share code, notes, and snippets.

@naganowl
Last active February 17, 2017 19:02
Show Gist options
  • Save naganowl/0e6211498e028c606f14ec1140a49996 to your computer and use it in GitHub Desktop.
Save naganowl/0e6211498e028c606f14ec1140a49996 to your computer and use it in GitHub Desktop.
Replacing leading newlines from a text editor find/replace
# Get the files changed in SHA
git diff-tree --no-commit-id --name-only -r <SHA> > theFiles
# Inline replace and backup file (b/c of Mac) for leading newlines
xargs -0 -n 1 sed -ie '/./,$!d'< <(tr \\n \\0 <theFiles)
# Same as above, but for trailing newlines
find . -type f -name '*.coffee' -exec sed -ie -e :a -e '/^\n*$/{$d;N;};/\n$/ba' {} \;
# Remove the backup files (assuming only Coffee files changed)
find . -name *.coffeee | xargs rm -f
# Find files w/ duplicate exports
grep -Rn -o "module.exports.*$" app/assets/javascripts | sort -n | uniq -c | cut -d : -f 1 | sort | uniq -d
# Output giant file with all duplicate lines from filtered files that can be used manually to search for duplicate require/exports
find app/assets/javascripts -type f -name '*.coffee' -print0 | xargs -0 -I '{}' sh -c 'sort {} | uniq -d' > dupes
# Mass rename files by stripping a character off of the original file name
find . -type f -name '*.coffeee' | while read file; do mv "$file" "${file%?}"; done
# `sed -ie '/./,$!d'` can be replaced above with the command that you want
# On Unix systems, shouldn't need to provide an extension as it doesn't require a backup file (so it can just be `sed -i ...`
# `cat theFiles | xargs sed -ie ...` seemed to only strip the first file
# Before the initial find/replace, `find . -name *.coffee | xargs sed -ie ...` should let you skip the temp file
# When using `find ... -exec`, terminate the command with `\;`
# In `find` and `xargs` (and possibly in other Unix commands), it appears that `{}` is a reference to the current file/line in loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment