From owner-freebsd-questions@FreeBSD.ORG Sat Feb 25 20:43:54 2006 Return-Path: X-Original-To: questions@freebsd.org Delivered-To: freebsd-questions@FreeBSD.ORG Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 6CF2616A420 for ; Sat, 25 Feb 2006 20:43:54 +0000 (GMT) (envelope-from vaaf@broadpark.no) Received: from osl1smout1.broadpark.no (osl1smout1.broadpark.no [80.202.4.58]) by mx1.FreeBSD.org (Postfix) with ESMTP id E877443D45 for ; Sat, 25 Feb 2006 20:43:53 +0000 (GMT) (envelope-from vaaf@broadpark.no) Received: from osl1sminn1.broadpark.no ([80.202.4.59]) by osl1smout1.broadpark.no (Sun Java System Messaging Server 6.1 HotFix 0.05 (built Oct 21 2004)) with ESMTP id <0IV900JJ0G95QJD0@osl1smout1.broadpark.no> for questions@freebsd.org; Sat, 25 Feb 2006 21:43:53 +0100 (CET) Received: from urban.broadpark.no ([213.187.181.70]) by osl1sminn1.broadpark.no (Sun Java System Messaging Server 6.1 HotFix 0.05 (built Oct 21 2004)) with ESMTP id <0IV900KZSG93H7C0@osl1sminn1.broadpark.no> for questions@freebsd.org; Sat, 25 Feb 2006 21:43:53 +0100 (CET) Date: Sat, 25 Feb 2006 21:43:59 +0100 From: Kristian Vaaf In-reply-to: <20060213195118.51e1334f.bsd-unix@comcast.net> To: Randy Pratt Message-id: <7.0.1.0.2.20060225214016.021e6cc0@broadpark.no> MIME-version: 1.0 X-Mailer: QUALCOMM Windows Eudora Version 7.0.1.0 Content-type: text/plain; charset=us-ascii; format=flowed Content-transfer-encoding: 7BIT References: <20060213195118.51e1334f.bsd-unix@comcast.net> Cc: questions@freebsd.org Subject: Re: How to ensure one blank line on top of ASCII files? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 25 Feb 2006 20:43:54 -0000 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