How to replace new line characters \r\n with a newline in VIM
To get the ^M character, type CTRL+V and press enter:
:%s/\\r\\n/^M/g
To get the ^M character, type CTRL+V and press enter:
:%s/\\r\\n/^M/g
Replace control character ^A with tab character:
sed -e "s/$(echo -e \\001)/\\echo -e '\t'/g" file.txt
See Wikipedia for list of control characters.
NOTE: remember to exclude the .git folder…
Search and replace XXX with YYY in all files:
perl -e "s/XXX/YYY/g;" -pi $(find . -type f)
If you get this error:
zsh: argument list too long: perl
Your argument list is clearly too long. Try this instead, or use xargs:
find . \
-type f \
-exec perl -i -pe's/XXX/YYY/g' {} +
Using sed is more complicated, but this should at least work on Linux:
# Find, backup and replace
find . -name "*.rb" -print | xargs sed -i.bak 's/XXX/YYY/g'
# Delete backup files
find . -name '*.bak' -type f -delete
Also see: https://snippets.aktagon.com/snippets/861-search-and-replace-file-contents-and-file-names
# Replace Tree with Trees
# NOTE: If you want to backup the files, change -i '' to -i (without empty string)
grep -rl Tree . |xargs sed -i '' -e 's/Tree/Trees/'
find . -name '*observation*' -exec bash -c 'mv $0 ${0/observation/condition}' {} \;
References https://www.commandlinefu.com/commands/matching/search-replace/c2VhcmNoIHJlcGxhY2U=/sort-by-votes
find . -name "*.yaml" | xargs perl -p -i -e "s/foo/buzz/g"
find . -type f -name "*.yaml" -print0 | xargs -0 sed -i '' -e 's/foo/bar/g'
I recommend installing and using repren if you need a simpler solution: https://github.com/jlevy/repren
Reference: