Click to See Complete Forum and Search --> : sed in FreeBsd (every Nth Line)


Brian Teaman
June 14th, 2007, 12:26 AM
I found that selete will delete every 3rd line with the following command.

sed '1~3d' file

However, my freeBSD (on Mac OS X) chokes on the '~' Anybody know what the freeBSD eqivalent would be?

Thanks in advance,

Brian

Brian Teaman
June 14th, 2007, 02:05 AM
selete -> sed (oops!)

cilu
June 14th, 2007, 06:24 AM
[ redirected ]

SuperKoko
June 14th, 2007, 11:32 AM
A bit complex, but POSIX conforming:

{p
s/.*//
N
s/.*//
N
s/.*//
N
D
}


This may be simplified:

{p
N
N
s/.*//
N
D
}

But, this may reduce limits on line sizes.

For less trivial sed tasks, you may do a two steps sed.
First, prefixing lines by 0 or 1, indicating which type of line it is:

{s/.*/1&/
n
s/.*/0&/
n
s/.*/0&/
}

Then, since lines are tagged, you can easily pipe this output to another sed script:

/^0/d
s/^1//