Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

erase paragraphs according to number of fields in first line of par

0 views
Skip to first unread message

marprus

unread,
Jan 4, 2009, 6:08:34 PM1/4/09
to
Hi
i have text file with lots of paragraphs and i need to separate
paragraphs according to number of fields in first line of par. First
line in par is alway 2 and/or 3 fields. Like this:

first middle last
other line
other line

f last
line line
line

first M last
line line
line

(so in this case first and last par have three fields and middle par
has two fields)
I believe it's possible (with awk or sed) to delete paragraphs
depending on first line. eg: delete all par with 2 fields in first
line of paragraph. thank for any help.

Ed Morton

unread,
Jan 4, 2009, 6:11:14 PM1/4/09
to

Untested:

awk -v RS= -F'\n' 'split($1,a)!=2' file

Ed.

Ed Morton

unread,
Jan 4, 2009, 9:26:11 PM1/4/09
to
awk -v RS= -F'\n' 'split($1,a,"[[:space:]]+")!=2' file

marprus

unread,
Jan 4, 2009, 9:57:02 PM1/4/09
to

>
> awk -v RS= -F'\n' 'split($1,a,"[[:space:]]+")!=2' file

when i do !=3 it leaves paragraphs with 3 fields in first line and
deletes 2-fields paragraphs but when i do !=2 it leaves both types of
paragraphs (with 2 and 3 fields in first line of par). i may need to
do more testing.

Also command deletes spaces between paragraphs and i need to preserve
spaces.
thanks

Ed Morton

unread,
Jan 5, 2009, 8:50:53 AM1/5/09
to
On Jan 4, 8:57 pm, marprus <marcel.pru...@gmail.com> wrote:
> > awk -v RS= -F'\n' 'split($1,a,"[[:space:]]+")!=2' file
>
> when i do !=3 it leaves paragraphs with 3 fields in first line and
> deletes 2-fields paragraphs but when i do !=2 it leaves both types of
> paragraphs (with 2 and 3 fields in first line of par). i may need to
> do more testing.

You may have trailing white space on each line.

> Also command deletes spaces between paragraphs and i need to preserve
> spaces.
> thanks

Add .... -v ORS="\n\n" ...

Ed.

marprus

unread,
Jan 6, 2009, 10:49:27 PM1/6/09
to
thanks, it worked 100%

awk -v RS= -F'\n' -v ORS="\n\n" 'split($1,a,"[[:space:]]+")!=3'

(i had to del trailing white spaces)

0 new messages