Operating System - HP-UX
1834150 Members
2419 Online
110064 Solutions
New Discussion

Removing spaces between lines

 
SOLVED
Go to solution
N Gopal
Frequent Advisor

Removing spaces between lines

Hi All,

I am having one file say abc.txt. File has two fields.Entries of files :

Prefix description

"202xxxx" This is hhhhh tatat.

"303nnnnn" This is 2nd line.



"606hhhh" This is 3rd line.
......
.... and so on.

Between two lines many blank lines are there. I want to remove all blank line.
Logic i have written is:

Code:
****************************************
var1=`wc -l abc.txt.txt | cut -f1 -d' '`

#This will take total no of lines in file.

count=1
while read line
do

length=`echo $line | wc -w`
if [ $length -eq 0 ]; then
echo " NULL LINE "
else
`echo $line >> abc_final.txt`
fi;
if [ $count -eq $var ]; then
echo " Breaking loop "
break;
fi;
count=`expr $count + 1`
done < abc.txt

******************************************
Can some one suggest any simple and effective way to acieve this?

Thanks

9 REPLIES 9
Richard Hepworth
Esteemed Contributor

Re: Removing spaces between lines

Hi,

You can use awk to achieve this:

cat abc.txt | awk '$0!~/^$/ {print $0}' > abc.txt.new

regards,

Richard
Oviwan
Honored Contributor

Re: Removing spaces between lines

Hey

Check this from http://sed.sourceforge.net/sed1line.txt:

# delete ALL blank lines from a file (same as "grep '.' ")
sed '/^$/d' # method 1
sed '/./!d' # method 2

example:
$sed '/./!d' yourfile >> newfile

Regards

john korterman
Honored Contributor

Re: Removing spaces between lines

Hi,

grep -v "^$" abc.txt >newfile

would also be possible.

regards,
John K.
it would be nice if you always got a second chance
Srikanth Arunachalam
Trusted Contributor

Re: Removing spaces between lines

Hi Gopal,

Simply do sed '/^$/d' abc.txt > def.txt that should suffice.

where abc.txt will have quiet few new lines in it, like
abc


qasd

sd
EOF

def.txt will be
abc
qasd
sd
EOF

Thanks,
Srikanth
James R. Ferguson
Acclaimed Contributor
Solution

Re: Removing spaces between lines

Hi:

The problem with reqular expressions like:

/^$/

...is that this assumes that a "blank" line consists only of a newline character without any intervening spaces and/or tabs.

If you wish to use 'sed':

# sed '/^[ ]*$/d' file

...where the expression in brackets consists of a space followed by a tab character, will work. Some 'sed' variations (but not HP's) allow a '\t' in lieu of typing the actual TAB.

Otherwise, I prefer:

# perl -ne 'print unless /^\s*$/' file

The '\s' represents "whitespace" which is defined to include both spaces and tabs. Hence if a line consists of zero or more characters of whitespace only, it is skipped.

Regards!

...JRF...
Sandman!
Honored Contributor

Re: Removing spaces between lines

Try the ex(1) construct below:

ex -s file <g/^$/d
x
EOF
N Gopal
Frequent Advisor

Re: Removing spaces between lines

Hi All,

Appologies for very delay in responding.

Both solutions given by James is working for me. I am choosing perl out of that.

Thanks a lot to all for their replies.

Regards
Dennis Handly
Acclaimed Contributor

Re: Removing spaces between lines

There is a command to do that: rmnl(1)
Bill Hassell
Honored Contributor

Re: Removing spaces between lines

The simplest way is to use ssp and rmnl (check the man pages). rmnl removes all blank lines except those at the front of the file. ssp removes the blanks lines at the front and then reduces all subsequent blank lines to just one. This will remove all your blank lines:

cat $MYFILE | ssp | rmnl >> abc_final.txt

Note that if the incoming file is badly formatted with space-filled lines (which look blank but actually have blank characters) then additional steps will be needed to remove those 'looks-blank' lines.


Bill Hassell, sysadmin