PDA

View Full Version : adding newlines to ends of files


cyberslayer
December 1st, 2006, 07:31 PM
I have a directory that contains a large amount of C++ source code as well as subdirectories which contain more code and I would to know how to add newlines to the ends of all text/cpp/hpp/h files that don't end with newlines
so that g++ won't generate 5 million lines like "warning: no newline at end of file" when I build my code. I don't want to do this manually because there are far too many files to add newlines to by opening them and adding a blank line to the end of each file.

Thanks

lloyd mcclendon
December 1st, 2006, 08:10 PM
hi

you might want to check out awk it's sweet


but something like this

for extension in "txt cpp hpp h" ; do
for file in `find -type f -iname "*.${extension}" ; do
cat ${file} | awk ' { print $0 } END { print } ' > ${file}.new
done
done

then you can use a similar looping technique to mv ${file} ${file%.new}

Jucato
December 1st, 2006, 09:07 PM
I think you can safely ignore those since they are just warnings. However, I believe that a new line at the end of the file is an ISO C/C++ standard.

dwblas
December 4th, 2006, 08:18 PM
Read the dir. Open each file in append mode and write a "\n". If you want to get fancy you can check the last character to see if it is a newline first.

ghostdog74
December 5th, 2006, 01:08 AM
find . \( -name "*.cpp" -o -name "*.h" -o -name "*.hpp" \) -print | xargs sed -i -e "/$/G"

Arndt
December 5th, 2006, 05:22 AM
find . \( -name "*.cpp" -o -name "*.h" -o -name "*.hpp" \) -print | xargs sed -i -e "/$/G"


sed -i -e "/$/G" adds a newline after every line. To add one only after the last line, do

sed -i -e '$G'

To add a newline only if there isn't one in place seems more difficult.

ghostdog74
December 5th, 2006, 05:26 AM
sed -i -e "/$/G" adds a newline after every line. To add one only after the last line, do

sed -i -e '$G'

To add a newline only if there isn't one in place seems more difficult.

thanks. I have rusty brains :)

Ben Sprinkle
December 5th, 2006, 10:05 AM
If you open a file in Gedit it auto puts a new line at end of file so just open and save. Could take longer though. :P