How to ensure one blank line on top of ASCII files?

Kristian Vaaf vaaf at broadpark.no
Sat Feb 25 12:43:54 PST 2006


At 01:51 14.02.2006, Randy Pratt wrote:
>Hi,
>
>I'm replying offlist since this isn't particularly a FreeBSD
>question.
>
>Something like this may work for you:
>
>addline.sh:
>======================================
>#!/bin/sh
>#Check if file begins with blank line, if not, insert a blank line
>
>firstline="`head -1 "${1}"`"
>
>if [ "${firstline}" = "" ]; then
>    #echo "its a blank"
>else
>    #echo "insert line"
>    sed -i "" '1{s/^/\
>/;}' "${1}"
>fi
>=====================================
>
>Usage: addline.sh somefile.txt
>
>If you needed to do many files, then make a loop to do each one
>
>         for i in *.txt; do addline.sh "$i"; done
>
>or whatever syntax you need for the shell you are using.  I missed
>the beginning of the thread so I'm not sure of all the details.
>
>Caveat: This should be checked with some sample files before
>using on your good files.  I just did a few minimal tests.
>
>Note that this:
>
>    sed -i "" '1{s/^/\
>/;}' "${1}"
>
>is not a typographical error.  It is adding the newline after
>the blank line.  Check some of the online sed tutorials
>for an explanation of the syntax.  The manual page for sed
>is a bit terse ;-)
>
>Hope this helps more than it confuses!
>
>Randy
>
>
>
>
>--

Hello Randy!

Sorry to disturb, but how can I make this script add a blank line to the top
of all ASCII files except the ones that contain at the beginning "#!"?

It would also be nice to rule out certain filetypes.

#!/usr/local/bin/bash
#
#   Add blank line to the top of text files.
#   $ARBA: blank.sh,v 1.0 2007/11/11 15:09:05 vaafExp $
#
#   Use: blank
for file in `find -s . -type f -not -name ".*"`; do
         if file -b "$file" | grep -q 'text'; then
                 echo > blank
                 mv $file $file.tmp
                 cat blank $file.tmp >> $file
                 rm -f $file.tmp
                 rm -f blank
                 echo "$file: Done"
         fi
done

Thanks!

Vaaf



More information about the freebsd-questions mailing list