find . -iname "*.cpp" -exec perl -ni -e 'chomp; print "$_\n"' {} \;

Add a newline to the end of a cpp file

Adds a newline to the end of all cpp files in the directory to avoid warnings from gcc compiler.

2009-02-18 14:12:24

These Might Interest You

  • tail -c 1 "$1" returns the last byte in the file. Command substitution deletes any trailing newlines, so if the file ended in a newline $(tail -c 1 "$1") is now empty, and the -z test succeeds. However, $a will also be empty for an empty file, so we add -s "$1" to check that the file has a size greater than zero. Finally, -f "$1" checks that the file is a regular file -- not a directory or a socket, etc. Show Sample Output

    endnl () { [[ -f "$1" && -s "$1" && -z $(tail -c 1 "$1") ]]; }
    quintic · 2010-08-25 12:06:10 0
  • Changing newline to spaces using just echo Show Sample Output

    echo $(</tmp/foo)
    tatsu · 2009-10-01 12:43:20 0
  • Useful to detect number of tabs in an empty line, DOS newline (carriage return + newline). A tool that can help you understand why your parsing is not working. Show Sample Output

    cat -v -t -e
    alperyilmaz · 2009-03-24 19:29:03 1
  • end_w_nl filename will check if the last byte of filename is a unix newline character. tail -c1 yields the file's last byte and xxd converts it to hex format.

    end_w_nl() { [[ $(tail -c1 $1 | xxd -ps) == 0a ]] }
    xro · 2012-09-18 20:11:29 0
  • If you cat the file, all the parts of the command line are bunched up. If you use tr to convert the nulls to spaces, you're still left without a newline unless you add another step. This command does everything for you. Show Sample Output

    xargs -0a /proc/27288/cmdline echo
    dennisw · 2015-09-25 17:35:11 6
  • Using "wmic get * /value" within any Cygwin shell will return lots of Win/Dos newline junk ie "^M$" at the end of found value line, two lines ("$" Unix newline) above, and three below. This makes storing and or evaluating wmic queries as variables a pain. The method i suggest strips the mentioned junk, only returns the value after "OSArchitecture=", and includes only one Unix style newline. Other methods using sed|awk|cut can only handle the output of wmic cleanly when piped or using multiple sed statements. wmic OS get OSArchitecture /value | sed 's/\r//g;s/^M$//;/^$/d;s/.*=//' making wmic OS get OSArchitecture /value | grep -Eo '[^=]*$' a much cleaner and slightly less costly alternative. Show Sample Output

    wmic OS get OSArchitecture /value | grep -Eo '[^=]*$'
    lowjax · 2014-03-15 02:04:08 0

What Others Think

why not just use: perl -pi -e 's:$:\n:g' (less to type)
sil · 482 weeks and 3 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: