On 05/02/2018, at 12:25, Matthew London <mlon...@gmail.com 
<mailto:mlon...@gmail.com>> wrote:
> I have a file with the following:
> 
> &&&
> TEXT STRING I WANT TO KEEP
> ###
> TEXT STRING I WANT TO DELETE
> &&&
> TEXT STRING I WANT TO KEEP
> etc,
> 
> Where there is a linebreak after &&&, ###, and all text strings


Hey Matthew,

You weren't clear about whether your source text contains strings other than 
&&&…keep and ###…delete, so for the moment I'll assume these two types are the 
only content in the file.


For information on installing and using BBEdit's text filters see (here 
<http://www.bbeditextras.org/wiki/index.php?title=Text_Filters>).


This text-filter removes all but your keep text.  It does this by finding your 
keep symbol and printing the following line.

#!/usr/bin/env bash
sed -En '/^&&&/{n;p;}' # Print next line after found regex.


This text-filter prints the keep symbol and the following line.

#!/usr/bin/env bash
sed -En '/^&&&/,/[^[:blank:]]/{p;}' # Print found Regex and the next line after 
it.


Sed is very quick and efficient for this sort of job, but it's not the only 
game in town.


This text-filter uses egrep to find the regex and the line after it.  It then 
uses sed to delete any record separators “--” egrep places between found 
records.

#!/usr/bin/env bash

egrep -i -A1 "^&&&" | sed -E '/^--/d'

Output:

&&&
TEXT STRING I WANT TO KEEP
&&&
TEXT STRING I WANT TO KEEP


We can build on the above and also delete the keep symbol.

#!/usr/bin/env bash

egrep -i -A1 "^&&&" | sed -E '/^(--|&&&)/d'

Output:

TEXT STRING I WANT TO KEEP
TEXT STRING I WANT TO KEEP


Lastly I'll trot out the 900lb gorilla — Perl.

#!/usr/bin/env perl -sw

while (<>) {
    print if $,; $, = /^&&&/
}

Output:

TEXT STRING I WANT TO KEEP
TEXT STRING I WANT TO KEEP


#!/usr/bin/env perl -sw

while (<>) {
    print if /^&&&/;
    print if $,; $, = /^&&&/
}

Output:

&&&
TEXT STRING I WANT TO KEEP
&&&
TEXT STRING I WANT TO KEEP


--
Best Regards,
Chris

-- 
This is the BBEdit Talk public discussion group. If you have a 
feature request or would like to report a problem, please email
"supp...@barebones.com" rather than posting to the group.
Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>
--- 
You received this message because you are subscribed to the Google Groups 
"BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to bbedit+unsubscr...@googlegroups.com.
To post to this group, send email to bbedit@googlegroups.com.
Visit this group at https://groups.google.com/group/bbedit.

Reply via email to