Bash script: Recursively convert line breaks in all files from Dos/Windows to Unix AND Replace Tabs with Spaces.

It’s really annoying that on Windows, nearly every program that deals with text saves the text in Windows-format. That’s not good when you work in a multi-OS-environment.
Another annoying thing are TABS, which often are much wider in Linux editors than in Windows, and are generally superflous as i think. Especially when programming Scala, where the style guide suggests using spaces instead of tabs.

So today i ended my misery with converting text-files and tabs by hand and finally wrote a Bash script. It works both on Linux and on Cygwin.

It solves both problems at once: It converts Windows CR/LF line breaks (only when there is one!), AND replaces those superflous tabs with spaces. And all that with one single line.

find . -type f -regextype posix-awk -regex "(.*.java|.*.scala)" -exec sed -i -e 's/^M$//' -e 's/\t/  /g' {} ';'

What does it?

find .
recursively finding files in the current directory

-type f
only use files (d for directory)

-regextype posix-awk -regex “(.*.java|.*.scala)”
Find all files with extension Java OR Scala (you can add more)

-exec sed -i -e ’s/^M$//’
Remove CR/LF line break. -i means that the changes are written to the source immediately, -e is to concatenate multiple replacements

-e ’s/\t/ /g’
replace all Tabs with 2 spaces

{} ‘;’
use files found by find as parameters for sed, end

This entry was posted in General and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>