Search and Remove Lines within File

Hello,

I've searched through the scripting section but could not find what I need.

I need to search for empty sections within a file and remove them.

Here is an example file:

 
Title 123
A
B
C
D
E
 
Title 098
 
Title 567
Z
Y
X

I need to remove �Title 098� because it is empty, but keep sections and title header for �Title 123� & �Title 567�

So the output would be:

 
Title 123
A
B
C
D
E
 
Title 567
Z
Y
X

The "Title" is the common header of all sections from all inputed files. I use /bin/sh.

Any assistance would be GREATLY appreciative

Be my guest.

$ cat filter.sh
#!/bin/bash
# sorry, I'm basher ...
# usage:
# cat oldfile | ./filter.sh > newfile

C=0
TITLE='Title ???'
while read LINE; do
        if [ -z "`echo "$LINE" | grep -i "Title"`" ]; then
                if [ ! -z "$LINE" ]; then
                        if [ $C == 0 ]; then
                                echo "$TITLE"
                        fi
                        C=$((C + 1))
                fi
                echo "$LINE"
        else
                TITLE="$LINE"
                C=0
        fi
done

This does not remove multiple newlines, however.

sed "/Title/{N;/\n *$/d;}" file

Thanks, anbu23! That did it!!!

Thank you dpc. I'm learning bash and wrote your script down for future purposes.

Damn. I have to learn sed. My long script looks miserable now. :slight_smile: