1753717 Members
4540 Online
108799 Solutions
New Discussion юеВ

Formatting

 
SOLVED
Go to solution
SAM_24
Frequent Advisor

Formatting

Hi,

I have a text data file. I want to delete part of the file contents. Anything between START and END I want to delete.

EX:

line1
line2
START
line3
-
-
Line7
END
some lines
START
some lines
END

Thanks.
Never quit
6 REPLIES 6
Ceesjan van Hattum
Esteemed Contributor
Solution

Re: Formatting

Hi,
Assuming you want not only delete the lines between START and END, but also START and END themselves, you can use the following:

awk '{if ($1=="START") del=1;
if (del!=1) print $0;
if ($1=="END") del=0;
}' DATAFILE

In case you want to leave the START-END lines itselve, you should use:

awk '{ if ($1=="END") del=0;
if (del!=1) print $0;
if ($1=="START") del=1;
}' DATAFILE

Regards,
Ceesjan
Steven Mertens
Trusted Contributor

Re: Formatting

hi ,

#!/bin/sh

check=0

while read line
do
if [ "$line" = "START" ]
then
check=1
elif [ "$line" = "END" ]
then check=0
elif [ $check -eq 0 ]
then
echo $line
fi
done < data.txt

regards.
Steven
Steven Sim Kok Leong
Honored Contributor

Re: Formatting

Hi,

Here's another way:

# cat datafile | tr ["\n"] [+] | perl -e 'while () { $_ =~ s/\+START.+?END//g ; print $_; }' | tr [+] ["\n"]

This is assuming that you don't have a plus (+) sign in any of your text. + can be replaced by any other separator character not found in your needed text.

# cat datafile
this is line 1
this is line 2
START
this is line 3
this is line 4
this is line 5
this is line 6
END
this is line 8
START
this is line 10
END

# cat datafile | tr ["\n"] [+] | perl -e 'while () { $_ =~ s/\+START.+?END//g; print $_; }' | tr [+] ["\n"]
this is line 1
this is line 2
this is line 8

Hope this helps. Regards.

Steven Sim Kok Leong
H.Merijn Brand (procura
Honored Contributor

Re: Formatting

l1:/tmp 118 > cat xx
line1
line2
START
line3
-
-
Line7
END
some lines 1
START
some lines 2
END
l1:/tmp 119 > perl -ne '/^START/../^END/ or print' xx
line1
line2
some lines 1
l1:/tmp 120 >
Enjoy, Have FUN! H.Merijn
SAM_24
Frequent Advisor

Re: Formatting

Thank you all.
It is working fine.
Never quit
Frank Slootweg
Honored Contributor

Re: Formatting

I know that you already have several solutions, include procura's simple perl one, but I just would like you to know that even 'simple' sed(1) can do this in a simple way, i.e. as simple as perl:

sed '/START/,/END/d' output