rbend
asked on
How to strip off extra blank lines from the end of a text file?
I have a text file that has been formatted to be exactly 60 lines long even if the text is less. It has extra blank lines padding the end. What UNIX script or command can I use to get rid of the extra blank lines at the end of the file?
DIGITAL UNIX
DIGITAL UNIX
perl -0777 -i -pe 's/\s+$/\n/' file
ASKER
ozo: there is no perl command on my system.
Astroman: only want the blank lines at the end to go. How do I write that?
Astroman: only want the blank lines at the end to go. How do I write that?
ASKER
Astroman:
I tried your script, and I can see the results as it is executing, but when I look at the file afterword, there has been no change to it. ??
I tried your script, and I can see the results as it is executing, but when I look at the file afterword, there has been no change to it. ??
ASKER
Astroman: Sorry... I directed it to "file2" and I got the end result. Still need to know how to do just the ones at the end.
Sorry, I never thought of mentioning that sed is a filter. what you should nevre do is:
sed <cmd> FILE1 > FILE1
As it will just leave you with an empty file.
Not sure immediatly how to do only the lines at the end using standard unix commands, but I'll work on it.
Perl is free software though, if you want to install it, and it's very useful for all sorts of things.
look at www.perl.org
I'll be back! (but not for a few hours)
sed <cmd> FILE1 > FILE1
As it will just leave you with an empty file.
Not sure immediatly how to do only the lines at the end using standard unix commands, but I'll work on it.
Perl is free software though, if you want to install it, and it's very useful for all sorts of things.
look at www.perl.org
I'll be back! (but not for a few hours)
does `tail` on your system have a -r option?
if you have bash, the following might work (call it crclean):
#!/bin/bash
CRFLAG=""
fixup()
{
while read ; do
if [ "$CRFLAG" != "" ] ; then
echo "$REPLY" >> crtac
elif [ -l "{$REPLY}" -gt 2 ] && [ "$CRFLAG" = "" ] ; then
CRFLAG="yes"
echo "$REPLY" > crtac
fi
done
}
tac $1 | fixup
tac crtac > crfixed
rm crtac
# invoke this with 'crclean myfile'
# your cleaned-up file is now called 'crfixed'
# you may have to edit this to change paths for the temporary file and the output file
#!/bin/bash
CRFLAG=""
fixup()
{
while read ; do
if [ "$CRFLAG" != "" ] ; then
echo "$REPLY" >> crtac
elif [ -l "{$REPLY}" -gt 2 ] && [ "$CRFLAG" = "" ] ; then
CRFLAG="yes"
echo "$REPLY" > crtac
fi
done
}
tac $1 | fixup
tac crtac > crfixed
rm crtac
# invoke this with 'crclean myfile'
# your cleaned-up file is now called 'crfixed'
# you may have to edit this to change paths for the temporary file and the output file
sorry, im off travelling for amonth now, but it looks like you're in capable hands...
Leon (aka Astroman)
Leon (aka Astroman)
ASKER
OZO: yes, tail has a -r option.//
ASKER
This question has a deletion request Pending
This question no longer is pending deletion
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Please try to run the following script by supplying the input filename and the output filename as the first and the second parameters.
#!/bin/sh
if [ $# -lt 2 ]; then
clear
printf "\n\n You have to submit input filename as the first argument...\n"
printf " You have to submit output filename as the second argument...\n\n"
exit 0
else
tail -r $1 > tmp1
`awk '{if (length == 0) n++; else m=n; print m}' tmp1 > tmp2`
`awk '{if (length != 0) print $1}' tmp2 > tmp3`
sort -u -o tmp5 tmp3
head -2 tmp5 > tmp6
tail -1 tmp6 > tmp4
firstln=`awk '{print $1}' tmp4`
tail +$firstln tmp1 > tmp7
tail -r tmp7 > $2 && rm tmp*
exit 0
fi
#!/bin/sh
if [ $# -lt 2 ]; then
clear
printf "\n\n You have to submit input filename as the first argument...\n"
printf " You have to submit output filename as the second argument...\n\n"
exit 0
else
tail -r $1 > tmp1
`awk '{if (length == 0) n++; else m=n; print m}' tmp1 > tmp2`
`awk '{if (length != 0) print $1}' tmp2 > tmp3`
sort -u -o tmp5 tmp3
head -2 tmp5 > tmp6
tail -1 tmp6 > tmp4
firstln=`awk '{print $1}' tmp4`
tail +$firstln tmp1 > tmp7
tail -r tmp7 > $2 && rm tmp*
exit 0
fi
sed -e '/^$/d' <file>
Will get rid of all blank lines, e.g.
"
hello
my friend
"
becomes
"
hello
myfriend
"