On 06/01/2021, at 08:11, Howard <[email protected] 
<mailto:[email protected]>> wrote:
> I have a set of numbers. Within some of them is at least one number within 
> parentheses. I need to find all the lines containing numbers within 
> parentheses and extract those numbers. I also need to know which line they 
> are extracted from.


Hey Howard,

Bash text filter:

#!/usr/bin/env bash
nl -n ln | sed -En 's![0-9]*(\([0-9]+\))[0-9]*!\1!p;'

Result:

2       (10)
4       (16)
5       (10)(11)

I've left on the parentheses to make it clear when there are more than one set 
in a line.


If we're going to resort to Perl we might as well go all in:

#!/usr/bin/env perl -sw
while (<>) {
   if ( m!\(\d+\)! ) {
      s!\d*(\(\d+\))\d*!$1!g;
      print "$.\t" . $_;
   }
}

Result:

2       (10)
4       (16)
5       (10)(11)

Or just to be a bit cheeky:

#!/usr/bin/env perl -sw
while (<>) { if ( m!\(\d+\)! ) { s!\d*(\(\d+\))\d*!$1!g; print "$.\t" . $_; } }

OR

#!/usr/bin/env bash
perl -wlne '{if(m!\(\d+\)!){s!\d*(\(\d+\))\d*!$1!g;print"$.\t".$_;}}'

😎

Don't forget – you can extract text directly from the front document using the 
Find Dialog and the [Extract] button.

1) Number lines.
2) Extract lines to new doc (with Find Dialog).
3) Find/replace to leave only the desired text.

The same as Neil's suggestion but without using Process Lines Containing.

TMTOWTDI.

I generally prefer using a text filter for this sort of thing, and I have one 
that opens with a hotkey and runs via another hotkey. So writing a filter and 
running it usually takes me less time than running through the steps with 
BBEdit's other tools.  ( Usually but not always... :)

--
Best Regards,
Chris

-- 
This is the BBEdit Talk public discussion group. If you have a feature request 
or need technical support, please email "[email protected]" rather than 
posting here. Follow @bbedit on Twitter: <https://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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/bbedit/74CC204B-F53A-4EC0-8C2B-60CF212C16CE%40gmail.com.

Reply via email to