Bash Script: Windows --> *nix Line-Endings

Printer-friendly version

Update (23 October, 2007): A colleague, Jonah, has pointed me to a C program that accomplishes this much more efficiently than the below script. The source code and binaries of flip for different platforms are available at http://ccrma-www.stanford.edu/~craig/utility/flip/.

I originally wrote this simple script to convert the line-ending format in a Windows archive full of Python code stored in .txt files. It's operation is relatively straightforward: simply run it in the directory full of the files you wish to modify. It will scan the directory for any files with a .txt extension and change the line-endings. It will also scan the files in the first layer of any subdirectories it encounters. I found the actual bit that changes the line endings in the Sed One-Liners.

####################################################################
# Napzilla's Line-Ending Converter
####################################################################
# DATE: 2007-06-15
# VERSION: 0.0.1
# DESCRIPTION: 'A simple line-ending conversion script, orignally written to
# convert line endings on the files inside the "Learning Python" example sets.
# The sed command was taken from the Sed One Liners. This script changes all
# files in the main directory and the first layer of subdirectories.'
#
# Step 1: Change all .txt files in the current directory.
ls *.txt | sed 's/.$//';
echo 'Stage one complete';
# Step 2: Recursively list all subdirectories into a file.
ls -d */ > dirlist.tmp;
echo 'Commencing subdirectory scan';
# Step 3: Step through the list of subdirectories.
for dir in $(cat dirlist.tmp);
do
#   List items in the subdirectory into file.
    ls $dir*.txt > txtlist.tmp;
#   Step into the file list.
    for fle in $(cat txtlist.tmp);
    do  
#       Change line endings in each file listed.
        sed 's/.$//' $fle;
    done
#   Remove the temporary file list.
    rm txtlist.tmp;
#   Step to next subdirectory.
    echo "Subdirectory $dir has been scanned.";
done
# Remove subdirectory list
rm dirlist.tmp
# End Process notification.
echo 'Process complete. Now get back to work!'

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Use [fn]...[/fn] (or <fn>...</fn>) to insert automatically numbered footnotes.
  • Images can be added to this post.
  • You may quote other posts using [quote] tags.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.