Today I was attempting to run a grep search on all the *.as files in the project code I am working with. The code in places is both old and new and has been edited in a variety of editors, from the Flash IDE, to TextMate, to Flash Builder, to who knows how else. The unfortunate result of this history is that many of the files have Mac style line endings (\r) instead of Unix style line endings (\n). When trying to run a grep search on these files, the search returns a bunch of jumbled code salad.
Since I was only really wanting to do the search for now, I ended up using:
tr '\r' '\n' <filename.txt | grep searchstring
This works by using the transform command to convert the mac line endings, and pipe out the result to a grep search that is now happy. For more permanent results, the line endings could be replaced entirely using:
perl -pi -e 's/\r/\n/g' textfile.txt
The “tr” command, however, was a great temporary work around for running the search on the files that I needed to run, since I didn’t want to end up having to recommit a bunch of files to SVN just to change the line-endings. The original jumbled search result I was getting was somewhat inexplicable until I started analyzing the reason why it worked for some files and not for others. Hope this helps someone out there.