Carriage Return at end of file

Hi, I have a script that outputs a file that contains the dates from the previous month, which is then used by our application to run processes on each date contained in the file. My problem is is that my script created a blank line at the bottom of the file which causes issues for our application. I'm looking for suggestions on how to stop or remove the last line/carriage return. Any suggestions are appreciated. Thanks! :slight_smile:

Here is the current script:

echo "Creating DATES file. `date +"%m-%d-%Y_%H:%M:%S"`"
outdir=/test/iofiles

# ja fe ma ap ma ju ju ag se oc no de
set -A lasts 0 31 28 31 30 31 30 31 31 30 31 30 31
typeset -Z2 dmonth dday

month=`date +%m`
year=`date +%y`
((pmonth=month-1))

rm $outdir/date.txt

day=1
while((day<(lasts[pmonth])+1)) ; do
dday=$day
dmonth=$pmonth
echo ${dmonth}/${dday}/${year} >> $outdir/date.txt
((day=day+1))
done
echo "DATES file created. `date +"%m-%d-%Y_%H:%M:%S"`"

sed "/^ *$/d" $outdir/date.txt > temp
mv temp $outdir/date.txt 

Should I place this after the while loop or within it? I placed it after the loop and I still had the blank line at the end of the file. :frowning:

You have that code at the end of the loop.
Do you mean you have blank line or line with "/" at the end of the file?

It just has a blank line at the end

---------------
09/01/06
09/02/06
09/03/06
09/04/06
09/05/06
09/06/06
09/07/06
09/08/06
09/09/06
09/10/06
09/11/06
09/12/06
09/13/06
09/14/06
09/15/06
09/16/06
09/17/06
09/18/06
09/19/06
09/20/06
09/21/06
09/22/06
09/23/06
09/24/06
09/25/06
09/26/06
09/27/06
09/28/06
09/29/06
09/30/06
<---Want to remove this blank line.
--------------------------------

Thanks.

sed '$d' myFile.txt

Above code should work. I dont know whats happening in your case.
Try this

sed "$ d" $outdir/date.txt > temp
mv temp $outdir/date.txt 

This ended up deleting the 09/30/06 line, but the blank line is still at the end of the file.

----------------------
...........
09/27/06
09/28/06
09/29/06
---{blank line}---
----------------------

Then you don't have blank line in the file. Are you doing something else after SED?

I think it's my text editor (UltraEdit) displaying the file improperly, because it I use VI there is not a lbank line. But my application is seeing a blank line also when reading this file. I'm rather confused now. I may end up manully editing the file on a monthly basis :frowning:

the only thing that happens after the sed is an echo with the date stamp so be can monitor start and end times of all our processes.

you might have a '^M' as th last character on the LAST line.
try doing 'od -x' or 'cat -vet' on a file and see what you have.

Won't a '^M' show using vi? I've come across the '^M' when files are ftp'd using the wrong mode.

cat -vet output:

...
09/20/06$
09/21/06$
09/22/06$
09/23/06$
09/24/06$
09/25/06$
09/26/06$
09/27/06$
09/28/06$
09/29/06$
09/30/06$
$

I believe I figured out the problem. The echo command places a new line character at the end of each iteration thru the loop. I just needed to suppress the new line char for the last date. Lines changed and/or added in bold below...

outdir=/testg1/scripts

# ja fe ma ap ma ju ju ag se oc no de
set -A lasts 0 31 28 31 30 31 30 31 31 30 31 30 31
typeset -Z2 dmonth dday

month=`date +%m`
year=`date +%y`
((pmonth=month-1))

rm $outdir/date.txt

day=1
while((day<(lasts[pmonth]))) ; do
dday=$day
dmonth=$pmonth
echo ${dmonth}/${dday}/${year} >> $outdir/date.txt
((day=day+1))
done

dday=$day
dmonth=$pmonth
echo ${dmonth}/${dday}/${year}"\c" >> $outdir/date.txt

Thanks for everyone help!