Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 25 Feb 2006 21:43:59 +0100
From:      Kristian Vaaf <vaaf@broadpark.no>
To:        Randy Pratt <bsd-unix@comcast.net>
Cc:        questions@freebsd.org
Subject:   Re: How to ensure one blank line on top of ASCII files?
Message-ID:  <7.0.1.0.2.20060225214016.021e6cc0@broadpark.no>
In-Reply-To: <20060213195118.51e1334f.bsd-unix@comcast.net>
References:  <20060213195118.51e1334f.bsd-unix@comcast.net>

next in thread | previous in thread | raw e-mail | index | archive | help
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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?7.0.1.0.2.20060225214016.021e6cc0>